Skip to content

Commit

Permalink
Adding benchmark support.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Apr 17, 2024
1 parent 16c6076 commit 018c105
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ NODE_VERSION=16.x
PROGRAMS=["mpl-project-name"]
RUST_VERSION=1.70.0
SOLANA_VERSION=1.16.18
COMMIT_USER_NAME=github-actions
COMMIT_USER_EMAIL=github-actions@github.com
69 changes: 69 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Benchmark
on:
workflow_call:

permissions:
# deployments permission to deploy GitHub pages website
deployments: write
# contents permission to update benchmark contents in gh-pages branch
contents: write

env:
CACHE: true

jobs:
benchmark:
name: Performance regression check
runs-on: ubuntu-latest
steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Load environment variables
run: cat .github/.env >> $GITHUB_ENV

- name: Start validator
uses: metaplex-foundation/actions/start-validator@v1
with:
node: 18.x
solana: ${{ env.SOLANA_VERSION }}
cache: ${{ env.CACHE }}

- name: Install dependencies
uses: metaplex-foundation/actions/install-node-dependencies@v1
with:
folder: ./clients/js
cache: ${{ env.CACHE }}
key: clients-js

- name: Build
working-directory: ./clients/js
run: pnpm build

- name: Test
working-directory: ./clients/js
run: pnpm bench

# Download previous benchmark result from cache (if exists)
- name: Download previous benchmark data
uses: actions/cache@v4
with:
path: ./cache
key: ${{ runner.os }}-benchmark

# Run `github-action-benchmark` action
- name: Store benchmark result
uses: benchmark-action/github-action-benchmark@v1
with:
# What benchmark tool the output.json came from
tool: "customSmallerIsBetter"
# Where the output from the benchmark tool is stored
output-file-path: ./clients/js/output.json
# Where the previous data file is stored
# external-data-json-path: ./cache/benchmark-data.json
# Workflow will fail when an alert happens
fail-on-alert: true
# Access token to deploy GitHub Pages branch
github-token: ${{ secrets.GITHUB_TOKEN }}
# Push and deploy GitHub pages branch automatically
auto-push: true
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ jobs:
uses: ./.github/workflows/test-js-client.yml
secrets: inherit

benchmark:
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
name: Benchmark
needs: generate_clients
uses: ./.github/workflows/benchmark.yml
secrets: inherit

build_rust_client:
if: needs.changes.outputs.rust_client == 'true'
name: Rust Client
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dist
.amman
.crates
.bin
clients/js/output.json
12 changes: 12 additions & 0 deletions clients/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ A Umi-compatible JavaScript library for the project.

You can learn more about this library's API by reading its generated [TypeDoc documentation](https://mpl-project-name-js-docs.vercel.app).

## Setting up Benchmarks
The GitHub workflow will automatically run benchmarks on pushes to the `main` branch, however it needs a gh-pages branch to deploy the hosted graph website to. Run the commands below to setup the gh-pages branch.
```sh
git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty -m "Initializing gh-pages branch"
git push origin gh-pages
git checkout master
```

Afterwards, the webpage will be available at `https://<GITHUB_ORG_OR_USERNAME>.github.io/<REPO_NAME>/dev/bench/`

## Contributing

Check out the [Contributing Guide](./CONTRIBUTING.md) the learn more about how to contribute to this library.
7 changes: 7 additions & 0 deletions clients/js/bench/_setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable import/no-extraneous-dependencies */
import { createUmi as basecreateUmi } from '@metaplex-foundation/umi-bundle-tests';
import {
mplProjectName,
} from '../src';

export const createUmi = async () => (await basecreateUmi()).use(mplProjectName());
45 changes: 45 additions & 0 deletions clients/js/bench/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { generateSigner } from "@metaplex-foundation/umi";
// eslint-disable-next-line import/no-extraneous-dependencies
import test from "ava";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { create } from '../src';
import { createUmi } from "./_setup";

test('create a new account', async (t) => {
// Given an Umi instance and a new signer.
const umi = await createUmi();
const address = generateSigner(umi);

// When we create a new account.
const tx = await create(umi, { address, arg1: 1, arg2: 2 }).sendAndConfirm(umi);

const compute = Number((await umi.rpc.getTransaction(tx.signature))?.meta.computeUnitsConsumed);
const account = await umi.rpc.getAccount(address.publicKey);
const space = account.exists ? account.data.length : 0;

const cuResult = {
name: `CU: ${t.title}`,
unit: "Compute Units",
value: compute,
}

const spaceResult = {
name: `Space: ${t.title}`,
unit: "Bytes",
value: space,
}

// Read the results array from output.json
let output = [];
if (existsSync("./output.json")) {
output = JSON.parse(readFileSync("./output.json", 'utf-8'));
}

// Push the result to the array
output.push(cuResult);
output.push(spaceResult);
// Write the array to output.json
writeFileSync("./output.json", JSON.stringify(output, null, 2));

t.pass();
});
4 changes: 3 additions & 1 deletion clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"build": "rimraf dist && tsc -p tsconfig.json",
"build:docs": "typedoc",
"test": "ava",
"bench": "rm -f output.json && ava ./dist/bench/*.js --serial --no-worker-threads",
"lint": "eslint --ext js,ts,tsx src",
"lint:fix": "eslint --fix --ext js,ts,tsx src",
"format": "prettier --check src test",
Expand Down Expand Up @@ -56,7 +57,8 @@
"compile": false,
"rewritePaths": {
"src/": "dist/src/",
"test/": "dist/test/"
"test/": "dist/test/",
"bench/": "dist/bench/"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion clients/js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"skipLibCheck": true,
"useUnknownInCatchVariables": false
},
"include": ["src", "test"],
"include": ["src", "test", "bench"],
"exclude": ["node_modules", "dist", "build", "lib"]
}

0 comments on commit 018c105

Please sign in to comment.