Skip to content

feat: add validate function and improve performance #420

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions benchmarks/operations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# multiaddr Benchmark

Benchmarks multiaddr performance during common operations - parsing strings,
encapsulating/decapsulating addresses, turning to bytes, decoding bytes, etc.

## Running the benchmarks

```console
% npm start

> [email protected] start
> npm run build && node dist/src/index.js


> [email protected] build
> aegir build --bundle false

[06:10:56] tsc [started]
[06:10:56] tsc [completed]
┌─────────┬──────────────────────────────────┬─────────────┬────────┬───────┬────────┐
│ (index) │ Implementation │ ops/s │ ms/op │ runs │ p99 │
├─────────┼──────────────────────────────────┼─────────────┼────────┼───────┼────────┤
│ 0 │ 'head' │ '105679.89' │ '0.01' │ 50000 │ '0.01' │
│ 1 │ '@multiformats/[email protected]' │ '18244.31' │ '0.06' │ 50000 │ '0.13' │
└─────────┴──────────────────────────────────┴─────────────┴────────┴───────┴────────┘
```
21 changes: 21 additions & 0 deletions benchmarks/operations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "benchmarks-operations",
"version": "1.0.0",
"main": "index.js",
"private": true,
"type": "module",
"scripts": {
"clean": "aegir clean",
"build": "aegir build --bundle false",
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"doc-check": "aegir doc-check",
"start": "npm run build && node dist/test/index.js"
},
"devDependencies": {
"@multiformats/multiaddr-12.4.0": "npm:@multiformats/[email protected]",
"@multiformats/multiaddr": "../../",
"aegir": "^47.0.7",
"tinybench": "^4.0.1"
}
}
1 change: 1 addition & 0 deletions benchmarks/operations/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
68 changes: 68 additions & 0 deletions benchmarks/operations/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable no-console */

import { multiaddr } from '@multiformats/multiaddr'
import { multiaddr as multiaddr12 } from '@multiformats/multiaddr-12.4.0'
import { Bench } from 'tinybench'

const ITERATIONS = parseInt(process.env.ITERATIONS ?? '50000')
const MIN_TIME = parseInt(process.env.MIN_TIME ?? '1')
const RESULT_PRECISION = 2

function bench (m: typeof multiaddr | typeof multiaddr12): void {
const ma = m('/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/uEiAkH5a4DPGKUuOBjYw0CgwjvcJCJMD2K_1aluKR_tpevQ/certhash/uEiAfbgiymPP2_nX7Dgir8B4QkksjHp2lVuJZz0F79Be9JA')
ma.encapsulate('/p2p-circuit/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
ma.decapsulate('/quic-v1')

const ma2 = m(ma.bytes)
ma2.encapsulate('/tls/sni/example.com/http/http-path/path%2Findex.html')
ma2.equals(ma)

ma2.getPath()
ma2.getPeerId()

const ma3 = m('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/1234/quic-v1/webtransport/certhash/uEiAkH5a4DPGKUuOBjYw0CgwjvcJCJMD2K_1aluKR_tpevQ/certhash/uEiAfbgiymPP2_nX7Dgir8B4QkksjHp2lVuJZz0F79Be9JA')
ma3.encapsulate('/p2p-circuit/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
ma3.decapsulate('/quic-v1')
}

async function main (): Promise<void> {
const suite = new Bench({
iterations: ITERATIONS,
time: MIN_TIME
})
suite.add('head', () => {
bench(multiaddr)
})
suite.add('@multiformats/[email protected]', () => {
bench(multiaddr12)
})

await suite.run()

console.table(suite.tasks.map(({ name, result }) => {
if (result?.error != null) {
console.error(result.error)

return {
Implementation: name,
'ops/s': 'error',
'ms/op': 'error',
runs: 'error',
p99: 'error'
}
}

return {
Implementation: name,
'ops/s': result?.hz.toFixed(RESULT_PRECISION),
'ms/op': result?.period.toFixed(RESULT_PRECISION),
runs: result?.samples.length,
p99: result?.p99.toFixed(RESULT_PRECISION)
}
}))
}

main().catch(err => {
console.error(err)
process.exit(1)
})
15 changes: 15 additions & 0 deletions benchmarks/operations/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src",
"test"
],
"references": [
{
"path": "../../"
}
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
"@chainsafe/is-ip": "^2.0.1",
"@chainsafe/netmask": "^2.0.0",
"@multiformats/dns": "^1.0.3",
"abort-error": "^1.0.1",
"multiformats": "^13.0.0",
"uint8-varint": "^2.0.1",
"uint8arrays": "^5.0.0"
Expand Down
236 changes: 0 additions & 236 deletions src/codec.ts

This file was deleted.

Loading