|
| 1 | +/** |
| 2 | + * @packageDocumentation |
| 3 | + * |
| 4 | + * The `performanceService` implements the [perf protocol](https://github.com/libp2p/specs/blob/master/perf/perf.md), which is used to measure performance within and across libp2p implementations |
| 5 | + * addresses. |
| 6 | + * |
| 7 | + * @example |
| 8 | + * |
| 9 | + * ```typescript |
| 10 | + * import { createLibp2p } from 'libp2p' |
| 11 | + * import { perfService } from '@libp2p/perf' |
| 12 | + * |
| 13 | + * const node = await createLibp2p({ |
| 14 | + * service: [ |
| 15 | + * perfService() |
| 16 | + * ] |
| 17 | + * }) |
| 18 | + * ``` |
| 19 | + * |
| 20 | + * The `measurePerformance` function can be used to measure the latency and throughput of a connection. |
| 21 | + * server. This will not work in browsers. |
| 22 | + * |
| 23 | + * @example |
| 24 | + * |
| 25 | + * ```typescript |
| 26 | + * import { createLibp2p } from 'libp2p' |
| 27 | + * import { perfService } from 'libp2p/perf' |
| 28 | + * |
| 29 | + * const node = await createLibp2p({ |
| 30 | + * services: [ |
| 31 | + * perf: perfService() |
| 32 | + * ] |
| 33 | + * }) |
| 34 | + * |
| 35 | + * const connection = await node.dial(multiaddr(multiaddrAddress)) |
| 36 | + * |
| 37 | + * const startTime = Date.now() |
| 38 | + * |
| 39 | + * await node.services.perf.measurePerformance(startTime, connection, BigInt(uploadBytes), BigInt(downloadBytes)) |
| 40 | + * |
| 41 | + * ``` |
| 42 | + */ |
| 43 | + |
| 44 | +import { logger } from '@libp2p/logger' |
| 45 | +import { PROTOCOL_NAME, WRITE_BLOCK_SIZE } from './constants.js' |
| 46 | +import type { Connection } from '@libp2p/interface/connection' |
| 47 | +import type { Startable } from '@libp2p/interface/startable' |
| 48 | +import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager' |
| 49 | +import type { IncomingStreamData, Registrar } from '@libp2p/interface-internal/registrar' |
| 50 | +import type { AbortOptions } from '@libp2p/interfaces' |
| 51 | + |
| 52 | +const log = logger('libp2p:perf') |
| 53 | + |
| 54 | +export const defaultInit: PerfServiceInit = { |
| 55 | + protocolName: '/perf/1.0.0', |
| 56 | + writeBlockSize: BigInt(64 << 10) |
| 57 | +} |
| 58 | + |
| 59 | +export interface PerfService { |
| 60 | + measurePerformance: (startTime: number, connection: Connection, sendBytes: bigint, recvBytes: bigint, options?: AbortOptions) => Promise<number> |
| 61 | +} |
| 62 | + |
| 63 | +export interface PerfServiceInit { |
| 64 | + protocolName?: string |
| 65 | + maxInboundStreams?: number |
| 66 | + maxOutboundStreams?: number |
| 67 | + timeout?: number |
| 68 | + writeBlockSize?: bigint |
| 69 | +} |
| 70 | + |
| 71 | +export interface PerfServiceComponents { |
| 72 | + registrar: Registrar |
| 73 | + connectionManager: ConnectionManager |
| 74 | +} |
| 75 | + |
| 76 | +class DefaultPerfService implements Startable, PerfService { |
| 77 | + public readonly protocol: string |
| 78 | + private readonly components: PerfServiceComponents |
| 79 | + private started: boolean |
| 80 | + private readonly databuf: ArrayBuffer |
| 81 | + private readonly writeBlockSize: bigint |
| 82 | + |
| 83 | + constructor (components: PerfServiceComponents, init: PerfServiceInit) { |
| 84 | + this.components = components |
| 85 | + this.started = false |
| 86 | + this.protocol = init.protocolName ?? PROTOCOL_NAME |
| 87 | + this.writeBlockSize = init.writeBlockSize ?? WRITE_BLOCK_SIZE |
| 88 | + this.databuf = new ArrayBuffer(Number(init.writeBlockSize)) |
| 89 | + } |
| 90 | + |
| 91 | + async start (): Promise<void> { |
| 92 | + await this.components.registrar.handle(this.protocol, (data: IncomingStreamData) => { |
| 93 | + void this.handleMessage(data).catch((err) => { |
| 94 | + log.error('error handling perf protocol message', err) |
| 95 | + }) |
| 96 | + }) |
| 97 | + this.started = true |
| 98 | + } |
| 99 | + |
| 100 | + async stop (): Promise<void> { |
| 101 | + await this.components.registrar.unhandle(this.protocol) |
| 102 | + this.started = false |
| 103 | + } |
| 104 | + |
| 105 | + isStarted (): boolean { |
| 106 | + return this.started |
| 107 | + } |
| 108 | + |
| 109 | + async handleMessage (data: IncomingStreamData): Promise<void> { |
| 110 | + const { stream } = data |
| 111 | + |
| 112 | + const writeBlockSize = this.writeBlockSize |
| 113 | + |
| 114 | + let bytesToSendBack: bigint | null = null |
| 115 | + |
| 116 | + for await (const buf of stream.source) { |
| 117 | + if (bytesToSendBack === null) { |
| 118 | + bytesToSendBack = BigInt(buf.getBigUint64(0, false)) |
| 119 | + } |
| 120 | + // Ingest all the bufs and wait for the read side to close |
| 121 | + } |
| 122 | + |
| 123 | + const uint8Buf = new Uint8Array(this.databuf) |
| 124 | + |
| 125 | + if (bytesToSendBack === null) { |
| 126 | + throw new Error('bytesToSendBack was not set') |
| 127 | + } |
| 128 | + |
| 129 | + await stream.sink(async function * () { |
| 130 | + while (bytesToSendBack > 0n) { |
| 131 | + let toSend: bigint = writeBlockSize |
| 132 | + if (toSend > bytesToSendBack) { |
| 133 | + toSend = bytesToSendBack |
| 134 | + } |
| 135 | + bytesToSendBack = bytesToSendBack - toSend |
| 136 | + yield uint8Buf.subarray(0, Number(toSend)) |
| 137 | + } |
| 138 | + }()) |
| 139 | + } |
| 140 | + |
| 141 | + async measurePerformance (startTime: number, connection: Connection, sendBytes: bigint, recvBytes: bigint, options: AbortOptions = {}): Promise<number> { |
| 142 | + log('opening stream on protocol %s to %p', this.protocol, connection.remotePeer) |
| 143 | + |
| 144 | + const uint8Buf = new Uint8Array(this.databuf) |
| 145 | + |
| 146 | + const writeBlockSize = this.writeBlockSize |
| 147 | + |
| 148 | + const stream = await connection.newStream([this.protocol]) |
| 149 | + |
| 150 | + // Convert sendBytes to uint64 big endian buffer |
| 151 | + const view = new DataView(this.databuf) |
| 152 | + view.setBigInt64(0, recvBytes, false) |
| 153 | + |
| 154 | + log('sending %i bytes to %p', sendBytes, connection.remotePeer) |
| 155 | + try { |
| 156 | + await stream.sink((async function * () { |
| 157 | + // Send the number of bytes to receive |
| 158 | + yield uint8Buf.subarray(0, 8) |
| 159 | + // Send the number of bytes to send |
| 160 | + while (sendBytes > 0n) { |
| 161 | + let toSend: bigint = writeBlockSize |
| 162 | + if (toSend > sendBytes) { |
| 163 | + toSend = sendBytes |
| 164 | + } |
| 165 | + sendBytes = sendBytes - toSend |
| 166 | + yield uint8Buf.subarray(0, Number(toSend)) |
| 167 | + } |
| 168 | + })()) |
| 169 | + |
| 170 | + // Read the received bytes |
| 171 | + let actualRecvdBytes = BigInt(0) |
| 172 | + for await (const buf of stream.source) { |
| 173 | + actualRecvdBytes += BigInt(buf.length) |
| 174 | + } |
| 175 | + |
| 176 | + if (actualRecvdBytes !== recvBytes) { |
| 177 | + throw new Error(`Expected to receive ${recvBytes} bytes, but received ${actualRecvdBytes}`) |
| 178 | + } |
| 179 | + } catch (err) { |
| 180 | + log('error sending %i bytes to %p: %s', sendBytes, connection.remotePeer, err) |
| 181 | + throw err |
| 182 | + } finally { |
| 183 | + log('performed %s to %p', this.protocol, connection.remotePeer) |
| 184 | + await stream.close() |
| 185 | + } |
| 186 | + |
| 187 | + // Return the latency |
| 188 | + return Date.now() - startTime |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +export function perfService (init: PerfServiceInit = {}): (components: PerfServiceComponents) => PerfService { |
| 193 | + return (components) => new DefaultPerfService(components, init) |
| 194 | +} |
0 commit comments