Skip to content

Commit

Permalink
perf: add benchmark
Browse files Browse the repository at this point in the history
Adds benchmarks for writing and traversal

Closes hldb#43, hldb#47
  • Loading branch information
tabcat committed Jan 21, 2023
1 parent 5fdcc36 commit 4f9569d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
66 changes: 66 additions & 0 deletions benchmark/benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { IPFS } from 'ipfs-core-types'
import all from 'it-all'
import type { Libp2p } from 'libp2p'
import { dagLinks, loadEntry, traverser } from '~replica/traversal.js'
import { parsedcid } from '~utils/index.js'
import { Welo } from '../src/index.js'
import { getTestPaths, tempPath } from '../test/utils/constants.js'
import { getTestIpfs, offlineIpfsOptions } from '../test/utils/ipfs.js'

const paths = getTestPaths(tempPath, 'benchmark')
const ipfs = await getTestIpfs(paths, offlineIpfsOptions) as (IPFS & { libp2p: Libp2p })
if (ipfs.libp2p == null) {
throw new Error('ipfs.libp2p is not defined')
}
const libp2p = ipfs.libp2p

const welo = await Welo.create({ ipfs, libp2p, directory: paths.database })
const db = await welo.open(await welo.determine({ name: '1000' }))

/**
* write 1000 entries
*/
console.log('writing 1000 entries')
console.time('wrote 1000 entries')
let timeNew = Date.now()
let timeOld: number
for (let i = 0; i < 1000; i++) {
if (i % 100 === 0) {
console.log(`wrote ${i} entries`)
timeOld = timeNew
timeNew = Date.now()
console.log(`wrote the last 100 entries in ${(timeNew - timeOld) / 1000} seconds`)
}
await db.replica.write({})
}
console.timeEnd('wrote 1000 entries')

/**
* descending ordered traversal of entries
*/
console.log('descending traversal of ordered entries')
console.time('descending ordered traversal')
await db.replica.traverse({ direction: 'descend' })
console.timeEnd('descending ordered traversal')

/**
* ascending ordered traversal of entries
*/
console.log('ascending traversal of ordered entries')
console.time('ascending ordered traversal')
await db.replica.traverse({ direction: 'ascend' })
console.timeEnd('ascending ordered traversal')

/**
* unordered traversal of entries
*/
console.log('traversing unordered entries')
console.time('unordered traversal')
const { blocks, access, Entry, Identity } = db.replica
const load = loadEntry({ blocks, Entry, Identity })
const links = dagLinks({ graph: { has: (string): boolean => false }, access })
await traverser({ cids: (await all(db.replica.heads.keys())).map(parsedcid), load, links })
console.timeEnd('unordered traversal')

await welo.stop()
await ipfs.stop()
1 change: 0 additions & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dep-check": "npx depcheck --ignores=~*,polendina,ts-node,typescript-transform-paths",
"copy-deps": "npx copy-deps",
"format": "prettier -w src test && eslint --fix src test",
"benchmark": "npm run clean && npm run build && node dist/benchmark/benchmark.js && npm run clean",
"build": "tsc",
"extract-api": "api-extractor run -c ./config/api-extractor.json -l -v",
"document-api": "api-documenter markdown -i ./API -o ./API",
Expand Down Expand Up @@ -48,7 +49,6 @@
"@microsoft/api-extractor": "^7.33.6",
"@types/mocha": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^5.47.1",
"benchmark": "^2.1.4",
"c8": "^7.12.0",
"copy-deps": "^1.1.2",
"depcheck": "^1.4.3",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
},
"include": [
"src",
"test"
"test",
"benchmark"
],
"exclude": [
"node_modules",
Expand Down

0 comments on commit 4f9569d

Please sign in to comment.