Skip to content

Commit 911cd00

Browse files
committed
refactor: use ExecutionRecorder to collect assert result
It can reduce peak memory usage of wasm module because there are not need to encode assert info to json at the end of exection
1 parent 441b9c4 commit 911cd00

File tree

8 files changed

+126
-52
lines changed

8 files changed

+126
-52
lines changed

assembly/assertCollector.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

assembly/env.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export namespace assertResult {
2+
3+
@external("__unittest_framework_env","addDescription")
4+
export declare function addDescription(description: string): void;
5+
6+
7+
@external("__unittest_framework_env","removeDescription")
8+
export declare function removeDescription(): void;
9+
10+
11+
@external("__unittest_framework_env","collectCheckResult")
12+
export declare function collectCheckResult(
13+
result: bool,
14+
codeInfoIndex: number,
15+
actualValue: string,
16+
expectValue: string,
17+
): void;
18+
}

assembly/expect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { equal, isNull } from "./comparison";
2-
import { assertResult } from "./assertCollector";
2+
import { assertResult } from "./env";
33
import { toJson } from "./formatPrint";
44

55

assembly/implement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertResult } from "./assertCollector";
1+
import { assertResult } from "./env";
22
import { MockFn, mockFunctionStatus } from "./mockInstrument";
33

