Skip to content

Commit

Permalink
chore: add basic benchmark script (#3)
Browse files Browse the repository at this point in the history
`npm run bench` will create a million projects and tell you how long it
takes.
  • Loading branch information
EvanHahn authored Jan 30, 2024
1 parent 4713f72 commit d25dff5
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 57 deletions.
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()
},
}
}

0 comments on commit d25dff5

Please sign in to comment.