Skip to content

Commit

Permalink
refactor: refactored main runner
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Jul 30, 2023
1 parent ec98ddf commit 8ba8db3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 53 deletions.
4 changes: 4 additions & 0 deletions packages/libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
"types": "./dist/src/autonat/index.d.ts",
"import": "./dist/src/autonat/index.js"
},
"./address-manager": {
"types": "./dist/src/address-manager/index.d.ts",
"import": "./dist/src/address-manager/index.js"
},
"./circuit-relay": {
"types": "./dist/src/circuit-relay/index.d.ts",
"import": "./dist/src/circuit-relay/index.js"
Expand Down
8 changes: 8 additions & 0 deletions packages/perf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import type { AbortOptions } from '@libp2p/interfaces'

const log = logger('libp2p:perf')

export const defaultInit: PerfServiceInit = {
protocolName: '/perf/1.0.0',
maxInboundStreams: 1 << 10,
maxOutboundStreams: 1 << 10,
timeout: 10000,
writeBlockSize: BigInt(64 << 10)
}

export interface PerfService {
perf: (peer: PeerId, sendBytes: bigint, recvBytes: bigint, options?: AbortOptions) => Promise<void>
measureDownloadBandwidth: (peer: PeerId, size: bigint) => Promise<number>
Expand Down
52 changes: 3 additions & 49 deletions packages/perf/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
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 { connectionPair } from '@libp2p/interface-compliance-tests/mocks'
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 { perfService, type PerfServiceInit } from '.'
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
import { defaultInit, perfService } from '../src/index.js'
import { createComponents } from '../test/index.spec.js'
import type { Multiaddr } from '@multiformats/multiaddr'

export const defaultInit: PerfServiceInit = {
protocolName: '/perf/1.0.0',
maxInboundStreams: 1 << 10,
maxOutboundStreams: 1 << 10,
timeout: 10000,
writeBlockSize: BigInt(64 << 10)
}

const argv = yargs
.options({
'run-server': {
Expand Down Expand Up @@ -102,36 +86,6 @@ function splitHostPort (urlString: string): { host: string, port?: string } {
}
}

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

const events = new EventEmitter()

const components = defaultComponents({
peerId,
registrar: mockRegistrar(),
upgrader: mockUpgrader(),
datastore: new MemoryDatastore(),
transportManager: stubInterface<TransportManager>(),
connectionGater: mockConnectionGater(),
events
})

components.peerStore = new PersistentPeerStore(components)
components.connectionManager = new DefaultConnectionManager(components, {
minConnections: 50,
maxConnections: 1000,
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})

components.addressManager = new DefaultAddressManager(components, {
announce: listenMaddrs.map(ma => ma.toString())
})

return components
}

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)
Expand Down
46 changes: 42 additions & 4 deletions packages/perf/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
/* eslint-env mocha */

import { EventEmitter } from '@libp2p/interface/events'
import { start, stop } from '@libp2p/interface/startable'
import { connectionPair } from '@libp2p/interface-compliance-tests/mocks'
import { connectionPair, mockConnectionGater, mockRegistrar, mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { PersistentPeerStore } from '@libp2p/peer-store'
import { expect } from 'aegir/chai'
import { perfService } from '../src/index.js'
import { createComponents, defaultInit } from '../src/main.js'
import type { Components } from 'libp2p/components'
import { MemoryDatastore } from 'datastore-core'
import { DefaultAddressManager } from 'libp2p/address-manager'
import { type Components, defaultComponents } from 'libp2p/components'
import { DefaultConnectionManager } from 'libp2p/connection-manager'
import { stubInterface } from 'sinon-ts'
import { defaultInit, perfService } from '../src/index.js'
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
import type { Multiaddr } from '@multiformats/multiaddr'

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

const events = new EventEmitter()

const components = defaultComponents({
peerId,
registrar: mockRegistrar(),
upgrader: mockUpgrader(),
datastore: new MemoryDatastore(),
transportManager: stubInterface<TransportManager>(),
connectionGater: mockConnectionGater(),
events
})

components.peerStore = new PersistentPeerStore(components)
components.connectionManager = new DefaultConnectionManager(components, {
minConnections: 50,
maxConnections: 1000,
autoDialInterval: 1000,
inboundUpgradeTimeout: 1000
})

components.addressManager = new DefaultAddressManager(components, {
announce: listenMaddrs.map(ma => ma.toString())
})

return components
}

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

0 comments on commit 8ba8db3

Please sign in to comment.