-
Notifications
You must be signed in to change notification settings - Fork 4
/
perf.spec.js
126 lines (110 loc) · 4.19 KB
/
perf.spec.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
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
118
119
120
121
122
123
124
125
126
/* eslint-env jest */
// init avec default data
// 100 SET d'un tableau de 1000
// 100 ADD
// 100 UPDATE
// 100 ADD_OR_UPDATE
// 100 REMOVE
// <time>
// 1000 de chaque selector
const { keyValue } = require('.')
const RERUN = 100
const RERUN_SELECTORS = 100000
const LENGTH = 1000
const rerun = callback => Array.from({ length: RERUN }).reduce(callback)
const rerunSelectors = callback => Array.from({ length: RERUN_SELECTORS }).reduce(callback)
describe('performance', () => {
const genData = (label, offset) => (curr, index) => ({
code: (index + offset),
other: `value-${(index + offset)}`,
label,
})
const data = {
defaultData: Array.from({ length: LENGTH }).map(genData('default', 0)),
sets: Array.from({ length: LENGTH }).map(genData('sets', 0)),
add: genData('add', 100000)(0),
adds: Array.from({ length: LENGTH }).map(genData('adds', 100002)),
update: genData('update', 100000)(0),
updates: Array.from({ length: LENGTH }).map(genData('updates', 100002)),
remove: genData('remove', 2)(0),
removes: Array.from({ length: LENGTH }).map(genData('removes', 4)),
addOrUpdate: genData('addOrUpdate', 100500)(0),
addOrUpdates: Array.from({ length: LENGTH }).map(genData('addOrUpdates', 100502)),
getByIds: Array.from({ length: LENGTH }).map((...args) => genData('getByIds', 120)(args).code),
}
const timers = {
writes: {},
reads: {},
}
const getReadOrWrite = (what) => {
switch (what) {
case 'sets':
case 'add':
case 'adds':
case 'update':
case 'updates':
case 'remove':
case 'removes':
case 'addOrUpdate':
case 'addOrUpdates':
return 'writes'
default: return 'reads'
}
}
const runWithTimers = (state, reducer, what, args) => {
let result
const readOrWrite = getReadOrWrite(what)
const multiple = what.endsWith('s')
const action = multiple ? what.slice(0, -1) : what
timers[readOrWrite][what] = Date.now()
if (readOrWrite === 'writes') result = rerun(() => reducer(state, reducer[action](data[what])))
else result = args !== undefined ? rerunSelectors(() => reducer[action === 'getById' ? 'get' : what](...args)(state)) : rerunSelectors(() => reducer[what](state))
timers[readOrWrite][what] = Date.now() - timers[readOrWrite][what]
timers[readOrWrite][what] = `${timers[readOrWrite][what] / (readOrWrite === 'writes' ? RERUN : RERUN_SELECTORS)}ms / op [${multiple ? LENGTH : 1}]`
return result
}
it('should print perf', () => {
let state
timers.all = Date.now()
// writes
timers.writes.total = Date.now()
// - init
timers.writes.init = Date.now()
const reducer = rerun(() => keyValue({ name: 'perf', key: 'code', defaultData: data.defaultData }))
state = reducer()
timers.writes.init = Date.now() - timers.writes.init
timers.writes.init = `${timers.writes.init / RERUN}ms / op [1000]`
// - writes
state = runWithTimers(state, reducer, 'sets')
state = runWithTimers(state, reducer, 'add')
state = runWithTimers(state, reducer, 'adds')
state = runWithTimers(state, reducer, 'update')
state = runWithTimers(state, reducer, 'updates')
state = runWithTimers(state, reducer, 'remove')
state = runWithTimers(state, reducer, 'removes')
state = runWithTimers(state, reducer, 'addOrUpdate')
state = runWithTimers(state, reducer, 'addOrUpdates')
// end writes tests
timers.writes.total = Date.now() - timers.writes.total
// reads
timers.reads.total = Date.now()
state = { perf: state }
// - reads
runWithTimers(state, reducer, 'getState')
runWithTimers(state, reducer, 'getKeys')
runWithTimers(state, reducer, 'getAsArray')
runWithTimers(state, reducer, 'getLength')
runWithTimers(state, reducer, 'isInitialized')
runWithTimers(state, reducer, 'get', [])
runWithTimers(state, reducer, 'getById', [19])
runWithTimers(state, reducer, 'getByIds', [data.getByIds])
runWithTimers(state, reducer, 'hasKey', [137])
// - getBy
// TODO:
// end reads tests
timers.reads.total = Date.now() - timers.reads.total
// prints
timers.all = Date.now() - timers.all
console.log('results (ms):', JSON.stringify(timers, null, 2))
})
})