-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathperformance.test.js
65 lines (60 loc) · 1.56 KB
/
performance.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
'use strict'
import engineFactory from '../src/index'
import perfy from 'perfy'
import deepClone from 'clone'
describe('Performance', () => {
const baseConditions = {
any: [{
fact: 'age',
operator: 'lessThan',
value: 50
},
{
fact: 'segment',
operator: 'equal',
value: 'european'
}]
}
const event = {
type: 'ageTrigger',
params: {
demographic: 'under50'
}
}
/*
* Generates an array of integers of length 'num'
*/
function range (num) {
return Array.from(Array(num).keys())
}
function setup (conditions) {
const engine = engineFactory()
const config = deepClone({ conditions, event })
range(1000).forEach(() => {
const rule = factories.rule(config)
engine.addRule(rule)
})
engine.addFact('segment', 'european', { cache: true })
engine.addFact('age', 15, { cache: true })
return engine
}
it('performs "any" quickly', async () => {
const engine = setup(baseConditions)
perfy.start('any')
await engine.run()
const result = perfy.end('any')
expect(result.time).to.be.greaterThan(0.001)
expect(result.time).to.be.lessThan(0.5)
})
it('performs "all" quickly', async () => {
const conditions = deepClone(baseConditions)
conditions.all = conditions.any
delete conditions.any
const engine = setup(conditions)
perfy.start('all')
await engine.run()
const result = perfy.end('all')
expect(result.time).to.be.greaterThan(0.001) // assert lower value
expect(result.time).to.be.lessThan(0.5)
})
})