-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
194 lines (161 loc) · 5.6 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const rp = require('request-promise-native');
const Bottleneck = require('bottleneck');
const async = require('async');
const ProgressBar = require('progress');
const argv = require('yargs')
.usage('Usage: node $0 --url=[URL] --max=[NUM] [OPTIONS]')
.option({
'u': {
alias: 'url',
describe: 'URL to test, best to be in quotation marks',
demandOption: true,
},
'per': {
describe: 'Set measurement basis',
choices: ['second','minute'],
},
't': {
alias: 'test',
describe: 'What to test and measure, defaults to persecond',
choices: ['concurrent', 'persecond'],
}
})
.describe('min', 'Minimum value for tests')
.describe('max', 'Maximum value for tests')
.describe('concurrent', 'Number of concurrent requests')
.alias('h', 'help')
.demandOption(['max'])
.argv;
const url = argv.url;
const max = argv.max || 20;
const min = argv.min || max;
const reqOptions = {
simple: true,
resolveWithFullResponse: true,
time: true,
}
let perTime;
let testSettings;
const perSecond = {
seconds: 1,
label: 'per second',
};
const perMinute = {
seconds: 60,
label: 'per minute',
}
switch (argv.per) {
case 'second':
perTime = perSecond;
break;
case 'minute':
perTime = perMinute;
break;
default:
perTime = perSecond;
break;
}
prepTest();
function calcMedian(values) {
if (values.length === 0) return 0;
values.sort(function(a,b){
return a-b;
});
var half = Math.floor(values.length / 2);
if (values.length % 2) return values[half];
return Math.round((values[half - 1] + values[half]) / 2.0);
}
function calcAverage(values) {
if (values.length === 0) return 0;
let average = values.reduce((a, b) => a + b) / values.length;
return Math.round(average);
}
// Test response time to figure out how many requests to send in a minute
function prepTest() {
var speedtestTimings = [];
async.timesLimit(10, 1, async function(prepNum){
try {
const speedtest = await rp(url, reqOptions);
speedtestTimings.push(speedtest.timingPhases.total);
// var speedtestTiming = 150; // Pretend it's 500 ms
// console.log(speedtestTiming, 'ms per run');
} catch (error) {
console.error(error.message);
}
}, (error) => {
if (error) console.error(error.message);
const mediaTiming = calcMedian(speedtestTimings);
const averageTiming = calcAverage(speedtestTimings);
// console.log(speedtestTiming);
console.log(mediaTiming, 'ms median per test');
console.log(averageTiming, 'ms average per test');
console.log('Pausing for 10 seconds before continuing …');
setTimeout(() => {
runTest(averageTiming);
}, 10 * 1000);
});
}
function runTest(speedtestTiming) {
// How many different rate limits to test
const limitsToTest = max - min + 1;
console.log(limitsToTest, 'limits to test:', min, 'to', max, perTime.label);
async.timesLimit(limitsToTest, 1, function(limitTestNum, nextTest) {
// The limit to test
testLimit = min + limitTestNum;
if (argv.test == 'concurrent') {
limiter = new Bottleneck({
maxConcurrent: testLimit
});
timesPerSecond = speedtestTiming / 1000;
// console.log(timesPerSecond);
// if (timesPerSecond > max) timesPerSecond = max;
// console.log(timesPerSecond);
timesPerMinute = (60 / timesPerSecond) * testLimit;
// console.log(timesPerMinute);
} else if (argv.test == 'persecond') {
limiter = new Bottleneck({
reservoir: testLimit, // initial value
reservoirRefreshAmount: testLimit,
reservoirRefreshInterval: perTime.seconds * 1000, // must be divisible by 250
maxConcurrent: argv.concurrent || 1,
});
timesPerSecond = (1000 / speedtestTiming);
// console.log(timesPerSecond);
if (timesPerSecond > testLimit) timesPerSecond = testLimit;
// console.log(timesPerSecond);
timesPerMinute = 60 * timesPerSecond;
}
// Run a minute's worth of tests
const timesToRun = Math.round(timesPerMinute);
console.log(testLimit, 'per time in', timesToRun, 'runs');
var bar = new ProgressBar('|:bar| :percent :elapsed', {
total: timesToRun,
// clear: true
width: 40,
});
async.times(timesToRun, (runNum, nextRun) => {
// console.log('run', runNum, 'for', testLimit, 'per time');
limiter.schedule(() => rp(url, reqOptions))
.then((result) => {
bar.tick();
// console.log(result.statusCode);
nextRun();
})
.catch((error) => {
console.error('1', error.message);
nextRun(`\n${testLimit} per time failed (${error.statusCode})`);
});
}, (error) => { // Do this when finished or failed
if (error) {
console.error('2', error.message);
nextTest(error);
} else {
console.log('Success');
nextTest();
};
});
}, (error) => { // Do this when finished or failed
if (error) console.log(error);
process.exit();
});
}