From 8ba8db32926de830078c100551c939c746e2966d Mon Sep 17 00:00:00 2001 From: chad Date: Sun, 30 Jul 2023 12:15:19 -0500 Subject: [PATCH] refactor: refactored main runner --- packages/libp2p/package.json | 4 +++ packages/perf/src/index.ts | 8 +++++ packages/perf/src/main.ts | 52 ++------------------------------ packages/perf/test/index.spec.ts | 46 +++++++++++++++++++++++++--- 4 files changed, 57 insertions(+), 53 deletions(-) diff --git a/packages/libp2p/package.json b/packages/libp2p/package.json index 0de9617d03..b5e89939d3 100644 --- a/packages/libp2p/package.json +++ b/packages/libp2p/package.json @@ -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" diff --git a/packages/perf/src/index.ts b/packages/perf/src/index.ts index 8ed9261a86..a52a4b2e1b 100644 --- a/packages/perf/src/index.ts +++ b/packages/perf/src/index.ts @@ -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 measureDownloadBandwidth: (peer: PeerId, size: bigint) => Promise diff --git a/packages/perf/src/main.ts b/packages/perf/src/main.ts index fb9d17ed80..4881874624 100644 --- a/packages/perf/src/main.ts +++ b/packages/perf/src/main.ts @@ -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': { @@ -102,36 +86,6 @@ function splitHostPort (urlString: string): { host: string, port?: string } { } } -export async function createComponents (listenMaddrs: Multiaddr[] = []): Promise { - const peerId = await createEd25519PeerId() - - const events = new EventEmitter() - - const components = defaultComponents({ - peerId, - registrar: mockRegistrar(), - upgrader: mockUpgrader(), - datastore: new MemoryDatastore(), - transportManager: stubInterface(), - 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) diff --git a/packages/perf/test/index.spec.ts b/packages/perf/test/index.spec.ts index 8758c2c0db..aecaa61664 100644 --- a/packages/perf/test/index.spec.ts +++ b/packages/perf/test/index.spec.ts @@ -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 { + const peerId = await createEd25519PeerId() + + const events = new EventEmitter() + + const components = defaultComponents({ + peerId, + registrar: mockRegistrar(), + upgrader: mockUpgrader(), + datastore: new MemoryDatastore(), + transportManager: stubInterface(), + 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