-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.js
88 lines (71 loc) · 2.91 KB
/
bench.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
/**
* Created by xx on 15/7/2.
*/
var async = require("async");
var request = require('superagent');
var validator = require('validator');
var agent = request.agent();
Array.prototype.sum = function () {
for (var sum = i = 0; i < this.length; i++)sum += parseInt(this[i]);
return sum
};
var bench = function (arg_param) {
if (!validator.isInt(arg_param.count)) throw new Error("count is not a number");
if (!validator.isURL(arg_param.url)) throw new Error("wrong url");
if (arg_param.method != "post" && arg_param.method != "get") throw new Error("wrong method");
var pass = 0;
arg_param.param = arg_param.param || {};
function httpReq(callback) {
var time = process.hrtime();
var req = request[arg_param.method](arg_param.url);
if (arg_param.needCookie) agent.attachCookies(req);
req.send(arg_param.param)
.end(function (err, res) {
//if (err) return callback(null,err);
if (res && arg_param.resultAssert && res.text && validator.isJSON(res.text)) {
var result = JSON.parse(res.text);
for (var i in arg_param.resultAssert) {
if (arg_param.resultAssert.hasOwnProperty(i) && result.hasOwnProperty(i) && result[i] === arg_param.resultAssert[i]) {
pass++;
}
}
} else if (!arg_param.resultAssert) {
pass++;
}
var diff = process.hrtime(time);
callback(null, diff[0] * 1e9 + diff[1]);
});
};
var reqArray = [];
for (var i = 0; i < arg_param.count; i++) {
reqArray.push(httpReq);
}
var time = process.hrtime();
async.series({
saveCookie: function (callback) {
if (!arg_param.needCookie) return callback(null);
if (!arg_param.getCookieUrl) return callback(new Error("need cookie url"));
request.post(arg_param.getCookieUrl)
.send(arg_param.getCookieParam)
.end(function (err, res) {
if (err) {
callback(err);
}
agent.saveCookies(res);
callback();
});
},
req: function (callback) {
async.parallel(reqArray, callback);
}
},
function (err, results) {
if (err) return console.log(err);
var diff = process.hrtime(time);
console.log('pass %d', pass);
console.log('pass cent %d %', (pass / arg_param.count) * 100);
console.log('benchmark took %d s', (diff[0] * 1e9 + diff[1]) / 1000000000);
console.log('benchmark took %d s/op', (results.req.sum() / arg_param.count / 1000000000));
});
};
module.exports = bench;