-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathlatency_test.js
72 lines (63 loc) · 1.69 KB
/
latency_test.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
var latency_test = module.exports;
const axios = require('axios');
const _ = require('lodash');
const async = require('async');
const nconf = require('nconf');
const url = require('url');
const test_url =
'https://' + url.parse(nconf.get('PROVISIONING_TICKET')).host + '/test';
const exit = require('./lib/exit');
/**
* test the url and return the ns
* @param {[type]} message [description]
* @param {Function} done [description]
* @return {[type]} [description]
*/
latency_test.run = function (done) {
var start = process.hrtime();
axios
.get(test_url)
.then((response) => {
var took = process.hrtime(start);
done(null, took[0] * 1e9 + took[1]);
})
.catch((err) => done(err));
};
latency_test.run_many = function (n, done) {
async.mapSeries(
_.range(n),
function (n, callback) {
latency_test.run(callback);
},
function (err, times) {
if (err) {
console.log('Error when doing the latency test, exiting.');
exit(1);
}
var sum = times.reduce(function (prev, curr) {
return prev + curr;
}, 0);
var max = times.reduce(function (prev, curr) {
return Math.max(prev, curr);
}, 0);
var min = times.reduce(function (prev, curr) {
return Math.min(prev, curr);
}, Infinity);
var result = [sum / n, max, min].map(function (nanos) {
return (nanos / 1e6).toFixed(2);
});
console.log(
'latency test took avg: %d ms, max: %d ms, min: %d ms',
result[0],
result[1],
result[2]
);
if (done) {
done(null, result);
}
}
);
};
if (require.main === module) {
latency_test.run_many(10);
}