|
| 1 | +'use strict' |
| 2 | + |
| 3 | +import sinon from 'sinon' |
| 4 | +import engineFactory from '../src/index' |
| 5 | +import perfy from 'perfy' |
| 6 | +import deepClone from 'clone' |
| 7 | + |
| 8 | +describe('Performance', () => { |
| 9 | + let baseConditions = { |
| 10 | + any: [{ |
| 11 | + 'fact': 'age', |
| 12 | + 'operator': 'lessThan', |
| 13 | + 'value': 50 |
| 14 | + }, { |
| 15 | + 'fact': 'segment', |
| 16 | + 'operator': 'equal', |
| 17 | + 'value': 'european' |
| 18 | + }] |
| 19 | + } |
| 20 | + let event = { |
| 21 | + type: 'ageTrigger', |
| 22 | + params: { |
| 23 | + demographic: 'under50' |
| 24 | + } |
| 25 | + } |
| 26 | + /* |
| 27 | + * Generates an array of integers of length 'num' |
| 28 | + */ |
| 29 | + function range(num) { |
| 30 | + return Array.from(Array(num).keys()) |
| 31 | + } |
| 32 | + |
| 33 | + function setup(conditions) { |
| 34 | + let engine = engineFactory() |
| 35 | + const config = deepClone({ conditions, event }) |
| 36 | + range(1000).forEach(() => { |
| 37 | + let rule = factories.rule(config) |
| 38 | + engine.addRule(rule) |
| 39 | + }) |
| 40 | + engine.addFact('segment', 'european') |
| 41 | + engine.addFact('age', 15) |
| 42 | + return engine |
| 43 | + } |
| 44 | + |
| 45 | + it('performs "any" quickly', async () => { |
| 46 | + let engine = setup(baseConditions) |
| 47 | + perfy.start('any') |
| 48 | + await engine.run() |
| 49 | + const result = perfy.end('any') |
| 50 | + expect(result.milliseconds).to.be.greaterThan(850) // assert lower value |
| 51 | + expect(result.milliseconds).to.be.lessThan(1000) |
| 52 | + console.log(result.milliseconds); |
| 53 | + }) |
| 54 | + |
| 55 | + it('performs "all" quickly', async () => { |
| 56 | + const conditions = deepClone(baseConditions) |
| 57 | + conditions.all = conditions.any |
| 58 | + delete conditions.any |
| 59 | + let engine = setup(conditions) |
| 60 | + perfy.start('all') |
| 61 | + await engine.run() |
| 62 | + const result = perfy.end('all') |
| 63 | + expect(result.milliseconds).to.be.greaterThan(800) // assert lower value |
| 64 | + expect(result.milliseconds).to.be.lessThan(900) |
| 65 | + }) |
| 66 | +}) |
0 commit comments