Skip to content

Commit

Permalink
Add a measure_respond script for debugging / flamegraphing
Browse files Browse the repository at this point in the history
  • Loading branch information
sgwilym committed Feb 2, 2023
1 parent 67b70a9 commit 2e15790
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 68 deletions.
72 changes: 72 additions & 0 deletions scripts/measure_respond.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { assertEquals } from "https://deno.land/std@0.158.0/testing/asserts.ts";
import { FingerprintTree } from "../src/fingerprint_tree/fingerprint_tree.ts";
import { concatMonoid } from "../src/lifting_monoid.ts";
import { RangeMessenger } from "../src/range_messenger/range_messenger.ts";
import { objConfig } from "../src/range_messenger/range_messenger_config.ts";
import { reconcile } from "../src/util.ts";

function makeSet(size: number): number[] {
const set = new Set<number>();

for (let i = 0; i < size; i++) {
const int = Math.floor(Math.random() * ((size * 2) - 1 + 1) + 1);

set.add(int);
}

return Array.from(set);
}

function compare<T>(a: T, b: T) {
if (a > b) {
return 1;
} else if (a < b) {
return -1;
} else {
return 0;
}
}

function nativeEquals(a: string, b: string) {
return a === b;
}

const setSize = 10000;

const treeA = new FingerprintTree(concatMonoid, compare);

const setA = makeSet(setSize);

for (const item of setA) {
treeA.insert(`${item}`);
}

const brokerA = new RangeMessenger({
tree: treeA,
fingerprintEquals: nativeEquals,
encoding: objConfig,
payloadThreshold: 1,
rangeDivision: 2,
});

// Other peer

const treeB = new FingerprintTree(concatMonoid, compare);

const setB = makeSet(setSize);

for (const item of setB) {
treeB.insert(`${item}`);
}

const brokerB = new RangeMessenger({
tree: treeB,
fingerprintEquals: nativeEquals,
encoding: objConfig,
payloadThreshold: 1,
rangeDivision: 2,
});

await reconcile(brokerA, brokerB);

assertEquals(treeA.lnrValues(), treeB.lnrValues());
68 changes: 0 additions & 68 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export function reconcile<E, V, L>(
for (const msg of responses) {
queueA.push(msg);
}

console.groupEnd();
}
})();

Expand Down Expand Up @@ -162,69 +160,3 @@ export class FastFIFO<T> {
}
}
}

/** Execute a complete exchange between two RangeMessengers, syncing their trees. */
/*
export async function reconcile<E, V, L>(
a: RangeMessenger<E, V, L>,
b: RangeMessenger<E, V, L>,
): Promise<unknown[]> {
const queueA = new AsyncQueue<E>();
const queueB = new AsyncQueue<E>();
queueB.push(...a.initialMessages());
(async () => {
for await (const msg of queueB) {
const responses = b.respond(msg);
if (responses.length) {
queueA.push(...responses);
}
}
})();
(async () => {
for await (const msg of queueA) {
const responses = a.respond(msg);
if (responses.length) {
queueB.push(...responses);
}
}
})();
a.isDone().then(() => queueA.close());
b.isDone().then(() => queueB.close());
return Promise.all([a.isDone(), b.isDone()]);
}
*/

/** Execute a complete exchange between two RangeMessengers, syncing their trees. */
/*
export async function reconcile<E, V, L>(
from: RangeMessenger<E, V, L>,
to: RangeMessenger<E, V, L>,
round: number = 0,
messages?: AsyncIterable<E> | Iterable<E>,
isDone?: Deferred<unknown>,
): Promise<void> {
const msgs: E[] = [];
const messagesToProcess = messages || from.initialMessages();
for await (
const msg of messagesToProcess
) {
const responses = to.respond(msg);
msgs.push(...responses);
}
if (isDone?.state === "fulfilled") {
return Promise.resolve();
} else {
await reconcile(to, from, round + 1, msgs, to.isDone());
}
}
*/

0 comments on commit 2e15790

Please sign in to comment.