Skip to content

Commit ddbd19c

Browse files
committed
feat: include the wasm file size scripts for generating a markdown table
1 parent 0d25d1a commit ddbd19c

19 files changed

+1033
-417
lines changed

Diff for: benchmark/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
build
2-
file-sizes.json
2+
file-sizes.json

Diff for: benchmark/__tests__/results-store.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
const resultsFilePath = path.resolve("temp-all-test-results.json");
5+
6+
/**
7+
* Reads existing test results from a JSON file if it exists.
8+
*
9+
* This function checks if the file specified by `resultsFilePath` exists.
10+
* If the file is found, it reads the file's content and parses it as JSON.
11+
* If the file does not exist, it returns an empty object.
12+
*
13+
* @returns {Object} The parsed JSON object from the file, or an empty object if the file does not exist.
14+
*/
15+
function readExistingResults() {
16+
if (fs.existsSync(resultsFilePath)) {
17+
try {
18+
const fileContent = fs.readFileSync(resultsFilePath, "utf-8");
19+
return JSON.parse(fileContent);
20+
} catch (error) {
21+
console.error("Failed to read or parse results file:", error);
22+
return {};
23+
}
24+
}
25+
return {};
26+
}
27+
28+
/**
29+
* Function to add test results to the report if the GENERATE_REPORT environment variable is set to "true"
30+
* @param {string} testName - The name of the test.
31+
* @param {Object} result - The test result object.
32+
*/
33+
export function addTestResults(testName, result) {
34+
// Check if we need to generate a report
35+
if (process.env.GENERATE_REPORT === "true") {
36+
// Create a temporary object for the new test results
37+
const tempResults = {
38+
[testName]: result,
39+
};
40+
41+
// Read existing results from the file
42+
const existingResults = readExistingResults();
43+
44+
// Combine existing results with new test results
45+
const combinedResults = {
46+
...existingResults,
47+
...tempResults,
48+
};
49+
50+
try {
51+
// Write the combined results to the file
52+
fs.writeFileSync(
53+
resultsFilePath,
54+
JSON.stringify(combinedResults, null, 2)
55+
);
56+
} catch (error) {
57+
console.error("Failed to write results to file:", error);
58+
}
59+
}
60+
}

Diff for: benchmark/__tests__/test-deploy-contract.ava.js

+16-80
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { Worker } from "near-workspaces";
22
import test from "ava";
3-
import {
4-
formatGas,
5-
gasBreakdown,
6-
logGasBreakdown,
7-
logGasDetail,
8-
} from "./util.js";
3+
import { generateGasObject, logTestResults } from "./util.js";
4+
import { addTestResults } from "./results-store.js";
95

