-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(store-sync): add query benchmark, refactor test data scripts (#2228
- Loading branch information
Showing
17 changed files
with
880 additions
and
745 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
12 changes: 12 additions & 0 deletions
12
e2e/packages/contracts/src/codegen/world/IVectorSystem.sol
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,11 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.24; | ||
|
||
import { System } from "@latticexyz/world/src/System.sol"; | ||
import { Vector } from "../codegen/index.sol"; | ||
|
||
contract VectorSystem is System { | ||
function setVector(uint32 key, int32 x, int32 y) public { | ||
Vector.set(key, x, y); | ||
} | ||
} |
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,42 @@ | ||
import fs from "node:fs/promises"; | ||
import path from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
import { createAnvil } from "@viem/anvil"; | ||
import { generateLogs } from "./generateLogs"; | ||
|
||
const anvil = createAnvil({ | ||
blockTime: 1, | ||
blockBaseFeePerGas: 0, | ||
gasLimit: 20_000_000, | ||
}); | ||
|
||
console.log("starting anvil"); | ||
await anvil.start(); | ||
const rpc = `http://${anvil.host}:${anvil.port}`; | ||
|
||
const logs = await generateLogs(rpc, async (worldContract) => { | ||
for (let i = 0; i < 100; i++) { | ||
await worldContract.write.setNumber([i, i]); | ||
} | ||
|
||
for (let i = 0; i < 50; i++) { | ||
await worldContract.write.setVector([i, i, i]); | ||
} | ||
|
||
const lastTx = await worldContract.write.set([[0]]); | ||
|
||
return lastTx; | ||
}); | ||
|
||
const logsFilename = path.join( | ||
path.dirname(fileURLToPath(import.meta.url)), | ||
`../../../test-data/world-logs-query.json` | ||
); | ||
|
||
console.log("writing", logs.length, "logs to", logsFilename); | ||
await fs.writeFile(logsFilename, JSON.stringify(logs, null, 2)); | ||
|
||
// TODO: figure out why anvil doesn't stop immediately | ||
// console.log("stopping anvil"); | ||
// await anvil.stop(); | ||
process.exit(0); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,36 @@ | ||
import { bench, describe } from "vitest"; | ||
import { Has, runQuery } from "@latticexyz/recs"; | ||
import { createRecsStorage, createSqliteStorage, createZustandStorage, tables } from "../test/utils"; | ||
import { logsToBlocks } from "../test/logsToBlocks"; | ||
import worldRpcLogs from "../../../test-data/world-logs-query.json"; | ||
import { buildTable, getTables } from "../src/sqlite"; | ||
import { eq } from "drizzle-orm"; | ||
|
||
const blocks = logsToBlocks(worldRpcLogs); | ||
|
||
const { components, storageAdapter: recsStorageAdapter } = createRecsStorage(); | ||
const { useStore, storageAdapter: zustandStorageAdapter } = createZustandStorage(); | ||
const { database, storageAdapter: sqliteStorageAdapter } = await createSqliteStorage(); | ||
|
||
for (const block of blocks) { | ||
await Promise.all([recsStorageAdapter(block), zustandStorageAdapter(block), sqliteStorageAdapter(block)]); | ||
} | ||
|
||
describe("Get records with query", async () => { | ||
bench("recs: `runQuery`", async () => { | ||
runQuery([Has(components.Number), Has(components.Vector)]); | ||
}); | ||
|
||
bench("zustand: `getRecords`", async () => { | ||
const records = useStore.getState().getRecords(tables.Number); | ||
|
||
Object.keys(useStore.getState().getRecords(tables.Vector)).filter((id) => id in records); | ||
}); | ||
|
||
bench("sqlite: `innerJoin`", async () => { | ||
const numberTable = buildTable(getTables(database).filter((table) => table.name === "Number")[0]); | ||
const vectorTable = buildTable(getTables(database).filter((table) => table.name === "Vector")[0]); | ||
|
||
await database.select().from(numberTable).innerJoin(vectorTable, eq(numberTable.key, vectorTable.key)); | ||
}); | ||
}); |
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
Oops, something went wrong.