-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
226 lines (207 loc) · 4.53 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const fs = require("node:fs");
const fsp = require("node:fs/promises");
const os = require("node:os");
const workerThreads = require("node:worker_threads");
const { OUTPUT_PATH, MEASUREMENTS_PATH, Timer } = require("../common");
const cityMap = new Map(
[
"Adenarith",
"Amsterdam",
"Anápolis",
"Aparecida de Goiânia",
"Athens",
"Austin",
"Bahrain",
"Bangalore",
"Barcelona",
"Belo Horizonte",
"Belém",
"Boa Vista",
"Boston",
"Brasília",
"Brussels",
"Bucharest",
"Campinas",
"Canada",
"Central",
"Chennai",
"Chongqing",
"Copenhagen",
"Cuiabá",
"Curitiba",
"Dallas",
"Dublin",
"Duque de Caxias",
"Feira de Santana",
"Fortaleza",
"Frankfurt",
"Gaaphis",
"Goiania",
"Guadalajara",
"Guarulhos",
"Helsinki",
"Hong Kong",
"Hyderabad",
"Indianapolis",
"Ireland",
"Istanbul",
"Juiz de Fora",
"Kiev",
"Kolkata",
"Krofast",
"Krore",
"Larfast",
"London",
"Londrina",
"Los Angeles",
"Macapá",
"Madrid",
"Manaus",
"Mexico City",
"Miami",
"Milan",
"Montreal",
"Moscow",
"Mumbai",
"N. California",
"N. Virginia",
"New Delhi",
"New York",
"Niterói",
"Nova Iguaçu",
"Ohio",
"Oregon",
"Osaka",
"Osasco",
"Oslo",
"Palmas",
"Paris",
"Porto Alegre",
"Porto Velho",
"Prico",
"Prover",
"Pune",
"Qreigh",
"Qrokwood",
"Recife",
"Ribeirão Preto",
"Rio de Janeiro",
"Salvador",
"Santo André",
"Sao Paulo",
"Seoul",
"Singapore",
"St. Petersburg",
"Stockholm",
"Sydney",
"São Bernardo do Campo",
"São Gonçalo",
"São José dos Campos",
"São Paulo",
"Tokyo",
"Toronto",
"Urgtin",
"Vancouver",
"Vienna",
"Warsaw",
"Zurich",
].map((v) => [
v,
{
min: Number.MAX_SAFE_INTEGER,
max: 0,
total: 0,
count: 0,
},
])
);
const BUFFER_SIZE = 128;
async function solution() {
const file = await fsp.open(MEASUREMENTS_PATH);
const size = (await file.stat()).size;
const workerCount = os.cpus().length - 1;
const chunkSize = ~~(size / workerCount); // fast Math.floor
const threadHeapSize = 1024;
const offsets = [];
const buffer = Buffer.alloc(BUFFER_SIZE);
let offset = 0;
while (true) {
offset += chunkSize;
if (offset >= size || offset < 0) {
offsets.push(size);
break;
}
await file.read(buffer, 0, BUFFER_SIZE, offset);
const pos = buffer.indexOf(10); // \n
buffer.fill(0);
if (pos !== -1) {
offset += pos + 1;
offsets.push(offset);
} else {
offsets.push(size);
break;
}
}
file.close();
await Promise.all(
offsets.map(async (v, idx, array) => {
return new Promise((resolve, _) => {
const worker = new workerThreads.Worker(
"./nodejs/sunrabbit2/worker.js",
{
workerData: {
path: MEASUREMENTS_PATH,
start: idx === 0 ? 0 : array[idx - 1],
end: v,
},
resourceLimits: {
maxOldGenerationSizeMb: threadHeapSize,
},
}
);
worker.on("message", (message) => {
for (const [key, value] of message.entries()) {
const existing = cityMap.get(key);
existing.min = Math.min(existing.min, value.min);
existing.max = Math.max(existing.max, value.max);
existing.total += value.total;
existing.count += value.count;
}
});
worker.on("error", (err) => {
console.error(err);
});
worker.on("exit", (code) => {
if (code !== 0) {
new Error(`Worker stopped with exit code ${code}`);
}
resolve();
});
});
})
);
return Array.from(cityMap)
.map(
([city_name, status]) =>
`${city_name}=${status.min};${status.max};${~~(
status.total / status.count
)}(${status.total}/${status.count})\n`
)
.join("");
}
async function main() {
const expectOutput = String(fs.readFileSync(OUTPUT_PATH));
const timer = new Timer();
const got = await solution();
console.log(`Elapsed: ${timer.elapsedAsMilliSeconds()}ms`);
if (expectOutput === got) {
console.log("Test passed");
} else {
console.log("Test failed");
console.log("Expected:");
console.log(expectOutput);
console.log("Actual:");
console.log(got);
}
}
main();