Skip to content

Commit

Permalink
feat: add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
trungnotchung committed Nov 5, 2024
1 parent c476658 commit 79c2894
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.2.1-4";
export const VERSION = "0.2.1";
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 });
63 changes: 41 additions & 22 deletions pnpm-lock.yaml

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

0 comments on commit 79c2894

Please sign in to comment.