-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
168 lines (151 loc) · 3.58 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var request = require( 'request' );
var fs = require( 'fs' );
var threads = 10;
var renderingTimeout = 5 * 60; // 5 minutes
var stats = {
successful: 0,
timedOut: 0,
errors: 0
};
var pageList = null;
var file = process.argv[2];
if ( file ) {
var res = fs.readFileSync( file, { encoding: 'utf8' } );
if ( !res ) {
console.log( 'Cannot open file ' + file );
process.exit( 1 );
}
pageList = res.split( '\n' );
}
for ( var i = 0; i < threads; i++ ) {
runThread( i );
}
function runThread( id ) {
var baseUrl = 'http://en.wikipedia.org/';
var log = function( msg ) {
console.log( '[' + id + '] ' + msg );
};
var get = function( url, query, callback, json ) {
var options = {
url: baseUrl + url,
qs: query,
headers: {
'User-Agent': "MaxSem's hellish load tester"
}
};
if ( json ) {
options.json = true;
}
request( options, function( error, response, body ) {
if ( error ) {
log( 'Error while making a request to ' + options.url + ': ' + error );
}
callback( error, response, body );
} );
};
var api = function( query, callback ) {
query.format = 'json';
get( 'w/api.php', query, callback, true );
};
var isFailedRequest = function( body ) {
return body.indexOf( 'coll-request_failed' ) > 0;
};
var testPage = function( title ) {
log( 'Attempting to render page ' + title );
var matches = title.match( /^((https?:)?\/\/[^\/]+\/)wiki\/(.*)$/ );
if ( matches ) {
baseUrl = matches[1];
title = decodeURIComponent( matches[3] );
}
var startTime = process.uptime();
// https://en.wikipedia.org/w/index.php?title=Special:Book&bookcmd=render_article&arttitle=Operation+Cobra&oldid=581927656&writer=rl
var query = {
title: 'Special:Book',
bookcmd: 'render_article',
arttitle: title,
writer: 'rl',
uselang: 'qqx'
};
var doTest = function() {
// https://en.wikipedia.org/w/index.php?title=Special:Book&bookcmd=download&collection_id=7d167509c3b51e92&writer=rl&return_to=Operation+Cobra
if ( process.uptime() - startTime > renderingTimeout ) {
log( 'Page ' + title + ' timed out' );
stats.timedOut++;
nextPage();
}
get(
'w/index.php',
query,
function( err, res, body ) {
if ( err || isFailedRequest( body ) ) {
stats.errors++;
nextPage();
} else if ( body.indexOf( 'bookcmd=download&collection_id' ) > 0 ) {
log( 'Download link found, proceeding' );
stats.successful++;
nextPage();
} else {
setTimeout( doTest, 2000 );
}
}
);
};
doTest();
};
var nextRandomPage = function() {
log( 'Requesting a random page...' );
api(
{
action: 'query',
list: 'random',
rnlimit: 1,
rnnamespace: 0
},
function( err, res, body ) {
if ( !err ) {
if ( body.query && body.query.random ) {
testPage( body.query.random[0].title );
return;
} else {
console.log(body);
process.exit( 1 );
}
}
nextPage();
}
);
};
var nextListPage = function() {
do {
var page = pageList.shift().trim();
if ( page.indexOf( '#' ) === 0 ) {
page = false;
}
} while ( !page );
if ( page ) {
testPage( page );
} else {
log( 'No more pages' );
// End of thread
}
};
var nextPage = function() {
if ( pageList ) {
nextListPage();
} else {
nextRandomPage();
}
}
nextPage();
}
function reportStats() {
if ( stats ) { // Prevent multiple invocation
console.log( 'Statistics:' );
console.log( stats );
stats = null;
process.exit();
}
}
process.on( 'exit', reportStats );
process.on( 'SIGINT', reportStats );
process.on( 'uncaughtException', reportStats );