Add assembleEvents bench

This commit is contained in:
Alex Gleason
2024-05-25 10:30:46 -05:00
parent 40369be6c6
commit 14e5948394
5 changed files with 61 additions and 3 deletions

View File

@@ -0,0 +1,14 @@
import { assembleEvents } from '@/storages/hydrate.ts';
import { jsonlEvents } from '@/test.ts';
const testEvents = await jsonlEvents('fixtures/hydrated.jsonl');
Deno.bench('assembleEvents with home feed', (b) => {
// The first 20 events in this file are my home feed.
// The rest are events that would be hydrated by the store.
const events = testEvents.slice(0, 20);
b.start();
assembleEvents(events, testEvents, { authors: [], events: [] });
});

View File

@@ -1,8 +1,8 @@
import { assertEquals } from '@std/assert';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { MockRelay } from '@nostrify/nostrify/test';
import { assertEquals } from '@std/assert';
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { eventFixture } from '@/test.ts';
Deno.test('hydrateEvents(): author --- WITHOUT stats', async () => {

View File

@@ -67,7 +67,7 @@ async function hydrateEvents(opts: HydrateOpts): Promise<DittoEvent[]> {
}
/** Connect the events in list `b` to the DittoEvent fields in list `a`. */
function assembleEvents(
export function assembleEvents(
a: DittoEvent[],
b: DittoEvent[],
stats: { authors: DittoTables['author_stats'][]; events: DittoTables['event_stats'][] },

View File

@@ -16,6 +16,12 @@ export async function eventFixture(name: string): Promise<NostrEvent> {
return structuredClone(result.default);
}
/** Import a JSONL fixture by name in tests. */
export async function jsonlEvents(path: string): Promise<NostrEvent[]> {
const data = await Deno.readTextFile(path);
return data.split('\n').map((line) => JSON.parse(line));
}
/** Generate an event for use in tests. */
export function genEvent(t: Partial<NostrEvent> = {}, sk: Uint8Array = generateSecretKey()): NostrEvent {
const event = finalizeEvent({