Skip to content

Commit

Permalink
chore(store-sync): add benchmarks to store-sync (#2178)
Browse files Browse the repository at this point in the history
  • Loading branch information
yonadaa authored Jan 26, 2024
1 parent 6ca974b commit a5bb956
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/store-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
}
},
"scripts": {
"bench": "vitest bench",
"build": "pnpm run build:js",
"build:js": "tsup",
"clean": "pnpm run clean:js",
Expand Down
26 changes: 26 additions & 0 deletions packages/store-sync/src/getRecord.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { bench, describe } from "vitest";
import { getComponentValue } from "@latticexyz/recs";
import { blocks } from "../test/blocks";
import {
components,
recsStorageAdapter,
sqliteStorageAdapter,
tables,
useStore,
zustandStorageAdapter,
} from "../test/utils";
import { singletonEntity } from "./recs";

for (const block of blocks) {
await Promise.all([recsStorageAdapter(block), zustandStorageAdapter(block), sqliteStorageAdapter(block)]);
}

describe("Get single record by key", () => {
bench("recs: `getComponentValue`", async () => {
getComponentValue(components.NumberList, singletonEntity);
});

bench("zustand: `getRecord`", async () => {
useStore.getState().getRecord(tables.NumberList, {});
});
});
34 changes: 34 additions & 0 deletions packages/store-sync/src/getRecords.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { bench, describe } from "vitest";
import { getComponentEntities, getComponentValue } from "@latticexyz/recs";
import { eq } from "drizzle-orm";
import { mudStoreTables } from "./sqlite";
import { blocks } from "../test/blocks";
import {
components,
db,
recsStorageAdapter,
sqliteStorageAdapter,
tables,
useStore,
zustandStorageAdapter,
} from "../test/utils";

for (const block of blocks) {
await Promise.all([recsStorageAdapter(block), zustandStorageAdapter(block), sqliteStorageAdapter(block)]);
}

describe("Get all records for table", () => {
bench("recs: `getComponentValue`", async () => {
for (const entity of getComponentEntities(components.NumberList)) {
getComponentValue(components.NumberList, entity);
}
});

bench("zustand: `getRecords`", async () => {
useStore.getState().getRecords(tables.NumberList);
});

bench("sqlite: `select`", async () => {
db.select().from(mudStoreTables).where(eq(mudStoreTables.name, "NumberList")).all();
});
});
16 changes: 16 additions & 0 deletions packages/store-sync/src/recs/recsStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,21 @@ describe("recsStorage", () => {
],
}
`);

expect(
[...getComponentEntities(components.NumberList)].map((entity) => getComponentValue(components.NumberList, entity))
).toMatchInlineSnapshot(`
[
{
"__dynamicData": "0x000001a400000045",
"__encodedLengths": "0x0000000000000000000000000000000000000000000000000800000000000008",
"__staticData": undefined,
"value": [
420,
69,
],
},
]
`);
});
});
24 changes: 24 additions & 0 deletions packages/store-sync/src/storageAdapter.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { bench, describe } from "vitest";

import { blocks } from "../test/blocks";
import { recsStorageAdapter, sqliteStorageAdapter, zustandStorageAdapter } from "../test/utils";

describe("Storage Adapter", () => {
bench("recs: `storageAdapter`", async () => {
for (const block of blocks) {
await recsStorageAdapter(block);
}
});

bench("zustand: `storageAdapter`", async () => {
for (const block of blocks) {
await zustandStorageAdapter(block);
}
});

bench("sqlite: `storageAdapter`", async () => {
for (const block of blocks) {
await sqliteStorageAdapter(block);
}
});
});
18 changes: 18 additions & 0 deletions packages/store-sync/test/blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { groupLogsByBlockNumber } from "@latticexyz/block-logs-stream";
import { storeEventsAbi } from "@latticexyz/store";
import { RpcLog, formatLog, decodeEventLog, Hex } from "viem";
import worldRpcLogs from "../../../test-data/world-logs.json";
import { StoreEventsLog } from "../src/common";

// TODO: make test-data a proper package and export this
export const blocks = groupLogsByBlockNumber(
worldRpcLogs.map((log) => {
const { eventName, args } = decodeEventLog({
abi: storeEventsAbi,
data: log.data as Hex,
topics: log.topics as [Hex, ...Hex[]],
strict: true,
});
return formatLog(log as any as RpcLog, { args, eventName: eventName as string }) as StoreEventsLog;
})
);
26 changes: 26 additions & 0 deletions packages/store-sync/test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { drizzle } from "drizzle-orm/sql-js";
import { createPublicClient, http } from "viem";
import { foundry } from "viem/chains";
import initSqlJs from "sql.js";
import mudConfig from "../../../e2e/packages/contracts/mud.config";
import { createWorld } from "@latticexyz/recs";
import { resolveConfig } from "@latticexyz/store";
import { recsStorage } from "../src/recs";
import { sqliteStorage } from "../src/sqlite";
import { createStorageAdapter, createStore } from "../src/zustand";

const publicClient = createPublicClient({
chain: foundry,
transport: http(),
});

export const tables = resolveConfig(mudConfig).tables;

export const { components, storageAdapter: recsStorageAdapter } = recsStorage({ world: createWorld(), tables });

export const useStore = createStore({ tables });
export const zustandStorageAdapter = createStorageAdapter({ store: useStore });

const SqlJs = await initSqlJs();
export const db = drizzle(new SqlJs.Database(), {});
export const sqliteStorageAdapter = await sqliteStorage({ database: db, publicClient });

0 comments on commit a5bb956

Please sign in to comment.