-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestWorker.js
60 lines (60 loc) · 2.19 KB
/
testWorker.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
import { CMyWorker } from "./MyWorker.js";
let globalVar;
import { Worker } from "webworker-threads";
function Calculate(cyclesCount = 1) {
let start = Date.now();
globalVar = start;
//return start;
let count = CycleSize * cyclesCount;
let stop = start + count;
let sum = 0;
for (let i = start; i < stop; i++)
sum += Math.sqrt(i);
//for(let i=0; i<count; i++)
//sum += Math.sqrt(i+globalVar);
//console.log("result: ",sum," Elapsed:",Date.now()-start,"ms");
return sum;
}
var onmessage = async function (ev) {
let result = Calculate(1);
postMessage(result);
};
function JSON_clone(obj) { return JSON.parse(JSON.stringify(obj)); }
const CycleSize = 1000000;
export async function Test(nthreads) {
let a = new Worker("AAA");
console.log("!!! ", a);
//if ((nthreads as any) instanceof String) nthreads= Number.parseInt(nthreads as any);
let workers = [];
let tasks = [];
let time = Date.now();
let threadCount = 4;
let isParallel = Boolean(0);
if (nthreads != undefined) {
threadCount = nthreads;
isParallel = threadCount > 1;
}
//threadCount=1;
//isParallel=false;
console.log(isParallel ? "Параллельное вычисление: " + threadCount + " потоков" : "Одиночное вычисление");
//console.log(threadCount);
let sum = 0;
if (threadCount > 0)
for (let n = 0; n < 240 / threadCount; n++)
if (!isParallel)
sum += Calculate(threadCount);
else {
for (let i = 0; i < threadCount; i++) {
let worker = n == 0 ? workers[i] = new CMyWorker("testWorker.js") : workers[i]; //if (n == 0) workers.push(worker);
let task = worker.send(null);
tasks.push(task);
//if (i==0) console.log(n);
}
}
if (isParallel)
console.log(tasks.length + " задач");
if (isParallel)
sum = (await Promise.all(tasks)).reduce((prev, curr) => prev + curr);
console.log("Elapsed total:", Date.now() - time, "ms, Sum=", sum);
}
Test(2);