generated from sonofmagic/npm-lib-template
-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathbench.js
69 lines (63 loc) · 1.45 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
const path = require('node:path')
const fs = require('fs-extra')
const dayjs = require('dayjs')
const { set, get } = require('lodash')
const useBabel = process.env.BABEL
console.log('useBabel:', Boolean(useBabel))
class Bench {
constructor(name) {
this.name = name
}
start() {
this.startTs = performance.now()
}
end() {
this.endTs = performance.now()
}
timeSpan() {
return this.endTs - this.startTs
}
get useBabel() {
return Boolean(useBabel)
}
dump(key = this.useBabel ? 'babel' : 'ast-grep') {
const ts = this.timeSpan()
// if (ts < 20) {
// return
// }
const filename = dayjs().format('YYYY-MM-DD') + '.json'
const targetDataFile = path.resolve(__dirname, '../benchmark/data', filename)
if (fs.existsSync(targetDataFile)) {
const json = fs.readJsonSync(targetDataFile)
const arr = get(json, [this.name, key], [])
arr.push(ts)
set(json, [this.name, key], arr)
fs.writeJSONSync(targetDataFile, json, { spaces: 2 })
} else {
fs.writeJSONSync(
targetDataFile,
{
[this.name]: {
[key]: [ts]
}
},
{ spaces: 2 }
)
}
}
}
module.exports = function createBench(name) {
return new Bench(name)
// const bench = new Bench(name)
// return {
// start() {
// bench.start()
// },
// end() {
// bench.end()
// },
// dump(key) {
// bench.dump(key)
// }
// }
}