106
test.before(async (t) => {
117
// Init the worker and start a Sandbox server
@@ -47,51 +43,21 @@ test("JS promise batch deploy contract and call", async (t) => {
4743
let r = await bob.callRaw(callerContract, "deploy_contract", "", {
4844
gas: "300 Tgas",
4945
});
50-
// console.log(JSON.stringify(r, null, 2));
46+
5147
let deployed = callerContract.getSubAccount("a");
48+
5249
t.deepEqual(JSON.parse(Buffer.from(r.result.status.SuccessValue, "base64")), {
5350
currentAccountId: deployed.accountId,
5451
signerAccountId: bob.accountId,
5552
predecessorAccountId: callerContract.accountId,
5653
input: "abc",
5754
});
5855

59-
t.log(
60-
"Gas used to convert transaction to receipt: ",
61-
formatGas(r.result.transaction_outcome.outcome.gas_burnt)
62-
);
63-
t.log(
64-
"Gas used to execute the receipt (actual contract call): ",
65-
formatGas(r.result.receipts_outcome[0].outcome.gas_burnt)
66-
);
67-
let map = gasBreakdown(r.result.receipts_outcome[0].outcome);
68-
logGasBreakdown(map, t);
69-
t.log(
70-
"Gas used to execute the cross contract call: ",
71-
formatGas(r.result.receipts_outcome[1].outcome.gas_burnt)
72-
);
73-
map = gasBreakdown(r.result.receipts_outcome[1].outcome);
74-
logGasBreakdown(map, t);
75-
t.log(
76-
"Gas used to refund unused gas for cross contract call: ",
77-
formatGas(r.result.receipts_outcome[2].outcome.gas_burnt)
78-
);
79-
t.log(
80-
"Gas used to refund unused gas: ",
81-
// TODO: fix after near-workspaces is updated
82-
formatGas(r.result.receipts_outcome[3]?.outcome.gas_burnt || 0)
83-
);
84-
t.log(
85-
"Total gas used: ",
86-
formatGas(
87-
r.result.transaction_outcome.outcome.gas_burnt +
88-
r.result.receipts_outcome[0].outcome.gas_burnt +
89-
r.result.receipts_outcome[1].outcome.gas_burnt +
90-
r.result.receipts_outcome[2].outcome.gas_burnt +
91-
// TODO: fix after near-workspaces is updated
92-
(r.result.receipts_outcome[3]?.outcome.gas_burnt || 0)
93-
)
94-
);
56+
logTestResults(r);
57+
58+
const gasObject = generateGasObject(r);
59+
60+
addTestResults("JS_promise_batch_deploy_contract_and_call", gasObject);
9561
});
9662

9763
test("RS promise batch deploy contract and call", async (t) => {
@@ -100,49 +66,19 @@ test("RS promise batch deploy contract and call", async (t) => {
10066
let r = await bob.callRaw(callerContractRs, "deploy_contract", "", {
10167
gas: "300 Tgas",
10268
});
103-
// console.log(JSON.stringify(r, null, 2));
69+
10470
let deployed = callerContractRs.getSubAccount("a");
71+
10572
t.deepEqual(JSON.parse(Buffer.from(r.result.status.SuccessValue, "base64")), {
10673
currentAccountId: deployed.accountId,
10774
signerAccountId: bob.accountId,
10875
predecessorAccountId: callerContractRs.accountId,
10976
input: "abc",
11077
});
11178

112-
t.log(
113-
"Gas used to convert transaction to receipt: ",
114-
formatGas(r.result.transaction_outcome.outcome.gas_burnt)
115-
);
116-
t.log(
117-
"Gas used to execute the receipt (actual contract call): ",
118-
formatGas(r.result.receipts_outcome[0].outcome.gas_burnt)
119-
);
120-
let map = gasBreakdown(r.result.receipts_outcome[0].outcome);
121-
logGasBreakdown(map, t);
122-
t.log(
123-
"Gas used to execute the cross contract call: ",
124-
formatGas(r.result.receipts_outcome[1].outcome.gas_burnt)
125-
);
126-
map = gasBreakdown(r.result.receipts_outcome[1].outcome);
127-
logGasBreakdown(map, t);
128-
t.log(
129-
"Gas used to refund unused gas for cross contract call: ",
130-
formatGas(r.result.receipts_outcome[2].outcome.gas_burnt)
131-
);
132-
t.log(
133-
"Gas used to refund unused gas: ",
134-
// TODO: fix after near-workspaces is updated
135-
formatGas(r.result.receipts_outcome[3]?.outcome.gas_burnt || 0)
136-
);
137-
t.log(
138-
"Total gas used: ",
139-
formatGas(
140-
r.result.transaction_outcome.outcome.gas_burnt +
141-
r.result.receipts_outcome[0].outcome.gas_burnt +
142-
r.result.receipts_outcome[1].outcome.gas_burnt +
143-
r.result.receipts_outcome[2].outcome.gas_burnt +
144-
// TODO: fix after near-workspaces is updated
145-
(r.result.receipts_outcome[3]?.outcome.gas_burnt || 0)
146-
)
147-
);
79+
logTestResults(r);
80+
81+
const gasObject = generateGasObject(r);
82+
83+
addTestResults("RS_promise_batch_deploy_contract_and_call", gasObject);
14884
});

Diff for: benchmark/__tests__/test-expensive-calc.ava.js

+42-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Worker } from "near-workspaces";
22
import test from "ava";
3-
import { logGasDetail } from "./util.js";
3+
import { generateGasObject, logTestResults } from "./util.js";
4+
import { addTestResults } from "./results-store.js";
45

56
test.before(async (t) => {
67
// Init the worker and start a Sandbox server
78
const worker = await Worker.init();
89

9-
// Prepare sandbox for tests, create accounts, deploy contracts, etx.
10+
// Prepare sandbox for tests, create accounts, deploy contracts, etc.
1011
const root = worker.rootAccount;
1112

1213
// Deploy the test contract.
@@ -35,14 +36,25 @@ test("JS expensive contract, iterate 100 times", async (t) => {
3536
let r = await bob.callRaw(expensiveContract, "expensive", { n: 100 });
3637

3738
t.is(r.result.status.SuccessValue, "LTUw");
38-
logGasDetail(r, t);
39+
40+
logTestResults(r);
41+
42+
const gasObject = generateGasObject(r, true);
43+
44+
addTestResults("JS_expensive_contract_100_times", gasObject);
3945
});
4046

4147
test("RS expensive contract. iterate 100 times", async (t) => {
4248
const { bob, expensiveContractRs } = t.context.accounts;
4349
let r = await bob.callRaw(expensiveContractRs, "expensive", { n: 100 });
50+
4451
t.is(r.result.status.SuccessValue, "LTUw");
45-
logGasDetail(r, t);
52+
53+
logTestResults(r);
54+
55+
const gasObject = generateGasObject(r, true);
56+
57+
addTestResults("RS_expensive_contract_100_times", gasObject);
4658
});
4759

4860
test("JS expensive contract, iterate 10000 times", async (t) => {
@@ -55,14 +67,25 @@ test("JS expensive contract, iterate 10000 times", async (t) => {
5567
);
5668

5769
t.is(r.result.status.SuccessValue, "LTUwMDA=");
58-
logGasDetail(r, t);
70+
71+
logTestResults(r);
72+
73+
const gasObject = generateGasObject(r, true);
74+
75+
addTestResults("JS_expensive_contract_10000_times", gasObject);
5976
});
6077

6178
test("RS expensive contract. iterate 10000 times", async (t) => {
6279
const { bob, expensiveContractRs } = t.context.accounts;
6380
let r = await bob.callRaw(expensiveContractRs, "expensive", { n: 10000 });
81+
6482
t.is(r.result.status.SuccessValue, "LTUwMDA=");
65-
logGasDetail(r, t);
83+
84+
logTestResults(r);
85+
86+
const gasObject = generateGasObject(r, true);
87+
88+
addTestResults("RS_expensive_contract_10000_times", gasObject);
6689
});
6790

6891
test("JS expensive contract, iterate 20000 times", async (t) => {
@@ -75,12 +98,23 @@ test("JS expensive contract, iterate 20000 times", async (t) => {
7598
);
7699

77100
t.is(r.result.status.SuccessValue, "LTEwMDAw");
78-
logGasDetail(r, t);
101+
102+
logTestResults(r);
103+
104+
const gasObject = generateGasObject(r, true);
105+
106+
addTestResults("JS_expensive_contract_20000_times", gasObject);
79107
});
80108

81109
test("RS expensive contract. iterate 20000 times", async (t) => {
82110
const { bob, expensiveContractRs } = t.context.accounts;
83111
let r = await bob.callRaw(expensiveContractRs, "expensive", { n: 20000 });
112+
84113
t.is(r.result.status.SuccessValue, "LTEwMDAw");
85-
logGasDetail(r, t);
114+
115+
logTestResults(r);
116+
117+
const gasObject = generateGasObject(r, true);
118+
119+
addTestResults("RS_expensive_contract_20000_times", gasObject);
86120
});

Diff for: benchmark/__tests__/test-highlevel-collection.ava.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Worker } from "near-workspaces";
22
import test from "ava";
3-
import { logGasDetail } from "./util.js";
3+
import { generateGasObject, logTestResults } from "./util.js";
4+
import { addTestResults } from "./results-store.js";
45

56
test.before(async (t) => {
67
// Init the worker and start a Sandbox server
@@ -50,7 +51,11 @@ test("JS highlevel collection contract", async (t) => {
5051
});
5152

5253
t.is(r.result.status.SuccessValue, "");
53-
logGasDetail(r, t);
54+
logTestResults(r);
55+
56+
const gasObject = generateGasObject(r, true);
57+
58+
addTestResults("JS_highlevel_collection_contract", gasObject);
5459
});
5560

5661
test("RS highlevel collection contract", async (t) => {
@@ -68,5 +73,9 @@ test("RS highlevel collection contract", async (t) => {
6873
value: "d".repeat(100),
6974
});
7075
t.is(r.result.status.SuccessValue, "");
71-
logGasDetail(r, t);
76+
logTestResults(r);
77+
78+
const gasObject = generateGasObject(r, true);
79+
80+
addTestResults("RS_highlevel_collection_contract", gasObject);
7281
});

Diff for: benchmark/__tests__/test-highlevel-minimal.ava.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Worker } from "near-workspaces";
22
import test from "ava";
3-
import { logGasDetail } from "./util.js";
3+
import { generateGasObject, logTestResults } from "./util.js";
4+
import { addTestResults } from "./results-store.js";
45

56
test.before(async (t) => {
67
// Init the worker and start a Sandbox server
@@ -39,13 +40,21 @@ test("JS highlevel minimal contract", async (t) => {
3940
let r = await bob.callRaw(highlevelContract, "empty", "");
4041

4142
t.is(r.result.status.SuccessValue, "");
42-
logGasDetail(r, t);
43+
logTestResults(r);
44+
45+
const gasObject = generateGasObject(r, true);
46+
47+
addTestResults("JS_highlevel_minimal_contract", gasObject);
4348
});
4449

4550
test("RS highlevel minimal contract", async (t) => {
4651
const { bob, highlevelContractRs } = t.context.accounts;
4752
let r = await bob.callRaw(highlevelContractRs, "empty", "");
4853

4954
t.is(r.result.status.SuccessValue, "");
50-
logGasDetail(r, t);
55+
logTestResults(r);
56+
57+
const gasObject = generateGasObject(r, true);
58+
59+
addTestResults("RS_highlevel_minimal_contract", gasObject);
5160
});

0 commit comments

Comments
 (0)