forked from plusuncold/longest-word-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_stream.js
54 lines (47 loc) · 1.46 KB
/
test_stream.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
const fs = require('fs')
const { Worker } = require('worker_threads')
const start = Date.now()
const readStream = fs.createReadStream('corpus.txt', 'utf8')
// let proms = []
// let buffer = ''
// let i = 0
let longest = ''
readStream.on('data', function(chunk) {
chunk.split(/\s/).forEach(w => {
if (w.length > longest.length) longest = w
})
// Unfortunately, the overhead for spinning up a thread
// far outweighs the savings of concurrent processing
// buffer = buffer + chunk
// if (i % 100 === 0) {
// // console.log("pushing buffer of size ", buffer.length)
// proms.push(runService(buffer))
// buffer = ''
// }
// i++
}).on('end', () => {
// console.log("pushing buffer of size ", buffer.length)
// proms.push(runService(buffer))
// doRollup()
console.log(longest, longest.length, Date.now() - start + 'ms');
})
function doRollup () {
Promise.all(proms).then(res => {
console.log("Threads: ", res.length)
const longest = res.reduce((agg, word) => {
return agg.length > word.length ? agg : word
}, '')
console.log(longest, longest.length, Date.now() - start + 'ms');
})
}
function runService(workerData) {
return new Promise((resolve, reject) => {
const worker = new Worker('./worker.js', { workerData });
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
})
})
}