Skip to content

Commit 01dfa5a

Browse files
committed
process in batches
1 parent 1c11770 commit 01dfa5a

File tree

2 files changed

+96
-28
lines changed

2 files changed

+96
-28
lines changed

tests/docstrings_examples/DocTest.res

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ module Node = {
4545
module OS = {
4646
@module("os")
4747
external tmpdir: unit => string = "tmpdir"
48+
49+
@module("os")
50+
external cpus: unit => array<{.}> = "cpus"
4851
}
4952

5053
module Util = {
@@ -280,6 +283,23 @@ let getCodeBlocks = example => {
280283
->Array.join("\n\n")
281284
}
282285

286+
let chunkArray = (array, chunkSize) => {
287+
let result = []
288+
289+
let rec loop = (i: int) => {
290+
if i < Array.length(array) {
291+
Array.push(
292+
result,
293+
Array.slice(array, ~start=i, ~end=Math.Int.min(i + chunkSize, Array.length(array))),
294+
)
295+
loop(i + chunkSize)
296+
}
297+
}
298+
299+
loop(0)
300+
result
301+
}
302+
283303
let main = async () => {
284304
let files = Fs.readdirSync("runtime")
285305

@@ -327,28 +347,55 @@ let main = async () => {
327347
(lhs, rhs)
328348
})
329349

330-
let runtimeErrors =
331-
(await compiled
332-
->Array.filter((({id}, _, _)) => !Array.includes(ignoreRuntimeTests, id))
333-
->Array.map(async ((example, rescriptCode, jsCode)) => {
334-
let nodeTests = await jsCode->runtimeTests
335-
switch nodeTests {
336-
| Ok(_) => None
337-
| Error(error) => Some(example, RuntimeError({rescript: rescriptCode, js: jsCode, error}))
338-
}
350+
let batchSize = OS.cpus()->Array.length
351+
let batches = chunkArray(compiled, batchSize)
352+
353+
let a =
354+
await batches
355+
->Array.map(async t => {
356+
(await t
357+
->Array.filter((({id}, _, _)) => !Array.includes(ignoreRuntimeTests, id))
358+
->Array.map(async ((example, rescriptCode, jsCode)) => {
359+
let nodeTest = await runtimeTests(jsCode)
360+
switch nodeTest {
361+
| Ok(_) => None
362+
| Error(error) => Some(example, RuntimeError({rescript: rescriptCode, js: jsCode, error}))
363+
}
364+
})
365+
->Promise.all)
366+
->Array.filterMap(i =>
367+
switch i {
368+
| Some(i) => Some(i)
369+
| None => None
370+
}
371+
)
339372
})
340-
->Promise.all)
341-
->Array.filterMap(i =>
342-
switch i {
343-
| Some(i) => Some(i)
344-
| None => None
345-
}
346-
)
373+
->Promise.all
347374

348-
let allErros = Array.concat(runtimeErrors, compilationErrors)
375+
let runtimeErrors = Array.flat(a)
376+
377+
// let runtimeErrors =
378+
// (await compiled
379+
// ->Array.filter((({id}, _, _)) => !Array.includes(ignoreRuntimeTests, id))
380+
// ->Array.map(async ((example, rescriptCode, jsCode)) => {
381+
// let nodeTests = await jsCode->runtimeTests
382+
// switch nodeTests {
383+
// | Ok(_) => None
384+
// | Error(error) => Some(example, RuntimeError({rescript: rescriptCode, js: jsCode, error}))
385+
// }
386+
// })
387+
// ->Promise.all)
388+
// ->Array.filterMap(i =>
389+
// switch i {
390+
// | Some(i) => Some(i)
391+
// | None => None
392+
// }
393+
// )
394+
395+
let allErrors = Array.concat(runtimeErrors, compilationErrors)
349396

350397
// Print Errors
351-
let () = allErros->Array.forEach(((example, errors)) => {
398+
let () = allErrors->Array.forEach(((example, errors)) => {
352399
let red = s => `\x1B[1;31m${s}\x1B[0m`
353400
let cyan = s => `\x1b[36m${s}\x1b[0m`
354401
let kind = switch example.kind {
@@ -389,7 +436,7 @@ ${error->indentOutputCode}
389436
Process.stderrWrite(a)
390437
})
391438

392-
let someError = allErros->Array.length > 0
439+
let someError = allErrors->Array.length > 0
393440

394441
someError ? 1 : 0
395442
}

tests/docstrings_examples/DocTest.res.mjs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function extractDocFromFile(file) {
182182
RE_EXN_ID: "Assert_failure",
183183
_1: [
184184
"DocTest.res",
185-
196,
185+
199,
186186
9
187187
],
188188
Error: new Error()
@@ -319,6 +319,23 @@ function getCodeBlocks(example) {
319319
return Belt_Array.reverse(List.toArray(loop(List.fromArray($$Array.reduce(example.docstrings, [], (acc, docstring) => acc.concat(docstring.split("\n")))), /* [] */0))).join("\n\n");
320320
}
321321

322+
function chunkArray(array, chunkSize) {
323+
let result = [];
324+
let loop = _i => {
325+
while (true) {
326+
let i = _i;
327+
if (i >= array.length) {
328+
return;
329+
}
330+
result.push(array.slice(i, Math.min(i + chunkSize | 0, array.length)));
331+
_i = i + chunkSize | 0;
332+
continue;
333+
};
334+
};
335+
loop(0);
336+
return result;
337+
}
338+
322339
async function main() {
323340
let files = Fs.readdirSync("runtime");
324341
let modules = $$Array.reduce(files.filter(f => {
@@ -385,10 +402,12 @@ async function main() {
385402
rhs
386403
];
387404
});
388-
let runtimeErrors = $$Array.filterMap(await Promise.all(match[0].filter(param => !ignoreRuntimeTests.includes(param[0].id)).map(async param => {
405+
let batchSize = Os.cpus().length;
406+
let batches = chunkArray(match[0], batchSize);
407+
let a = await Promise.all(batches.map(async t => $$Array.filterMap(await Promise.all(t.filter(param => !ignoreRuntimeTests.includes(param[0].id)).map(async param => {
389408
let jsCode = param[2];
390-
let nodeTests = await runtimeTests(jsCode);
391-
if (nodeTests.TAG === "Ok") {
409+
let nodeTest = await runtimeTests(jsCode);
410+
if (nodeTest.TAG === "Ok") {
392411
return;
393412
} else {
394413
return [
@@ -397,7 +416,7 @@ async function main() {
397416
TAG: "RuntimeError",
398417
rescript: param[1],
399418
js: jsCode,
400-
error: nodeTests._0
419+
error: nodeTest._0
401420
}
402421
];
403422
}
@@ -406,9 +425,10 @@ async function main() {
406425
return i;
407426
}
408427

409-
});
410-
let allErros = runtimeErrors.concat(match[1]);
411-
allErros.forEach(param => {
428+
})));
429+
let runtimeErrors = a.flat();
430+
let allErrors = runtimeErrors.concat(match[1]);
431+
allErrors.forEach(param => {
412432
let errors = param[1];
413433
let example = param[0];
414434
let cyan = s => "\x1b[36m" + s + "\x1b[0m";
@@ -424,7 +444,7 @@ async function main() {
424444
}
425445
process.stderr.write(a);
426446
});
427-
let someError = allErros.length > 0;
447+
let someError = allErrors.length > 0;
428448
if (someError) {
429449
return 1;
430450
} else {
@@ -453,6 +473,7 @@ export {
453473
extractDocFromFile,
454474
getExamples,
455475
getCodeBlocks,
476+
chunkArray,
456477
main,
457478
exitCode,
458479
}

0 commit comments

Comments
 (0)