-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.test.ts
More file actions
117 lines (114 loc) · 3.26 KB
/
benchmark.test.ts
File metadata and controls
117 lines (114 loc) · 3.26 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @jest-environment node
*/
import * as Benchmark from 'benchmark';
import { Flowzilla, generateID } from '../index';
if (process.env.BENCHMARK) {
test('benchmark', async cb => {
const suite = new Benchmark.Suite();
const timeout = (result: any) => (cb: any) => {
setTimeout(() => cb(result), 0);
};
// FN
function callback(type: any, value: any, cb: any) {
value.push(1);
callback2(type, value, cb);
}
function callback2(type: any, value: any, cb: any) {
value.push(2);
callback3(type, value, cb);
}
function callback3(type: any, value: any, cb: any) {
value.push(3);
timeout(value)(cb);
}
// PROMISE
function promise(type: any, value: any) {
value.push(1);
return new Promise(yay => yay(value))
.then(value => promise2(type, value))
.then(value => promise3(type, value));
}
function promise2(type: any, value: any) {
value.push(2);
return new Promise(yay => yay(value));
}
function promise3(type: any, value: any) {
value.push(3);
return new Promise(timeout(value));
}
const flowzilla = new Flowzilla();
flowzilla.addSkill((type, value, flow) => {
value.push(1);
flow(value);
});
flowzilla.addSkill((type, value, flow) => {
value.push(2);
flow(value);
});
flowzilla.addSkill((type, value, flow) => {
value.push(3);
timeout(value)(flow);
});
suite
.add(
'callback',
function(defer: any) {
callback('hans', [0], () => defer.resolve());
},
{ defer: true }
)
.add(
'promise',
function(defer: any) {
promise('hans', [0]).then(() => defer.resolve());
},
{ defer: true }
)
.add(
'flowzilla',
function(defer: any) {
flowzilla.run<any>('hans', [0], {}).then(() => defer.resolve());
},
{ defer: true }
)
.on('complete', function() {
const result = suite.reduce((store: any, i: any) => {
console.log(String(i));
return { ...store, [i.name]: i.hz };
}, {});
const perfScore = (100 / result['promise']) * result['flowzilla'];
const perfScore2 = (100 / result['callback']) * result['flowzilla'];
console.log('Score', perfScore, perfScore2);
expect(perfScore).toBeGreaterThan(80);
expect(perfScore2).toBeGreaterThan(80);
cb();
})
.run({ maxTime: 2, async: true });
}, 40000);
test('benchmark-id', async cb => {
const suite = new Benchmark.Suite();
suite
.add('wolfid', function() {
generateID();
})
.add('randstring32', function() {
Math.random()
.toString(36)
.substr(2, 9);
})
.on('complete', function() {
const result = suite.reduce((store: any, i: any) => {
console.log(String(i));
return { ...store, [i.name]: i.hz };
}, {});
const perfScore = (100 / result['randstring32']) * result['wolfid'];
console.log('Score', perfScore);
expect(perfScore).toBeGreaterThan(100);
cb();
})
.run({ maxTime: 2, async: true });
}, 40000);
} else {
test('blank', () => expect(true).toBe(true));
}