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

fix(@libp2p/protocol-perf): ensure only client calls measure performance #1960

Merged
merged 14 commits into from
Aug 14, 2023
Merged
1 change: 0 additions & 1 deletion packages/protocol-perf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"@libp2p/tcp": "^8.0.3",
"@multiformats/multiaddr": "^12.1.5",
"libp2p": "^0.46.4",
"p-wait-for": "^5.0.2",
"uint8arrays": "^4.0.6",
"yargs": "^17.7.2"
},
Expand Down
26 changes: 7 additions & 19 deletions packages/protocol-perf/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import { tcp } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2p } from 'libp2p'
import { plaintext } from 'libp2p/insecure'
import pWaitFor from 'p-wait-for'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { defaultInit, perfService } from '../src/index.js'
import type { Connection } from '@libp2p/interface/connection'

const argv = yargs(hideBin(process.argv))
.options({
Expand Down Expand Up @@ -89,24 +87,14 @@ export async function main (runServer: boolean, serverIpAddress: string, transpo

const startTime = Date.now()

let connection: any = null

if (runServer) {
node.addEventListener('connection:open', (eventInfo) => {
connection = eventInfo.detail
})
} else {
connection = await node.dial(multiaddr(tcpMultiaddrAddress))
if (!runServer) {
const connection = await node.dial(multiaddr(tcpMultiaddrAddress))
const duration = await node.services.perf.measurePerformance(startTime, connection, BigInt(uploadBytes), BigInt(downloadBytes))
// Output latency to stdout in seconds
// eslint-disable-next-line no-console
console.log(JSON.stringify({ latency: duration / 1000 }))
await node.stop()
}

await pWaitFor(() => connection != null)

const duration = await node.services.perf.measurePerformance(startTime, connection as Connection, BigInt(uploadBytes), BigInt(downloadBytes))

await node.stop()

// eslint-disable-next-line no-console
console.log('latency: ' + JSON.stringify({ latency: duration }))
}

function splitHostPort (address: string): { host: string, port?: string } {
Expand Down
Loading