-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workerTerminate.js
42 lines (37 loc) · 1.26 KB
/
workerTerminate.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
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { isMainThread, Worker } = require('worker_threads')
// a Very costly RegExp
const regEx = /(x+x+)+y+/g;
const sample = 'x'.repeat(50) + '';
if (!isMainThread) {
console.log('Worker: Starting')
const start = process.hrtime()
regEx.test(sample) ? console.log('true') : console.log('false')
console.log(`Elapsed Time ${elapsedTimeMsFrom(start)}`)
console.log('Worker: Stopping')
setTimeout(() => process.exit(0), 10)
}
if (isMainThread) {
const worker = new Worker(__filename)
setTimeout(() => {
console.log('Main: Terminating Worker')
const start = process.hrtime()
worker.terminate().then(i => {
console.log('Terminated with ' + i)
console.log(`After ${elapsedTimeMsFrom(start)}ms`)
console.log('Successfully Terminated!')
process.exit(0)
})
setTimeout(() => {
console.log('Hard Exit')
console.log(`After ${elapsedTimeMsFrom(start)}ms`)
process.exit(1)
}, 2000)
}, 500)
}
function elapsedTimeMsFrom(relativeTo) {
return hrTimeToMs(process.hrtime(relativeTo));
}
function hrTimeToMs(hrTime) {
return hrTime[0] * 1.0e-3 + hrTime[1] * 1.0e-6
}