-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
193 lines (174 loc) · 7.15 KB
/
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const chai = require('chai');
const should = chai.should();
const workerThreads = require('worker_threads');
const SharedMap = require('./index.umd');
const dict = require('./words_dictionary.json');
const words = Object.keys(dict);
const MAPSIZE = Math.floor(words.length * 1.01);
const KEYSIZE = 48;
const OBJSIZE = 16;
const NWORKERS = require('os').cpus().length;
const PASSES = 4;
function testMap(map, mypart, parts, out) {
const t0 = Date.now();
for (let pass = 0; pass < PASSES; pass++) {
out(`t: ${mypart} pass ${pass} of ${PASSES}`);
/**
* Test 1: Interleaved write
*/
for (let i = mypart; i < words.length; i += parts) {
map.set(words[i], i);
}
out(`t: ${mypart} ${map.length}/${map.size} elements in map`);
/**
* Test 2: Interleaved read of all words written during Test 1
*/
for (let i = mypart; i < words.length; i += parts) {
const v = map.get(words[i]);
if (+v != i)
throw new Error(`value mismatch ${words[i]} ${i} != ${+v}`);
}
/**
* Test 3: Reading of all words, including those written by other threads
* Some could have been deleted during Test 4, but those present should
* have correct values
*/
for (let i = mypart; i < words.length; i++) {
const v = map.get(words[i]);
if (v !== undefined && +v != i)
throw new Error(`value mismatch ${words[i]} ${i} != ${+v}`);
}
out(`t: ${mypart} ${map.length}/${map.size} elements in map`);
/**
* Test 4: Interleaved delete of 1/4th of all words
*/
for (let i = mypart; i < words.length; i += 4 * parts) {
map.delete(words[i]);
}
out(`t: ${mypart} ${map.length}/${map.size} elements in map`);
/**
* Test 5: Interleaved reading of all words, verifying that
* words written in Test 1 are still there and words deleted
* in Test 4 are missing
*/
for (let i = mypart; i < words.length; i += parts) {
if (i % (parts * 4) === mypart) {
if (map.has(words[i]))
throw new Error(`element not deleted ${words[i]} = ${i}`);
} else {
const v = map.get(words[i]);
if (+v != i)
throw new Error(`value mismatch ${words[i]} ${i} != ${+v}`);
}
}
out(`t: ${mypart} ${map.length}/${map.size} elements in map`);
/**
* Test 6: Unlocked iteration of all keys
* Values can disappear at any moment
*/
let c = 0;
for (let i of map.keys())
if (map.get(i) === undefined)
out(`t: ${mypart} value ${i} deleted under our nose`);
else
c++;
out(`t: ${mypart} ${map.length}/${map.size} elements in map, counted ${c}`);
/**
* Test 7: Explicitly locked iteration of all keys
* Rewriting values with opt.lockWrite=true
*/
c = 0;
let c2 = 0;
map.lockWrite();
for (let i of map.keys({ lockWrite: true })) {
const v = +map.get(i, { lockWrite: true });
if (v === undefined)
throw new Error(`value ${i} deleted under our nose`);
else
c++;
map.set(i, v, {lockWrite: true});
}
/**
* Test 8: Locked reduce, count should be exact
* !--reduce with explicit locking is not officialy supported--!
*/
c2 = map.reduce((a) => a + 1, 0);
if (c2 !== c)
throw new Error(`counted ${c} != ${c2}`);
out(`t: ${mypart} with writeLock ${map.length}/${map.size} elements in map, counted ${c} == ${c2}`);
map.unlockWrite();
/**
* Test 9: Unlocked reduce, count is potentially false
* by the time we are finished
*/
c2 = map.reduce((a) => a + 1, 0);
out(`t: ${mypart} unlocked reduce ${map.length}/${map.size} elements in map, counted ${c2}`);
}
const ops = map.stats.get + map.stats.set + map.stats.delete;
const t = Date.now() - t0;
out(`${ops} operations in ${(t / 1000).toFixed(3)}s, ${(ops / t * 1000).toFixed(0)} ops/s, fill ratio ${(words.length / map.size * 100).toFixed(2)}`);
}
if (workerThreads.isMainThread) {
(() => {
const mySmallMap = new SharedMap(4, KEYSIZE, OBJSIZE);
for (let i = 0; i < 5; i++)
mySmallMap.set('test' + i, i);
}).should.throw(RangeError);
const myMap = new SharedMap(MAPSIZE, KEYSIZE, OBJSIZE);
myMap.set('test', 2);
myMap.set('test', 1);
myMap.set('test2', 3);
const mmap = myMap.map((v, k) => ({ k, v }));
const accu = myMap.reduce((a, x) => a + (+x), 0);
mmap.length.should.equal(2);
myMap.get('test').should.equal('1');
accu.should.equal(4);
myMap.clear();
myMap.has('test').should.equal(false);
myMap.length.should.equal(0);
should.equal(myMap.get('test'), undefined);
(() => {
myMap.delete('nonexisting');
}).should.throw(RangeError);
(() => {
myMap.set('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'b');
}).should.throw(RangeError);
(() => {
myMap.set('b', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
}).should.throw(RangeError);
const workers = new Array(NWORKERS).fill(undefined);
for (let w in workers) {
workers[w] = new workerThreads.Worker('./test.js', { workerData: { map: myMap, part: w, parts: NWORKERS } });
workers[w].on('online', () => console.log(`worker ${w} started`));
workers[w].on('message', (m) => console.log(m));
workers[w].on('error', (e) => { console.log(e); myMap.__printMap(); });
workers[w].on('exit', () => {
console.log(`worker ${w} finished`);
workers[w].finished = true;
if (workers.reduce((a, x) => a && x.finished, true)) {
let newMap = {};
for (let k of myMap.keys()) {
if (newMap['xx' + k] !== undefined) {
console.log(k, newMap['xx' + k], myMap.get(k));
myMap.__printMap();
throw k;
}
newMap['xx' + k] = myMap.get(k);
}
console.log('all finished, checking consistency');
const wordsDeleted = Math.ceil(Math.ceil(words.length / 4) / NWORKERS) * NWORKERS;
myMap.length.should.equal(words.length - wordsDeleted);
for (let k of myMap.keys())
myMap.get(k).should.not.be.a('undefined');
}
});
}
} else {
const myMap = workerThreads.workerData.map;
/* This needs a more elegant way of doing it
* https://github.com/nodejs/help/issues/1558
*/
Object.setPrototypeOf(myMap, SharedMap.prototype);
testMap(myMap, +workerThreads.workerData.part, +workerThreads.workerData.parts,
workerThreads.parentPort.postMessage.bind(workerThreads.parentPort));
}