-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.js
105 lines (85 loc) · 2.9 KB
/
benchmark.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
var eos = require("./"),
FS = require("fs"),
Path = require("path"),
Async = require("async")
require("colors")
var Cluster = require("cluster")
//for (var i = 0; i < 4; i++)
// if (Cluster.isMaster)
// require("cluster").fork()
var connectionString = "Driver={ODBC Driver 11 for SQL Server};Server=localhost,3800;Trusted_Connection=Yes"
function repeatLimit(fn, limit, times, callback) {
var start = new Date().getTime(), iterations = 0, active = 0, started = 0, finished = 0, failed = false;
var env = new eos.Environment(),
connections = []
for (var i = 0; i < limit; i++)
connections.push(i)
Async.mapLimit(connections, 16, function(_, cb) {
var conn = env.newConnection()
conn.driverConnect(connectionString, function(err) {
return cb(err, !err && conn);
})
}, function(err, results) {
if (err) {
return callback(err)
connections.forEach(function(c) {
c.disconnect(function() {
c.free()
})
})
}
connections = results
for (var i = 0; i < limit; i++)
begin(i)
})
function begin(i) {
if (started >= times)
return
started++
fn(connections[i], function(err) {
finished++
if (err) {
if (!failed) {
failed = true
callback(err)
}
return
}
if (finished >= times) {
callback()
callback = null
} else if(started < times) {
begin(i)
} else {
connections[i].disconnect(function() {
connections[i].free()
})
}
})
}
}
Async.eachSeries(FS.readdirSync(Path.join(__dirname, "benchmarks")), function(name, callback) {
if (!/\.js$/.test(name))
return callback()
var fn = require(Path.join(__dirname, "benchmarks", name))
console.log("Running benchmark", fn.name.bold.magenta)
var start = new Date().getTime(),
times = fn.times || 10000
repeatLimit(fn, fn.limit || 1, times, function(err) {
callback(err)
if (!err) {
var elapsed = new Date().getTime() - start
console.log("Benchmark", fn.name.bold.magenta,
"run", times.toString().yellow.bold,
"times in", elapsed.toString().yellow.bold + "ms,",
Math.floor(times/(elapsed/1000)).toString().yellow.bold + "/sec")
} else {
console.log("Error".bold.red, "in benchmark", fn.name.bold.magenta + ":", err.message)
}
})
}, function(err) {
if (err)
console.error(err.message)
else
console.log("Done")
})