Skip to content

Commit

Permalink
fix(test): use number instead of bigint in benchmarks for gas (#1804
Browse files Browse the repository at this point in the history
)
  • Loading branch information
i582 authored Feb 13, 2025
1 parent fdd7b62 commit e525f67
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/test/benchmarks/__snapshots__/benchmarks.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ exports[`benchmarks benchmark cells creation: gas used emptySlice 1`] = `935n`;

exports[`benchmarks benchmark functions: code size 1`] = `211`;

exports[`benchmarks benchmark functions: gas used 1`] = `2769n`;
exports[`benchmarks benchmark functions: gas used 1`] = `2769`;

exports[`benchmarks benchmark readFwdFee: code size 1`] = `157`;

exports[`benchmarks benchmark readFwdFee: gas used 1`] = `2799n`;
exports[`benchmarks benchmark readFwdFee: gas used 1`] = `2799`;
38 changes: 19 additions & 19 deletions src/test/benchmarks/benchmarks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import "@ton/test-utils";
import { CellsCreation } from "./contracts/output/cells_CellsCreation";
import { getUsedGas } from "./util";

function measureGas(txs: BlockchainTransaction[]) {
return (
(txs[1]!.description as TransactionDescriptionGeneric)
.computePhase as TransactionComputeVm
).gasUsed;
function measureGas(txs: BlockchainTransaction[]): number {
return Number(
(
(txs[1]!.description as TransactionDescriptionGeneric)
.computePhase as TransactionComputeVm
).gasUsed,
);
}

describe("benchmarks", () => {
Expand Down Expand Up @@ -76,7 +78,7 @@ describe("benchmarks", () => {
async function hashStringSmall(
sha256: SandboxContract<Sha256Small>,
s: string,
): Promise<bigint> {
): Promise<number> {
const result = await sha256.send(
treasure.getSender(),
{ value: toNano(1) },
Expand All @@ -89,7 +91,7 @@ describe("benchmarks", () => {
async function hashStringBig(
sha256: SandboxContract<Sha256Big>,
s: string,
): Promise<bigint> {
): Promise<number> {
const result = await sha256.send(
treasure.getSender(),
{ value: toNano(1) },
Expand All @@ -102,7 +104,7 @@ describe("benchmarks", () => {
async function hashStringAsSLice(
sha256: SandboxContract<Sha256AsSlice>,
s: string,
): Promise<bigint> {
): Promise<number> {
const result = await sha256.send(
treasure.getSender(),
{ value: toNano(1) },
Expand Down Expand Up @@ -137,33 +139,31 @@ describe("benchmarks", () => {
await hashStringSmall(sha256Small, "hello world");
await hashStringAsSLice(sha256AsSlice, "hello world");

expect(await hashStringBig(sha256Big, "hello world")).toEqual(3039n);
expect(await hashStringSmall(sha256Small, "hello world")).toEqual(
2516n,
);
expect(await hashStringBig(sha256Big, "hello world")).toEqual(3039);
expect(await hashStringSmall(sha256Small, "hello world")).toEqual(2516);
expect(await hashStringAsSLice(sha256AsSlice, "hello world")).toEqual(
2516n,
2516,
);

expect(await hashStringBig(sha256Big, "hello world".repeat(5))).toEqual(
3040n,
3040,
);
expect(
await hashStringSmall(sha256Small, "hello world".repeat(5)),
).toEqual(2516n);
).toEqual(2516);
expect(
await hashStringAsSLice(sha256AsSlice, "hello world".repeat(5)),
).toEqual(2516n);
).toEqual(2516);

expect(
await hashStringBig(sha256Big, "hello world".repeat(10)),
).toEqual(3042n);
).toEqual(3042);
expect(
await hashStringSmall(sha256Small, "hello world".repeat(10)),
).toEqual(2516n);
).toEqual(2516);
expect(
await hashStringAsSLice(sha256AsSlice, "hello world".repeat(10)),
).toEqual(2516n);
).toEqual(2516);
});

it("benchmark cells creation", async () => {
Expand Down
16 changes: 8 additions & 8 deletions src/test/benchmarks/jetton/jetton.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ import benchmarkResults from "./results.json";

type BenchmarkResult = {
label: string;
transfer: bigint;
burn: bigint;
discovery: bigint;
transfer: number;
burn: number;
discovery: number;
};

const results: BenchmarkResult[] = benchmarkResults.results.map((result) => ({
label: result.label,
transfer: BigInt(result.transfer),
burn: BigInt(result.burn),
discovery: BigInt(result.discovery),
transfer: Number(result.transfer),
burn: Number(result.burn),
discovery: Number(result.discovery),
}));

type MetricKey = "transfer" | "burn" | "discovery";
const METRICS: readonly MetricKey[] = ["transfer", "burn", "discovery"];

function calculateChange(prev: bigint, curr: bigint): string {
const change = ((Number(curr - prev) / Number(prev)) * 100).toFixed(2);
function calculateChange(prev: number, curr: number): string {
const change = (((curr - prev) / prev) * 100).toFixed(2);
const number = parseFloat(change);
if (number === 0) {
return chalk.gray(`same`);
Expand Down
6 changes: 3 additions & 3 deletions src/test/benchmarks/util.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { SendMessageResult } from "@ton/sandbox/dist/blockchain/Blockchain";

export function getUsedGas(sendEnough: SendMessageResult) {
export function getUsedGas(sendEnough: SendMessageResult): number {
return sendEnough.transactions
.slice(1)
.map((t) =>
t.description.type === "generic" &&
t.description.computePhase.type === "vm"
? t.description.computePhase.gasUsed
: 0n,
? Number(t.description.computePhase.gasUsed)
: 0,
)
.reduceRight((prev, cur) => prev + cur);
}

0 comments on commit e525f67

Please sign in to comment.