Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
trungnotchung committed Jan 15, 2025
1 parent 24682d8 commit 0f2db45
Showing 1 changed file with 135 additions and 3 deletions.
138 changes: 135 additions & 3 deletions packages/object/tests/hashgraph.bench.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Benchmark from "benchmark";
import { AddWinsSet } from "../../blueprints/src/AddWinsSet/index.js";
import { DRPObject } from "../src/index.js";
import { ConflictResolvingMap } from "@topology-foundation/blueprints/src/index.js";

function benchmarkForAddWinSet(
name: string,
Expand Down Expand Up @@ -41,7 +42,6 @@ function benchmarkForAddWinSet(
}
});
}

const suite = new Benchmark.Suite();

benchmarkForAddWinSet("Create HashGraph with 1000 vertices", 1, 1000, false);
Expand All @@ -53,11 +53,143 @@ benchmarkForAddWinSet(
true,
);

suite.add("Create a HashGraph with 1000 vertices for update wins map", () => {
const object: DRPObject = new DRPObject(
"peer1",
new ConflictResolvingMap<number, number>(),
);
const drp = object.drp as ConflictResolvingMap<number, number>;
for (let i = 0; i < 1000; ++i) {
drp.update(i, i);
}
});

suite.add(
"Create a HashGraph with 1000 vertices for update wins map and read",
() => {
const object: DRPObject = new DRPObject(
"peer1",
new ConflictResolvingMap<number, number>(),
);
const drp = object.drp as ConflictResolvingMap<number, number>;
for (let i = 0; i < 1000; ++i) {
drp.update(i, i);
}

for (let i = 0; i < 1000; ++i) {
drp.query_get(i);
}
},
);

suite.add(
"Create a HashGraph with 1000 vertices for update wins map and update",
() => {
const object: DRPObject = new DRPObject(
"peer1",
new ConflictResolvingMap<number, number>(),
);
const drp = object.drp as ConflictResolvingMap<number, number>;
for (let i = 0; i < 1000; ++i) {
drp.update(i, i);
}

for (let i = 0; i < 1000; ++i) {
drp.update(i, i + 1);
}
},
);

suite.add(
"Create a HashGraph with 1000 vertices for update wins map and remove",
() => {
const object: DRPObject = new DRPObject(
"peer1",
new ConflictResolvingMap<number, number>(),
);
const drp = object.drp as ConflictResolvingMap<number, number>;
for (let i = 0; i < 1000; ++i) {
drp.update(i, i);
}

for (let i = 0; i < 1000; ++i) {
drp.remove(i);
}
},
);

suite.add("Create a HashGraph with 1000 vertices for update wins map", () => {
const object: DRPObject = new DRPObject(
"peer1",
new ConflictResolvingMap<number, number>(),
);
const drp = object.drp as ConflictResolvingMap<number, number>;
for (let i = 0; i < 1000; ++i) {
if (i % 3 === 0) {
drp.update(i, i);
} else if (i % 3 === 1) {
if ((i % 3) % 2 === 0) {
drp.update(i, i);
} else {
drp.remove(i);
}
} else {
if ((i % 3) % 2 === 0) {
drp.remove(i - 1);
} else {
drp.update(i - 1, i - 1);
}
}
}
});

suite.add(
"Create 2 HashGraphs with 1000 vertices each for update wins map and merge",
() => {
function initialize(drp: ConflictResolvingMap<number, number>) {
for (let i = 0; i < 1000; ++i) {
if (i % 3 === 0) {
drp.update(i, i);
} else if (i % 3 === 1) {
if ((i % 3) % 2 === 0) {
drp.update(i, i);
} else {
drp.remove(i);
}
} else {
if ((i % 3) % 2 === 0) {
drp.remove(i - 1);
} else {
drp.update(i - 1, i - 1);
}
}
}
}

const object1: DRPObject = new DRPObject(
"peer1",
new ConflictResolvingMap<number, number>(),
);
const drp1 = object1.drp as ConflictResolvingMap<number, number>;
initialize(drp1);

const object2: DRPObject = new DRPObject(
"peer2",
new ConflictResolvingMap<number, number>(),
);
const drp2 = object2.drp as ConflictResolvingMap<number, number>;
initialize(drp2);

object1.merge(object2.hashGraph.getAllVertices());
object2.merge(object1.hashGraph.getAllVertices());
},
);

suite
.on("cycle", (event) => {
.on("cycle", (event: Benchmark.Event) => {
console.log(String(event.target));
})
.on("complete", function () {
.on("complete", function (this: Benchmark.Suite) {
console.log(`Fastest is ${this.filter("fastest").map("name")}`);
})
.run({ async: true });

0 comments on commit 0f2db45

Please sign in to comment.