-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperform.test.js
90 lines (83 loc) · 2.57 KB
/
perform.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
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
const Benchmark = require('benchmark');
const colors = require('colors');
const fs = require('fs');
const _ = require('daguo');
const suite = new Benchmark.Suite();
const num = process.env.n || 001;
const logger = console.log;
const path = './problems';
const problems = fs.readdirSync(path);
// 根据题号匹配对应题目
const reg = new RegExp(num);
const problem = problems.find(problem => reg.test(problem));
const fns = require('./problems/' + problem + '/index.js');
const testCases = require('./problems/' + problem + '/test.js');
perfTest(problem, _.clone(testCases), fns);
/**
* 运行性能测试
* @param {*} problem 测试的题目目录
* @param {*} testCases 测试用例
* @param {*} fns 测试的函数
*/
function perfTest(problem, testCases, fns) {
logger(('<--start fns speed test-->' + problem).underline + '\n');
//非数组报错
isArray(problem, fns);
// 运算耗时排名
const timeRank = [];
// 执行字符串
const evalStr = fns.reduce(
(pre, cur, i) =>
pre +
`.add("${
cur.name
}", function() {fns[${i}].apply(null,testCases[0].input);})`,
'suite'
);
eval(evalStr)
.on('cycle', event => handleCycle(event, timeRank))
.on('complete', function() {
// 对运行时间排序
sort(timeRank);
const label = [];
timeRank.forEach(result => {
label.push(`${result.name} >>> ${result.time}`);
});
logger(`Allrank :\n${label.join('\n')}`.blue);
logger(`Fastest :\n${this.filter('fastest').map('name')}`.green);
logger(`Slowest :\n${this.filter('slowest').map('name')}`.red);
})
// 这个选项与时间计算有关
.run({ async: true });
}
function handleCycle(event, timeRank) {
const info = String(event.target);
logger(info.yellow);
timeRank.push({
name: info.split(' x ')[0],
time: info.split(' x ')[1].split(' ops/sec')[0]
});
}
/**
* 测试结果排序
* @param {*} arr
*/
function sort(arr) {
for (let i = 0, len = arr.length; i < len; i++) {
for (let j = 0; j < len - i - 1; j++) {
//比较相邻元素
if (+arr[j].time.replace(/,/g, '') < +arr[j + 1].time.replace(/,/g, '')) {
[arr[j].time, arr[j + 1].time] = [arr[j + 1].time, arr[j].time];
[arr[j].name, arr[j + 1].name] = [arr[j + 1].name, arr[j].name];
}
}
}
}
function isArray(problem, fns) {
if (Object.prototype.toString.call(fns) !== '[object Array]') {
const str = `./problems/${problem}/index.js \n--you can export a array fns for comparing speed between 2 or more fns`
.yellow;
logger(str);
return true;
}
}