forked from auth0/ad-ldap-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatency_test.js
61 lines (52 loc) · 1.63 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
var latency_test = module.exports;
var request = require('request');
var _ = require('lodash');
var async = require('async');
var nconf = require('nconf');
var url = require('url');
var test_url = 'https://' + url.parse(nconf.get('PROVISIONING_TICKET')).host + '/test';
var 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();
request.get(test_url, function(err){
if (err) return done(err);
var took = process.hrtime(start);
done(null, took[0] * 1e9 + took[1]);
});
};
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);
}