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

refactor!: rename perf exports to remove Service #2227

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions doc/migrations/v0.46-v1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A migration guide for refactoring your application code from libp2p `v0.46` to `
- [Fetch](#fetch)
- [KeyChain](#keychain)
- [UPnPNat](#upnpnat)
- [Perf](#perf)
- [Plaintext](#plaintext)
- [Pnet](#pnet)
- [Metrics](#metrics)
Expand Down Expand Up @@ -225,6 +226,37 @@ const node = await createLibp2p({
})
```

## Perf

The Perf service module exports have been renamed in line with the other changes
here.

**Before**

```ts
import { createLibp2p } from 'libp2p'
import { perService } from '@libp2p/perf'

const node = await createLibp2p({
services: {
perf: perService()
}
})
```

**After**

```ts
import { createLibp2p } from 'libp2p'
import { perf } from '@libp2p/perf'

const node = await createLibp2p({
services: {
perf: perf()
}
})
```

## Plaintext

The Plaintext connection encrypter module is now published in its own package.
Expand Down
6 changes: 3 additions & 3 deletions packages/protocol-perf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { mplex } from '@libp2p/mplex'
import { tcp } from '@libp2p/tcp'
import { createLibp2p, type Libp2p } from 'libp2p'
import { plaintext } from '@libp2p/plaintext'
import { perfService, type PerfService } from '@libp2p/perf'
import { perf, type Perf } from '@libp2p/perf'

const ONE_MEG = 1024 * 1024
const UPLOAD_BYTES = ONE_MEG * 1024
const DOWNLOAD_BYTES = ONE_MEG * 1024

async function createNode (): Promise<Libp2p<{ perf: PerfService }>> {
async function createNode (): Promise<Libp2p<{ perf: Perf }>> {
return createLibp2p({
addresses: {
listen: [
Expand All @@ -41,7 +41,7 @@ async function createNode (): Promise<Libp2p<{ perf: PerfService }>> {
yamux(), mplex()
],
services: {
perf: perfService()
perf: perf()
}
})
}
Expand Down
18 changes: 9 additions & 9 deletions packages/protocol-perf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
* import { tcp } from '@libp2p/tcp'
* import { createLibp2p, type Libp2p } from 'libp2p'
* import { plaintext } from '@libp2p/plaintext'
* import { perfService, type PerfService } from '@libp2p/perf'
* import { perf, type Perf } from '@libp2p/perf'
*
* const ONE_MEG = 1024 * 1024
* const UPLOAD_BYTES = ONE_MEG * 1024
* const DOWNLOAD_BYTES = ONE_MEG * 1024
*
* async function createNode (): Promise<Libp2p<{ perf: PerfService }>> {
* async function createNode (): Promise<Libp2p<{ perf: Perf }>> {
* return createLibp2p({
* addresses: {
* listen: [
Expand All @@ -35,7 +35,7 @@
* yamux(), mplex()
* ],
* services: {
* perf: perfService()
* perf: perf()
* }
* })
* }
Expand All @@ -52,7 +52,7 @@
* ```
*/

import { PerfService as PerfServiceClass } from './perf-service.js'
import { Perf as PerfClass } from './perf-service.js'
import type { AbortOptions } from '@libp2p/interface'
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
import type { Registrar } from '@libp2p/interface-internal/registrar'
Expand All @@ -69,7 +69,7 @@ export interface PerfOptions extends AbortOptions {
reuseExistingConnection?: boolean
}

export interface PerfService {
export interface Perf {
measurePerformance(multiaddr: Multiaddr, sendBytes: number, recvBytes: number, options?: PerfOptions): AsyncGenerator<PerfOutput>
}

Expand All @@ -80,7 +80,7 @@ export interface PerfOutput {
downloadBytes: number
}

export interface PerfServiceInit {
export interface PerfInit {
protocolName?: string
maxInboundStreams?: number
maxOutboundStreams?: number
Expand All @@ -92,11 +92,11 @@ export interface PerfServiceInit {
writeBlockSize?: number
}

export interface PerfServiceComponents {
export interface PerfComponents {
registrar: Registrar
connectionManager: ConnectionManager
}

export function perfService (init: PerfServiceInit = {}): (components: PerfServiceComponents) => PerfService {
return (components) => new PerfServiceClass(components, init)
export function perf (init: PerfInit = {}): (components: PerfComponents) => Perf {
return (components) => new PerfClass(components, init)
}
8 changes: 4 additions & 4 deletions packages/protocol-perf/src/perf-service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { logger } from '@libp2p/logger'
import { pushable } from 'it-pushable'
import { MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, RUN_ON_TRANSIENT_CONNECTION, WRITE_BLOCK_SIZE } from './constants.js'
import type { PerfOptions, PerfOutput, PerfServiceComponents, PerfServiceInit, PerfService as PerfServiceInterface } from './index.js'
import type { PerfOptions, PerfOutput, PerfComponents, PerfInit, Perf as PerfInterface } from './index.js'
import type { Startable } from '@libp2p/interface/startable'
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
import type { Multiaddr } from '@multiformats/multiaddr'

const log = logger('libp2p:perf')

export class PerfService implements Startable, PerfServiceInterface {
export class Perf implements Startable, PerfInterface {
public readonly protocol: string
private readonly components: PerfServiceComponents
private readonly components: PerfComponents
private started: boolean
private readonly databuf: ArrayBuffer
private readonly writeBlockSize: number
private readonly maxInboundStreams: number
private readonly maxOutboundStreams: number
private readonly runOnTransientConnection: boolean

constructor (components: PerfServiceComponents, init: PerfServiceInit = {}) {
constructor (components: PerfComponents, init: PerfInit = {}) {
this.components = components
this.started = false
this.protocol = init.protocolName ?? PROTOCOL_NAME
Expand Down
18 changes: 9 additions & 9 deletions packages/protocol-perf/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import { expect } from 'aegir/chai'
import last from 'it-last'
import { duplexPair } from 'it-pair/duplex'
import { stubInterface, type StubbedInstance } from 'sinon-ts'
import { PerfService } from '../src/perf-service.js'
import { Perf } from '../src/perf-service.js'
import type { Connection } from '@libp2p/interface/connection'
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
import type { Registrar } from '@libp2p/interface-internal/registrar'

interface StubbedPerfServiceComponents {
interface StubbedPerfComponents {
registrar: StubbedInstance<Registrar>
connectionManager: StubbedInstance<ConnectionManager>
}

export function createComponents (): StubbedPerfServiceComponents {
export function createComponents (): StubbedPerfComponents {
return {
registrar: stubInterface<Registrar>(),
connectionManager: stubInterface<ConnectionManager>()
}
}

describe('perf', () => {
let localComponents: StubbedPerfServiceComponents
let remoteComponents: StubbedPerfServiceComponents
let localComponents: StubbedPerfComponents
let remoteComponents: StubbedPerfComponents

beforeEach(async () => {
localComponents = createComponents()
Expand All @@ -46,8 +46,8 @@ describe('perf', () => {
})

it('should run perf', async () => {
const client = new PerfService(localComponents)
const server = new PerfService(remoteComponents)
const client = new Perf(localComponents)
const server = new Perf(remoteComponents)

await start(client)
await start(server)
Expand Down Expand Up @@ -77,8 +77,8 @@ describe('perf', () => {
})

it('should reuse existing connection', async () => {
const client = new PerfService(localComponents)
const server = new PerfService(remoteComponents)
const client = new Perf(localComponents)
const server = new Perf(remoteComponents)

await start(client)
await start(server)
Expand Down
Loading