Skip to content

Commit

Permalink
Wretched day.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgwilym committed Oct 19, 2022
1 parent 1709ccc commit 10d9cc0
Show file tree
Hide file tree
Showing 10 changed files with 817 additions and 806 deletions.
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- [ ] Set up benchmarks for monoids
- [ ] Set up tests for monoids (associativity, identity)
- [ ] Make b and k of MessageBroker configurable
- [ ] Create comparative benchmarks for syncing with changing b / k
107 changes: 61 additions & 46 deletions scripts/msg_broker.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
import { assertEquals } from "https://deno.land/std@0.158.0/testing/asserts.ts";
import { FingerprintTree } from "../src/fingerprint_tree.ts";
import { testMonoid } from "../src/lifting_monoid.ts";
import { xxHash32XorMonoid } from "../src/lifting_monoid.ts";
import { MessageBroker } from "../src/message_broker.ts";
import { testConfig } from "../src/message_broker_config.ts";
import { uint8TestConfig } from "../src/message_broker_config.ts";
import { sync } from "../src/util.ts";

function multiplyElements(elements: string[], by: number): string[] {
const acc = [];
//const logMsgRounds = false;

for (const element of elements) {
acc.push(element);
function makeSet(size: number): number[] {
const set = new Set<number>();

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

set.add(int);
}

return acc;
return Array.from(set);
}

const size = 100000;

// Set up peer

const treeA = new FingerprintTree(testMonoid);
console.log("Generating sets...");

const setA = makeSet(size);
const setB = makeSet(size);

const setA = [
"eel",
"bee",
"fox",
"hog",
"cat",
"ape",
"gnu",
];
const treeA = new FingerprintTree(xxHash32XorMonoid);

console.log("Inserting into tree a...");

const encoder = new TextEncoder();

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

const brokerA = new MessageBroker(treeA, testConfig, false);
console.log("Inserting into tree b...");

// Other peer

const treeB = new FingerprintTree(testMonoid);

const setB = ["doe", "doe2", "fox", "fox2"];
const treeB = new FingerprintTree(xxHash32XorMonoid);

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

const brokerB = new MessageBroker(treeB, testConfig, true);

/*
const aLog: string[] = [];
const bLog: string[] = [];
Expand All @@ -63,15 +62,17 @@ const printerA = new TransformStream<string>({
if (message.includes("TERMINAL")) {
aLogs.push(aLog.splice(0, aLog.length));
console.group("%c A", "color: red");
if (logMsgRounds) {
console.group("%c A", "color: red");
const logs = aLogs[aLogs.length - 1];
const logs = aLogs[aLogs.length - 1];
for (const log of logs) {
console.log(`%c ${log}`, "color: red");
}
for (const log of logs) {
console.log(`%c ${log}`, "color: red");
}
console.groupEnd();
console.groupEnd();
}
}
controller.enqueue(message);
Expand All @@ -85,43 +86,57 @@ const printerB = new TransformStream<string>({
if (message.includes("TERMINAL")) {
bLogs.push(bLog.splice(0, bLog.length));
console.group("%c B", "color: blue");
if (logMsgRounds) {
console.group("%c B", "color: blue");
const logs = bLogs[bLogs.length - 1];
const logs = bLogs[bLogs.length - 1];
for (const log of logs) {
console.log(`%c ${log}`, "color: blue");
}
for (const log of logs) {
console.log(`%c ${log}`, "color: blue");
}
console.groupEnd();
console.groupEnd();
}
}
controller.enqueue(message);
},
});
/*
console.group("%c A has:", "color: red");
console.log(`%c ${Array.from(treeA.lnrValues())}`, "color: red");
console.groupEnd();
console.group("%c B has:", "color: blue");
console.log(`%c ${Array.from(treeB.lnrValues())}`, "color: blue");
console.groupEnd();
*/

brokerB.readable
.pipeThrough(printerB)
.pipeThrough(brokerA)
.pipeThrough(printerA)
.pipeTo(brokerB.writable);
console.log("Syncing...");

await Promise.all([brokerA.isDone(), brokerB.isDone()]);
const brokerA = new MessageBroker(
treeA,
uint8TestConfig,
);

const brokerB = new MessageBroker(
treeB,
uint8TestConfig,
);

await sync(brokerA, brokerB);

/*
console.group("%c A has:", "color: red");
console.log(`%c ${Array.from(treeA.lnrValues())}`, "color: red");
console.groupEnd();
console.group("%c B has:", "color: blue");
console.log(`%c ${Array.from(treeB.lnrValues())}`, "color: blue");
console.groupEnd();
*/

assertEquals(Array.from(treeA.lnrValues()), Array.from(treeB.lnrValues()));

console.log("Done");
156 changes: 59 additions & 97 deletions src/fingerprint_tree.bench.ts
Original file line number Diff line number Diff line change
@@ -1,109 +1,71 @@
import { RedBlackTree } from "https://deno.land/std@0.158.0/collections/red_black_tree.ts";
import { FingerprintTree } from "./fingerprint_tree.ts";
import { testMonoid } from "./lifting_monoid.ts";
import { xxHash32XorMonoid } from "./lifting_monoid.ts";

const alphaElements = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
];
function makeSet(size: number): number[] {
const set = new Set<number>();

function multiplyElements(elements: string[], by: number): string[] {
const acc = [];
for (let i = 0; i < size; i++) {
const int = Math.floor(Math.random() * ((size * 2) - 1 + 1) + 1);

for (const element of elements) {
acc.push(element);

for (let i = 2; i <= by; i++) {
acc.push(element.repeat(i));
}
}

return acc;
}

function makeTree(
elements: string[],
): FingerprintTree<string, string> {
const tree = new FingerprintTree(testMonoid);

for (const element of elements) {
tree.insert(element);
set.add(int);
}

return tree;
return Array.from(set);
}

const smallestTree = makeTree(alphaElements);

Deno.bench("Fingerprint a - a (26 elements)", () => {
smallestTree.getFingerprint("a", "a");
});

Deno.bench("Fingerprint a - b (26 elements)", () => {
smallestTree.getFingerprint("a", "b");
});

Deno.bench("Fingerprint a - l (26 elements)", () => {
smallestTree.getFingerprint("a", "l");
});
const sizes = [10, 100, 1000, 10000, 100000];

Deno.bench("Fingerprint b - b (26 elements)", () => {
smallestTree.getFingerprint("b", "b");
});
const encoder = new TextEncoder();

Deno.bench("Fingerprint b - c (26 elements)", () => {
smallestTree.getFingerprint("b", "c");
});
for (const size of sizes) {
const set = makeSet(size);
const tree = new FingerprintTree(xxHash32XorMonoid);
const rbTree = new RedBlackTree();

Deno.bench("Fingerprint b - l (26 elements)", () => {
smallestTree.getFingerprint("b", "l");
});

const bigTree = makeTree(multiplyElements(alphaElements, 500));

Deno.bench("Fingerprint a - a (13000 elements)", () => {
bigTree.getFingerprint("a", "a");
});

Deno.bench("Fingerprint a - b (13000 elements)", () => {
bigTree.getFingerprint("a", "b");
});

Deno.bench("Fingerprint a - l (13000 elements)", () => {
bigTree.getFingerprint("a", "l");
});

Deno.bench("Fingerprint b - b (13000 elements)", () => {
bigTree.getFingerprint("b", "b");
});

Deno.bench("Fingerprint b - c (13000 elements)", () => {
bigTree.getFingerprint("b", "c");
});
Deno.bench(`Insert into RedBlackTree (${size} items)`, {
group: `insert (${size})`,
baseline: true,
}, () => {
for (const element of set) {
rbTree.insert(element);
}
});

Deno.bench("Fingerprint b - l (13000 elements)", () => {
bigTree.getFingerprint("b", "l");
});
Deno.bench(`Insert into FingerPrintTree (${size} items)`, {
group: `insert (${size})`,
}, () => {
for (const element of set) {
tree.insert(encoder.encode(`${element}`));
}
});

const min = encoder.encode(`${Math.min(...set)}`);
const max = encoder.encode(`${Math.max(...set)}`);
const mid = encoder.encode(`${Math.floor(Math.max(...set) / 2)}`);

Deno.bench(`Fingerprint min - min (${size} items) `, {
group: `fingerprint (${size})`,
baseline: true,
}, () => {
tree.getFingerprint(min, min);
});

Deno.bench(`Fingerprint min - mid (${size} items) `, {
group: `fingerprint (${size})`,
}, () => {
tree.getFingerprint(min, mid);
});

Deno.bench(`Fingerprint mid - mid (${size} items) `, {
group: `fingerprint (${size})`,
}, () => {
tree.getFingerprint(mid, mid);
});

Deno.bench(`Fingerprint mid - min (${size} items) `, {
group: `fingerprint (${size})`,
}, () => {
tree.getFingerprint(mid, min);
});
}
Loading

0 comments on commit 10d9cc0

Please sign in to comment.