-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_worker_threads.js
132 lines (108 loc) · 3.17 KB
/
node_worker_threads.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
const EXPERIMENT_SCRIPT = "./fib_worker.js"; // "./primality_worker.js"
const {
Worker,
isMainThread,
parentPort,
workerData,
} = require("worker_threads");
const NUM_EXPERIMENTS = 10;
let time_measure = [];
function fibonacci(num) {
if (num === 1 || num === 2) {
return 1;
} else {
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
function isPrime(n) {
let threshold = Math.ceil(Math.sqrt(n));
if (n === 2 || n === 3) {
return true;
}
if (n % 2 === 0) {
return false;
}
for (let i = 3; i < threshold; i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
}
function mean_std(vals) {
let sum = 0, mean, std = 0;
for (let i = 0; i < vals.length; i++) {
sum += vals[i];
}
mean = sum / vals.length;
for (let i = 0; i < vals.length; i++) {
std += Math.pow((vals[i] - mean), 2);
}
std = Math.sqrt(std / (vals.length));
return [mean, std];
}
function populate(num, numThreads = 4) {
let arr = [];
for (let i = 0; i < numThreads; i++) {
arr.push({num});
}
return arr;
}
function runExperiment(k) {
const threads = new Set();
if (isMainThread) {
const parts = populate(44);
let count = 0;
const t0 = performance.now();
for (let i = 0; i < parts.length; i++) {
threads.add(
new Worker(__filename, {
workerData: {
num: parts[i].num
},
})
);
}
threads.forEach((thread) => {
thread.on("error", (err) => {
throw err;
});
thread.on("exit", () => {
threads.delete(thread);
// console.log(`Thread exiting, ${threads.size} running...`);
if (++count == 4) {
const t1 = performance.now();
const t = (t1 - t0);
time_measure.push(t);
// console.log("Duration " + t + " ms.")
if (k == NUM_EXPERIMENTS) {
let c = mean_std(time_measure);
let mean = c[0];
let stdev = c[1];
console.log("Number of runs: " + NUM_EXPERIMENTS);
console.log("Mean running time: " + mean + " ms.");
console.log("Standard deviation running time: " + stdev);
} else {
runExperiment(k + 1);
}
}
});
thread.on("message", (msg) => {
console.log(msg);
});
});
} else {
if (EXPERIMENT_SCRIPT.indexOf("fib_worker.js") !== -1) {
const f = fibonacci(workerData.num);
} else if (EXPERIMENT_SCRIPT.indexOf("primality_worker.js") !== -1) {
for (let i = 0; i < 2500; i++) {
isPrime(87178291199);
isPrime(78736136153);
}
}
// parentPort.postMessage(
// `Fibonnaci ${workerData.num}: ${f}`
// );
}
}
runExperiment(1);