Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement benchmarking for code performance #230

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Benchmark.js Example
on:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: write
deployments: write

jobs:
benchmark:
name: Run benchmark
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9
- shell: bash
run: |
pnpm install --no-frozen-lockfile
cd packages/object
pnpm run benchmark

- name: Store benchmark result
uses: benchmark-action/github-action-benchmark@v1
with:
name: Benchmark.js Benchmark
tool: 'benchmarkjs'
output-file-path: packages/object/benchmark-output.txt
github-token: ${{ secrets.GITHUB_TOKEN }}
auto-push: true
# Show alert with commit comment on detecting possible performance regression
alert-threshold: '200%'
comment-on-alert: true
fail-on-alert: true
7 changes: 5 additions & 2 deletions packages/object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
"build": "tsc -b",
"clean": "rm -rf dist/ node_modules/",
"prepack": "tsc -b",
"test": "vitest"
"test": "vitest",
"benchmark": "npx tsx tests/hashgraph.bench.ts | tee benchmark-output.txt"
},
"devDependencies": {
"assemblyscript": "^0.27.29"
"assemblyscript": "^0.27.29",
"benchmark": "^2.1.4",
"tsx": "4.19.1"
},
"dependencies": {
"@bufbuild/protobuf": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/object/tests/causallyrelated.bench.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bench, describe } from "vitest";
import { AddWinsSet } from "../../crdt/src/cros/AddWinsSet/index.js";
import { AddWinsSet } from "../../blueprints/src/AddWinsSet/index.js";
import { type Hash, TopologyObject } from "../src/index.js";

describe("AreCausallyDependent benchmark", async () => {
Expand Down
63 changes: 63 additions & 0 deletions packages/object/tests/hashgraph.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Benchmark from "benchmark";
import { AddWinsSet } from "../../blueprints/src/AddWinsSet/index.js";
import { TopologyObject } from "../src/index.js";

function benchmarkForAddWinSet(
name: string,
numCROs: number,
verticesPerCRO: number,
mergeFn: boolean,
) {
return suite.add(name, () => {
const objects: TopologyObject[] = [];
for (let i = 0; i < numCROs; i++) {
const obj: TopologyObject = new TopologyObject(
`peer${i + 1}`,
new AddWinsSet<number>(),
);
const cro = obj.cro as AddWinsSet<number>;
for (let j = 0; j < verticesPerCRO; j++) {
if (i % 3 === 2) {
cro.add(j);
cro.remove(j);
} else if (i % 3 === 1) {
cro.remove(j);
cro.add(j);
} else {
cro.add(j);
}
}
objects.push(obj);
}

if (mergeFn) {
for (let i = 0; i < objects.length; i++) {
for (let j = 0; j < objects.length; j++) {
if (i !== j) {
objects[i].merge(objects[j].hashGraph.getAllVertices());
}
}
}
}
});
}

const suite = new Benchmark.Suite();

benchmarkForAddWinSet("Create HashGraph with 1000 vertices", 1, 1000, false);

benchmarkForAddWinSet(
"Create 2 CROs (1000 vertices each) and Merge",
2,
1000,
true,
);

suite
.on("cycle", (event) => {
console.log(String(event.target));
})
.on("complete", function () {
console.log(`Fastest is ${this.filter("fastest").map("name")}`);
})
.run({ async: true });
21 changes: 20 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading