-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add basic benchmark script (#3)
`npm run bench` will create a million projects and tell you how long it takes.
- Loading branch information
Showing
5 changed files
with
138 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// @ts-check | ||
|
||
import bench from 'nanobench' | ||
import assert from 'node:assert/strict' | ||
import { setup } from '../tests/helpers.js' | ||
|
||
const GET_PROJECT_COUNT = 1_000_000 | ||
|
||
bench(`fetching ${GET_PROJECT_COUNT} projects`, async (b) => { | ||
const { client, cleanup } = setup() | ||
|
||
const projectId = await client.createProject() | ||
|
||
b.start() | ||
|
||
const projects = await Promise.all( | ||
new Array(GET_PROJECT_COUNT) | ||
.fill(null) | ||
.map(() => client.getProject(projectId)), | ||
) | ||
|
||
b.end() | ||
|
||
await cleanup() | ||
|
||
const [firstProject] = projects | ||
projects.forEach((project) => { | ||
assert.equal(project, firstProject) | ||
}) | ||
}) |
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,57 @@ | ||
import { MessageChannel } from 'node:worker_threads' | ||
import RAM from 'random-access-memory' | ||
import { KeyManager } from '@mapeo/crypto' | ||
import { MapeoManager } from '@mapeo/core' | ||
import { createRequire } from 'node:module' | ||
import path from 'node:path' | ||
|
||
import { createMapeoClient, closeMapeoClient } from '../src/client.js' | ||
import { createMapeoServer } from '../src/server.js' | ||
|
||
const require = createRequire(import.meta.url) | ||
|
||
const MAPEO_CORE_PKG_FOLDER = path.dirname( | ||
require.resolve('@mapeo/core/package.json'), | ||
) | ||
const projectMigrationsFolder = path.join( | ||
MAPEO_CORE_PKG_FOLDER, | ||
'drizzle/project', | ||
) | ||
const clientMigrationsFolder = path.join( | ||
MAPEO_CORE_PKG_FOLDER, | ||
'drizzle/client', | ||
) | ||
|
||
export function setup() { | ||
const { port1, port2 } = new MessageChannel() | ||
|
||
const manager = new MapeoManager({ | ||
rootKey: KeyManager.generateRootKey(), | ||
dbFolder: ':memory:', | ||
coreStorage: () => new RAM(), | ||
projectMigrationsFolder, | ||
clientMigrationsFolder, | ||
}) | ||
|
||
// Since v14.7.0, Node's MessagePort extends EventTarget (https://nodejs.org/api/worker_threads.html#class-messageport) | ||
// @ts-expect-error | ||
const server = createMapeoServer(manager, port1) | ||
// @ts-expect-error | ||
const client = createMapeoClient(port2) | ||
|
||
port1.start() | ||
port2.start() | ||
|
||
return { | ||
port1, | ||
port2, | ||
server, | ||
client, | ||
cleanup: async () => { | ||
server.close() | ||
await closeMapeoClient(client) | ||
port1.close() | ||
port2.close() | ||
}, | ||
} | ||
} |