Skip to content

Commit

Permalink
chore: linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Jul 26, 2023
1 parent b1d89bd commit ccd5f26
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 71 deletions.
1 change: 1 addition & 0 deletions packages/perf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
}
},
"scripts": {
"start": "node dist/src/main.js",
"build": "aegir build",
"test": "aegir test",
"clean": "aegir clean",
Expand Down
141 changes: 72 additions & 69 deletions packages/perf/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { mockRegistrar, mockUpgrader, mockConnectionGater, connectionPair } from '@libp2p/interface-compliance-tests/src/mocks'
import type { TransportManager } from '@libp2p/interface-internal/src/transport-manager'
import { EventEmitter } from '@libp2p/interface/src/events'
import { EventEmitter } from '@libp2p/interface/events'
import { start } from '@libp2p/interface/startable'
import { mockRegistrar, mockUpgrader, mockConnectionGater, connectionPair } from '@libp2p/interface-compliance-tests/mocks'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { PersistentPeerStore } from '@libp2p/peer-store'
import { multiaddr } from '@multiformats/multiaddr'
import { MemoryDatastore } from 'datastore-core'
import { DefaultAddressManager } from 'libp2p/src/address-manager'
import { type Components, defaultComponents } from 'libp2p/src/components'
import { DefaultConnectionManager } from 'libp2p/src/connection-manager'
import { stubInterface } from 'sinon-ts'
import yargs from 'yargs'
import { multiaddr } from '@multiformats/multiaddr'
import type { Multiaddr } from '@multiformats/multiaddr'
import { perfService, type PerfServiceInit } from '.'
import { start } from '@libp2p/interface/src/startable'

import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
import type { Multiaddr } from '@multiformats/multiaddr'

export const defaultInit: PerfServiceInit = {
protocolName: '/perf/1.0.0',
Expand All @@ -24,85 +23,85 @@ export const defaultInit: PerfServiceInit = {
}

const argv = yargs
.options({
'run-server': {
type: 'boolean',
demandOption: true,
default: false,
description: 'Whether to run as a server',
},
'server-ip-address': {
type: 'string',
demandOption: false,
description: 'Server IP address',
default: '',
},
'transport': {
type: 'string',
demandOption: false,
description: 'Transport to use',
default: 'tcp'
},
'upload-bytes': {
type: 'number',
demandOption: false,
description: 'Number of bytes to upload',
default: 0
},
'download-bytes': {
type: 'number',
demandOption: false,
description: 'Number of bytes to download',
default: 0
},
})
.command('help', 'Print usage information', yargs.help)
.parseSync()

export async function main(runServer: boolean, serverIpAddress: string, transport: string, uploadBytes: number, downloadBytes: number): Promise<void> {
const listenAddrs: Multiaddr[] = []

if (runServer === true) {
const { host, port } = splitHostPort(serverIpAddress)
// #TODO: right now we only support tcp
listenAddrs.push(multiaddr(`/ip4/${host}/tcp/${port}`))
}

const localComponents = await createComponents(listenAddrs)
const remoteComponents = await createComponents()

const client = perfService(defaultInit)(localComponents)
.options({
'run-server': {
type: 'boolean',
demandOption: true,
default: false,
description: 'Whether to run as a server'
},
'server-ip-address': {
type: 'string',
demandOption: false,
description: 'Server IP address',
default: ''
},
transport: {
type: 'string',
demandOption: false,
description: 'Transport to use',
default: 'tcp'
},
'upload-bytes': {
type: 'number',
demandOption: false,
description: 'Number of bytes to upload',
default: 0
},
'download-bytes': {
type: 'number',
demandOption: false,
description: 'Number of bytes to download',
default: 0
}
})
.command('help', 'Print usage information', yargs.help)
.parseSync()

export async function main (runServer: boolean, serverIpAddress: string, transport: string, uploadBytes: number, downloadBytes: number): Promise<void> {
const listenAddrs: Multiaddr[] = []

if (runServer) {
const { host, port } = splitHostPort(serverIpAddress)
// #TODO: right now we only support tcp
listenAddrs.push(multiaddr(`/ip4/${host}/tcp/${port}`))
}

const localComponents = await createComponents(listenAddrs)
const remoteComponents = await createComponents()

const client = perfService(defaultInit)(localComponents)
const server = perfService(defaultInit)(remoteComponents)

await start(client)
await start(server)

const startTime = Date.now()
const startTime = Date.now()

const [localToRemote, remoteToLocal] = connectionPair(localComponents, remoteComponents)
localComponents.events.safeDispatchEvent('connection:open', { detail: localToRemote })
remoteComponents.events.safeDispatchEvent('connection:open', { detail: remoteToLocal })
const [localToRemote, remoteToLocal] = connectionPair(localComponents, remoteComponents)
localComponents.events.safeDispatchEvent('connection:open', { detail: localToRemote })
remoteComponents.events.safeDispatchEvent('connection:open', { detail: remoteToLocal })

await client.perf(remoteComponents.peerId, BigInt(uploadBytes), BigInt(downloadBytes))
await client.perf(remoteComponents.peerId, BigInt(uploadBytes), BigInt(downloadBytes))

const endTime = Date.now()
const endTime = Date.now()

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

function splitHostPort(urlString: string): { host: string, port?: string } {
function splitHostPort (urlString: string): { host: string, port?: string } {
try {
const url = new URL(urlString);
const url = new URL(urlString)
return {
host: url.hostname,
port: url.port || undefined,
};
port: url.port
}
} catch (error) {
throw Error('Invalid server address');
throw Error('Invalid server address')
}
}


export async function createComponents (listenMaddrs: Multiaddr[] = []): Promise<Components> {
const peerId = await createEd25519PeerId()

Expand Down Expand Up @@ -133,4 +132,8 @@ export async function createComponents (listenMaddrs: Multiaddr[] = []): Promise
return components
}

main(argv['run-server'], argv['server-ip-address'], argv['transport'], argv['upload-bytes'], argv['download-bytes'])
main(argv['run-server'], argv['server-ip-address'], argv.transport, argv['upload-bytes'], argv['download-bytes']).catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})
4 changes: 2 additions & 2 deletions packages/perf/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import { start, stop } from '@libp2p/interface/startable'
import { connectionPair } from '@libp2p/interface-compliance-tests/mocks'
import { expect } from 'aegir/chai'
import type { Components } from 'libp2p/components'
import { perfService } from '../src/index.js'
import { perfService } from '../src/index.js'
import { createComponents, defaultInit } from '../src/main.js'
import type { Components } from 'libp2p/components'

describe('perf', () => {
let localComponents: Components
Expand Down

0 comments on commit ccd5f26

Please sign in to comment.