Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add basic benchmark script #3

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions bench/bench.js
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)
})
})
45 changes: 44 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"types": "tsc -p tsconfig.json",
"test:unit": "node --test tests/*.js",
"test": "npm-run-all lint types test:unit",
"bench": "node bench/bench.js",
"build": "tsc -p tsconfig.npm.json",
"prepack": "npm run build",
"prepare": "husky install",
Expand Down Expand Up @@ -46,10 +47,12 @@
"devDependencies": {
"@digidem/types": "^2.1.0",
"@mapeo/crypto": "^1.0.0-alpha.8",
"@types/nanobench": "^3.0.0",
"@types/node": "^20.7.1",
"eslint": "^8.50.0",
"husky": "^8.0.0",
"lint-staged": "^14.0.1",
"nanobench": "^3.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"random-access-memory": "^6.2.0",
Expand Down
60 changes: 4 additions & 56 deletions tests/basic.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
import test from 'node:test'
import assert from 'node:assert/strict'
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',
)

import { closeMapeoClient } from '../src/client.js'

import { setup } from './helpers.js'

test('IPC wrappers work', async () => {
const { client, cleanup } = setup()
Expand Down Expand Up @@ -154,37 +136,3 @@ test('Client calls fail after server closes', async () => {

return cleanup()
})

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()
},
}
}
57 changes: 57 additions & 0 deletions tests/helpers.js
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()
},
}
}
Loading