-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(store-sync): add benchmarks to store-sync (#2178)
- Loading branch information
Showing
7 changed files
with
145 additions
and
0 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
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, {}); | ||
}); | ||
}); |
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,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(); | ||
}); | ||
}); |
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,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); | ||
} | ||
}); | ||
}); |
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,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; | ||
}) | ||
); |
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,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 }); |