Skip to content

Commit

Permalink
fix(@libp2p/protocol-perf): ensure only client calls measure performa…
Browse files Browse the repository at this point in the history
…nce (#1960)
  • Loading branch information
maschad authored Aug 14, 2023
1 parent 7b2ddc1 commit 8716555
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
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

0 comments on commit 8716555

Please sign in to comment.