44
export function describeImpl(

src/core/execute.ts

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,13 @@ import { basename } from "node:path";
55
import { instantiate, Imports as ASImports } from "@assemblyscript/loader";
66
import { AssertResult } from "../assertResult.js";
77
import { Imports, ImportsArgument } from "../index.js";
8-
import { AssertFailMessage, AssertMessage, IAssertResult, InstrumentResult } from "../interface.js";
8+
import { InstrumentResult } from "../interface.js";
99
import { mockInstruFunc, covInstruFunc } from "../utils/import.js";
1010
import { supplyDefaultFunction } from "../utils/index.js";
1111
import { parseImportFunctionInfo } from "../utils/wasmparser.js";
12+
import { ExecutionRecorder } from "./execution_recorder.js";
1213
const readFile = promises.readFile;
1314

14-
class ExecutionRecorder implements IAssertResult {
15-
total: number = 0;
16-
fail: number = 0;
17-
failed_info: AssertFailMessage = {};
18-
currentTestDescriptions: string[] = [];
19-
20-
addDescription(description: string): void {
21-
this.currentTestDescriptions.push(description);
22-
}
23-
removeDescription(): void {
24-
this.currentTestDescriptions.pop();
25-
}
26-
collectCheckResult(result: boolean, codeInfoIndex: number, actualValue: string, expectValue: string): void {
27-
this.total++;
28-
if (!result) {
29-
this.fail++;
30-
const testCaseFullName = this.currentTestDescriptions.join(" - ");
31-
const assertMessage: AssertMessage = [codeInfoIndex.toString(), actualValue, expectValue];
32-
this.failed_info[testCaseFullName] = this.failed_info[testCaseFullName] || [];
33-
this.failed_info[testCaseFullName].push(assertMessage);
34-
}
35-
}
36-
37-
getCollectionFuncSet(arg: ImportsArgument): Record<string, Record<string, unknown>> {
38-
const exports = arg.exports!;
39-
return {
40-
__unittest_framework_env: {
41-
addDescription: (description: number): void => {
42-
this.addDescription(exports.__getString(description));
43-
},
44-
removeDescription: (): void => {
45-
this.removeDescription();
46-
},
47-
collectCheckResult: (result: number, codeInfoIndex: number, actualValue: number, expectValue: number): void => {
48-
this.collectCheckResult(
49-
result != 0,
50-
codeInfoIndex,
51-
exports.__getString(actualValue),
52-
exports.__getString(expectValue)
53-
);
54-
},
55-
},
56-
};
57-
}
58-
}
59-
6015
async function nodeExecutor(wasm: string, outFolder: string, imports: Imports): Promise<ExecutionRecorder> {
6116
const wasi = new WASI({
6217
args: ["node", basename(wasm)],

src/core/execution_recorder.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { ImportsArgument } from "../index.js";
2+
import { AssertFailMessage, AssertMessage, IAssertResult } from "../interface.js";
3+
4+
export class ExecutionRecorder implements IAssertResult {
5+
total: number = 0;
6+
fail: number = 0;
7+
failed_info: AssertFailMessage = {};
8+
_currentTestDescriptions: string[] = [];
9+
10+
_addDescription(description: string): void {
11+
this._currentTestDescriptions.push(description);
12+
}
13+
_removeDescription(): void {
14+
this._currentTestDescriptions.pop();
15+
}
16+
collectCheckResult(result: boolean, codeInfoIndex: number, actualValue: string, expectValue: string): void {
17+
this.total++;
18+
if (!result) {
19+
this.fail++;
20+
const testCaseFullName = this._currentTestDescriptions.join(" - ");
21+
const assertMessage: AssertMessage = [codeInfoIndex.toString(), actualValue, expectValue];
22+
this.failed_info[testCaseFullName] = this.failed_info[testCaseFullName] || [];
23+
this.failed_info[testCaseFullName].push(assertMessage);
24+
}
25+
}
26+
27+
getCollectionFuncSet(arg: ImportsArgument): Record<string, Record<string, unknown>> {
28+
return {
29+
__unittest_framework_env: {
30+
addDescription: (description: number): void => {
31+
this._addDescription(arg.exports!.__getString(description));
32+
},
33+
removeDescription: (): void => {
34+
this._removeDescription();
35+
},
36+
collectCheckResult: (result: number, codeInfoIndex: number, actualValue: number, expectValue: number): void => {
37+
this.collectCheckResult(
38+
result != 0,
39+
codeInfoIndex,
40+
arg.exports!.__getString(actualValue),
41+
arg.exports!.__getString(expectValue)
42+
);
43+
},
44+
},
45+
};
46+
}
47+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { ExecutionRecorder } from "../../../../src/core/execution_recorder.js";
2+
3+
describe("execution recorder", () => {
4+
describe("description", () => {
5+
test("add single description", () => {
6+
const recorder = new ExecutionRecorder();
7+
recorder._addDescription("description");
8+
9+
recorder.collectCheckResult(false, 0, "", "");
10+
expect(recorder.failed_info).toHaveProperty("description");
11+
});
12+
test("add multiple descriptions", () => {
13+
const recorder = new ExecutionRecorder();
14+
recorder._addDescription("description1");
15+
recorder._addDescription("description2");
16+
17+
recorder.collectCheckResult(false, 0, "", "");
18+
expect(recorder.failed_info).toHaveProperty("description1 - description2");
19+
});
20+
21+
test("remove descriptions", () => {
22+
const recorder = new ExecutionRecorder();
23+
recorder._addDescription("description1");
24+
recorder._addDescription("description2");
25+
recorder._removeDescription();
26+
27+
recorder.collectCheckResult(false, 0, "", "");
28+
expect(recorder.failed_info).toHaveProperty("description1");
29+
});
30+
});
31+
32+
describe("collectCheckResult", () => {
33+
test("collect false result", () => {
34+
const recorder = new ExecutionRecorder();
35+
recorder.collectCheckResult(false, 0, "actual", "expect");
36+
37+
expect(recorder.total).toBe(1);
38+
expect(recorder.fail).toBe(1);
39+
});
40+
test("collect true results", () => {
41+
const recorder = new ExecutionRecorder();
42+
recorder.collectCheckResult(true, 0, "actual1", "expect1");
43+
44+
expect(recorder.total).toBe(1);
45+
expect(recorder.fail).toBe(0);
46+
});
47+
test("collect multiple results", () => {
48+
const recorder = new ExecutionRecorder();
49+
recorder.collectCheckResult(true, 0, "actual1", "expect1");
50+
recorder.collectCheckResult(false, 1, "actual2", "expect2");
51+
52+
expect(recorder.total).toBe(2);
53+
expect(recorder.fail).toBe(1);
54+
});
55+
});
56+
});

tests/ts/test/utils/path.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test("getIncludeFiles", () => {
3131
expect(getIncludeFiles(["src/core"], (s) => s.endsWith(".ts"))).toEqual([
3232
path.normalize("src/core/compile.ts"),
3333
path.normalize("src/core/execute.ts"),
34+
path.normalize("src/core/execution_recorder.ts"),
3435
path.normalize("src/core/instrument.ts"),
3536
path.normalize("src/core/precompile.ts"),
3637
]);

0 commit comments

Comments
 (0)