-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: benchmark for causallyRealted function
- Loading branch information
Showing
3 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import test from "node:test"; | ||
import { beforeEach, bench, describe } from "vitest"; | ||
import { AddWinsSet } from "../../crdt/src/cros/AddWinsSet/index.js"; | ||
import { | ||
type Hash, | ||
type TopologyObject, | ||
merge, | ||
newTopologyObject, | ||
} from "../src/index.js"; | ||
|
||
describe("AreCausallyDependent benchmark", async () => { | ||
const samples = 100000; | ||
const tests: Hash[][] = []; | ||
|
||
const obj1 = await newTopologyObject("peer1", new AddWinsSet<number>()); | ||
const obj2 = await newTopologyObject("peer2", new AddWinsSet<number>()); | ||
const obj3 = await newTopologyObject("peer3", new AddWinsSet<number>()); | ||
|
||
const cro1 = obj1.cro as AddWinsSet<number>; | ||
const cro2 = obj2.cro as AddWinsSet<number>; | ||
const cro3 = obj3.cro as AddWinsSet<number>; | ||
|
||
cro1.add(1); | ||
merge(obj2, obj1.hashGraph.getAllVertices()); | ||
|
||
cro1.add(1); | ||
cro1.remove(2); | ||
cro2.remove(2); | ||
cro2.add(2); | ||
|
||
merge(obj3, obj1.hashGraph.getAllVertices()); | ||
cro3.add(3); | ||
cro1.remove(1); | ||
|
||
merge(obj1, obj2.hashGraph.getAllVertices()); | ||
cro1.remove(3); | ||
cro2.remove(1); | ||
|
||
merge(obj1, obj2.hashGraph.getAllVertices()); | ||
merge(obj1, obj3.hashGraph.getAllVertices()); | ||
|
||
const vertices = obj1.hashGraph.getAllVertices(); | ||
for (let i = 0; i < samples; i++) { | ||
tests.push([ | ||
vertices[Math.floor(Math.random() * vertices.length)].hash, | ||
vertices[Math.floor(Math.random() * vertices.length)].hash, | ||
]); | ||
} | ||
|
||
bench("Causality check using BFS", async () => { | ||
const cro1 = obj1.cro as AddWinsSet<number>; | ||
|
||
for (let i = 0; i < samples; i++) { | ||
const result = obj1.hashGraph.areCausallyRelatedUsingBFS( | ||
tests[i][0], | ||
tests[i][1], | ||
); | ||
} | ||
}); | ||
|
||
bench("Causality check using Bitsets", async () => { | ||
const cro1 = obj1.cro as AddWinsSet<number>; | ||
|
||
for (let i = 0; i < samples; i++) { | ||
const result = obj1.hashGraph.areCausallyRelatedUsingBitsets( | ||
tests[i][0], | ||
tests[i][1], | ||
); | ||
} | ||
}); | ||
}); |