1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const binarySearchBounds = require("../util/binarySearchBounds");
|
9 |
|
10 | class ParallelismFactorCalculator {
|
11 | constructor() {
|
12 | this._rangePoints = [];
|
13 | this._rangeCallbacks = [];
|
14 | }
|
15 |
|
16 | range(start, end, callback) {
|
17 | if (start === end) return callback(1);
|
18 | this._rangePoints.push(start);
|
19 | this._rangePoints.push(end);
|
20 | this._rangeCallbacks.push(callback);
|
21 | }
|
22 |
|
23 | calculate() {
|
24 | const segments = Array.from(new Set(this._rangePoints)).sort((a, b) =>
|
25 | a < b ? -1 : 1
|
26 | );
|
27 | const parallelism = segments.map(() => 0);
|
28 | const rangeStartIndices = [];
|
29 | for (let i = 0; i < this._rangePoints.length; i += 2) {
|
30 | const start = this._rangePoints[i];
|
31 | const end = this._rangePoints[i + 1];
|
32 | let idx = binarySearchBounds.eq(segments, start);
|
33 | rangeStartIndices.push(idx);
|
34 | do {
|
35 | parallelism[idx]++;
|
36 | idx++;
|
37 | } while (segments[idx] < end);
|
38 | }
|
39 | for (let i = 0; i < this._rangeCallbacks.length; i++) {
|
40 | const start = this._rangePoints[i * 2];
|
41 | const end = this._rangePoints[i * 2 + 1];
|
42 | let idx = rangeStartIndices[i];
|
43 | let sum = 0;
|
44 | let totalDuration = 0;
|
45 | let current = start;
|
46 | do {
|
47 | const p = parallelism[idx];
|
48 | idx++;
|
49 | const duration = segments[idx] - current;
|
50 | totalDuration += duration;
|
51 | current = segments[idx];
|
52 | sum += p * duration;
|
53 | } while (current < end);
|
54 | this._rangeCallbacks[i](sum / totalDuration);
|
55 | }
|
56 | }
|
57 | }
|
58 |
|
59 | module.exports = ParallelismFactorCalculator;
|