-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcollisions.js
136 lines (112 loc) · 2.92 KB
/
collisions.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
127
128
129
130
131
132
133
134
135
136
const workerFarm = require('worker-farm')
const { promisify } = require('util')
const { v4: uuid } = require('uuid')
const Stats = require('fast-stats').Stats
const { TRANSFORMATIONS } = require('../dist/testutils')
const METHODS = process.env.METHODS ? process.env.METHODS.split(',') : [
'username',
'email',
'int',
'dateString',
'ipv4',
'mac',
'float',
'fullName',
'streetAddress',
'postalAddress',
'password',
'uuid',
]
const MAX_N = +(process.env.MAX_N ?? 999999)
const MIN_RUNS = Math.max(2, +(process.env.MIN_RUNS ?? 100))
const MAX_SUM = +(process.env.MAX_SUM ?? MAX_N)
const LO_MOE = +(process.env.MOE ?? 0.05)
const HI_MOE = +(process.env.MOE ?? 0.10)
const MAX_DURATION = +(process.env.MAX_MS ?? 1000 * 60 * 5)
const workerOptions = {
workerOptions: {
env: {
MAX_N,
MIN_RUNS,
MAX_SUM,
LO_MOE,
HI_MOE,
MAX_DURATION,
IS_WORKER: '1',
},
}
}
const workers = workerFarm(workerOptions, require.resolve(__filename))
const runWorker = promisify(workers)
const findFirstCollisionN = (methodName) => {
const fn = TRANSFORMATIONS[methodName]
let i = -1
const seen = new Set()
let firstCollisionN = null
while (++i < MAX_N && firstCollisionN == null) {
const result = fn(uuid()).toString()
if (seen.has(result)) {
firstCollisionN = i
} else {
seen.add(result)
}
}
return firstCollisionN
}
const worker = (methodName, done) => {
const stats = new Stats()
let hasCollided = false
let sum = 0
let startTime = Date.now()
let numRuns = 0
const computeMoe = () => stats.length > 2 && stats.amean() > 0
? stats.moe() / stats.amean()
: null
const computeDuration = () => Date.now() - startTime
const isComplete = () => {
const duration = computeDuration()
const moe = computeMoe()
return numRuns >= MIN_RUNS &&
((duration >= MAX_DURATION) ||
((moe != null && (moe <= LO_MOE || (moe <= HI_MOE && sum >= MAX_SUM))) || (moe == null && !hasCollided)))
}
while (!isComplete()) {
const firstCollisionN = findFirstCollisionN(methodName)
if (firstCollisionN != null) {
hasCollided = true
sum += firstCollisionN
stats.push(firstCollisionN)
} else {
sum = MAX_N
}
numRuns++
}
const [min = null, max = null] = stats.range() ?? []
done(null, {
methodName,
mean: format(stats.amean()),
stddev: format(stats.stddev()),
moe: format(computeMoe()),
runs: numRuns,
n: stats.length,
min,
max,
sum,
hasCollided,
duration: computeDuration()
})
}
const format = (v) => v == null || isNaN(v) ? null : v.toFixed(2)
async function main() {
await Promise.all(
METHODS.map(async (methodName) => {
const results = await runWorker(methodName)
console.log(JSON.stringify(results))
})
)
workerFarm.end(workers)
}
module.exports = worker
if (require.main === module && !process.env.IS_WORKER) {
main()
}