1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var tmp
|
10 | , child = require("child_process")
|
11 | , fs = require("fs")
|
12 | , path = require("path")
|
13 | , bench = require("../../test/bench")
|
14 |
|
15 | exports.execute = execute
|
16 | global.requireGit = requireGit
|
17 |
|
18 | function execute(argv, i) {
|
19 | var file = process.argv[i]
|
20 | , mod = file && require(path.join(process.cwd(), file))
|
21 |
|
22 | if (mod) {
|
23 | run(mod, execute.bind(null, argv, i + 1))
|
24 | }
|
25 | }
|
26 |
|
27 | function run(mod, next) {
|
28 | var i = 0
|
29 | if (Array.isArray(mod)) {
|
30 | loop()
|
31 | } else {
|
32 | var skip = []
|
33 | , enabled = Object.keys(mod).reduce(function(map, name) {
|
34 | if (name && name.charAt(0) !== "_") {
|
35 | map[name] = mod[name]
|
36 | } else {
|
37 | skip.push(name)
|
38 | }
|
39 | return map
|
40 | }, {})
|
41 | console.log(
|
42 | "---\nBench: %s%s\n---",
|
43 | Object.keys(enabled).join(" vs "),
|
44 | skip.length ? "\n Skip: " + skip.join(", ") : ""
|
45 | )
|
46 | bench(enabled, function(err, result) {
|
47 | Object.keys(result).forEach(function(name) {
|
48 | console.log(name + "\n " + result[name].text + " ops/s, " + result[name].rel)
|
49 | })
|
50 | if (typeof next === "function") {
|
51 | next()
|
52 | }
|
53 | })
|
54 | }
|
55 | function loop() {
|
56 | var cur = mod[i++]
|
57 | if (cur) run(cur, loop)
|
58 | else if (typeof next === "function") next()
|
59 | }
|
60 | }
|
61 |
|
62 | function requireGit(id) {
|
63 | if (!tmp) {
|
64 | tmp = fs.mkdtempSync(
|
65 | path.join(require("os").tmpdir(), "bench-")
|
66 | )
|
67 | console.log("mkdtemp", tmp)
|
68 | process.on("exit", cleanup)
|
69 | }
|
70 |
|
71 | id = id.replace(/:(.*)/, function(_, file) {
|
72 | return ":" + path.relative(process.cwd(), path.join(__filename, "..", file))
|
73 | })
|
74 |
|
75 | var cleared = tmp + "/" + id.replace(/\.?\.\/|\/|:/g, "-")
|
76 | , content = child.spawnSync("git", ["show", id]).output.join("")
|
77 |
|
78 | fs.writeFileSync(cleared, content, "utf8")
|
79 | return require(cleared)
|
80 | }
|
81 |
|
82 | function cleanup() {
|
83 | module.parent.exports.rmrf(tmp)
|
84 | }
|
85 |
|