Skip to content

Commit

Permalink
chore: run benchmarking utility in devnet environment (#3166)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad authored Oct 1, 2024
1 parent 115a1f1 commit 3015555
Show file tree
Hide file tree
Showing 14 changed files with 632 additions and 421 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-carpets-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@internal/benchmarks": patch
---

chore: run benchmarking utility in devnet environment
27 changes: 27 additions & 0 deletions .github/workflows/bench-devnet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: "Bench Devnet"

on:
push:
branches:
- release/*

jobs:
benchmarks:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: CI Setup
uses: ./.github/actions/test-setup

- name: Pretest
run: pnpm pretest

- name: Run Node benchmarks
uses: CodSpeedHQ/action@v3
with:
run: pnpm bench:node
token: ${{ secrets.CODSPEED_TOKEN }}
env:
DEVNET_WALLET_PVT_KEY: ${{ secrets.DEVNET_WALLET_PVT_KEY }}
1 change: 1 addition & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Benchmarks

on:
pull_request:
push:
Expand Down
3 changes: 2 additions & 1 deletion internal/benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"license": "Apache-2.0",
"dependencies": {
"fuels": "workspace:*"
"fuels": "workspace:*",
"@internal/utils": "workspace:*"
},
"version": "1.0.3"
}
17 changes: 17 additions & 0 deletions internal/benchmarks/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable import/no-extraneous-dependencies */

import { bench } from 'vitest';

export const isDevnet = process.env.DEVNET_WALLET_PVT_KEY !== undefined;

const iterations = isDevnet ? 1 : 10;

export const runBenchmark = (name: string, benchmarkFn: () => Promise<void>) => {
bench(
isDevnet ? name : `${name} (x${iterations} times)`,
async () => {
await benchmarkFn();
},
{ iterations }
);
};
88 changes: 61 additions & 27 deletions internal/benchmarks/src/contract-interaction.bench.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* eslint-disable import/no-extraneous-dependencies */

import type { WalletUnlocked } from 'fuels';
import { bn } from 'fuels';
import { DEVNET_NETWORK_URL } from '@internal/utils';
import { WalletUnlocked, bn, Provider } from 'fuels';
import { launchTestNode, TestAssetId } from 'fuels/test-utils';
import { bench } from 'vitest';

import type { CounterContract, CallTestContract } from '../test/typegen/contracts';
import { CounterContractFactory, CallTestContractFactory } from '../test/typegen/contracts';
import {
CounterContractFactory,
CallTestContractFactory,
LargeContractFactory,
} from '../test/typegen/contracts';

import { isDevnet, runBenchmark } from './config';

/**
* @group node
* @group browser
Expand All @@ -16,55 +23,82 @@ describe('Contract Interaction Benchmarks', () => {
let callTestContract: CallTestContract;
let wallet: WalletUnlocked;
let cleanup: () => void;
beforeEach(async () => {
const launched = await launchTestNode({
contractsConfigs: [{ factory: CounterContractFactory }, { factory: CallTestContractFactory }],
});

cleanup = launched.cleanup;
contract = launched.contracts[0];
callTestContract = launched.contracts[1];
wallet = launched.wallets[0];
});
const setupTestEnvironment = async () => {
if (isDevnet) {
const provider = await Provider.create(DEVNET_NETWORK_URL);
wallet = new WalletUnlocked(process.env.DEVNET_WALLET_PVT_KEY as string, provider);

const { waitForResult } = await CounterContractFactory.deploy(wallet);
contract = (await waitForResult()).contract;

afterEach(() => {
cleanup();
const { waitForResult: waitForResultCallTestContract } =
await CallTestContractFactory.deploy(wallet);
callTestContract = (await waitForResultCallTestContract()).contract;
} else {
const launched = await launchTestNode({
contractsConfigs: [
{ factory: CounterContractFactory },
{ factory: CallTestContractFactory },
],
});

cleanup = launched.cleanup;
contract = launched.contracts[0];
callTestContract = launched.contracts[1];
wallet = launched.wallets[0];
}
};

beforeAll(setupTestEnvironment);

afterAll(() => {
if (!isDevnet && cleanup) {
cleanup();
}
});

bench('should successfully execute a contract read function', async () => {
runBenchmark('should successfully execute a contract read function', async () => {
const tx = await contract.functions.get_count().call();

const { value } = await tx.waitForResult();

expect(JSON.stringify(value)).toEqual(JSON.stringify(bn(0)));
expect(value).toBeDefined();
});

bench('should successfully execute a contract multi call', async () => {
runBenchmark('should successfully execute a contract multi call', async () => {
const initialValue = 100;
const tx = await contract
.multiCall([contract.functions.increment_counter(100), contract.functions.get_count()])
.multiCall([
contract.functions.increment_counter(initialValue),
contract.functions.get_count(),
])
.call();

const { value } = await tx.waitForResult();

expect(JSON.stringify(value)).toEqual(JSON.stringify([bn(100), bn(100)]));
expect(value).toBeDefined();
});

bench('should successfully write to a contract', async () => {
runBenchmark('should successfully write to a contract', async () => {
const tx = await contract.functions.increment_counter(100).call();
await tx.waitForResult();
});

bench('should successfully execute a contract mint', async () => {
runBenchmark('should successfully execute a contract mint', async () => {
const tx = await callTestContract.functions.mint_coins(TestAssetId.A.value, bn(100)).call();

await tx.waitForResult();
});

bench('should successfully execute a contract deploy', async () => {
runBenchmark('should successfully execute a contract deploy', async () => {
const factory = new CounterContractFactory(wallet);
const { waitForResult } = await factory.deploy();
const { contract: deployedContract } = await waitForResult();
expect(deployedContract).toBeDefined();
});

bench('should successfully execute a contract deploy as blobs', async () => {
const factory = new LargeContractFactory(wallet);
const { waitForResult } = await factory.deployAsBlobTx({
chunkSizeMultiplier: 0.9,
});
const { contract: deployedContract } = await waitForResult();
expect(deployedContract).toBeDefined();
});
});
Loading

0 comments on commit 3015555

Please sign in to comment.