@libp2p/perf 1.1.15 → 2.0.0-fb8a6f188
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -33
- package/dist/index.min.js +1 -1
- package/dist/src/constants.d.ts +4 -1
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +4 -1
- package/dist/src/constants.js.map +1 -1
- package/dist/src/index.d.ts +63 -34
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +46 -138
- package/dist/src/index.js.map +1 -1
- package/dist/src/perf-service.d.ts +21 -0
- package/dist/src/perf-service.d.ts.map +1 -0
- package/dist/src/perf-service.js +169 -0
- package/dist/src/perf-service.js.map +1 -0
- package/package.json +11 -16
- package/src/constants.ts +4 -1
- package/src/index.ts +68 -160
- package/src/perf-service.ts +207 -0
- package/dist/src/main.d.ts +0 -2
- package/dist/src/main.d.ts.map +0 -1
- package/dist/src/main.js +0 -107
- package/dist/src/main.js.map +0 -1
- package/dist/typedoc-urls.json +0 -12
- package/src/main.ts +0 -118
@@ -0,0 +1,207 @@
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
2
|
+
import { pushable } from 'it-pushable'
|
3
|
+
import { MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS, PROTOCOL_NAME, RUN_ON_TRANSIENT_CONNECTION, WRITE_BLOCK_SIZE } from './constants.js'
|
4
|
+
import type { PerfOptions, PerfOutput, PerfServiceComponents, PerfServiceInit, PerfService as PerfServiceInterface } from './index.js'
|
5
|
+
import type { Startable } from '@libp2p/interface/startable'
|
6
|
+
import type { IncomingStreamData } from '@libp2p/interface-internal/registrar'
|
7
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
8
|
+
|
9
|
+
const log = logger('libp2p:perf')
|
10
|
+
|
11
|
+
export class PerfService implements Startable, PerfServiceInterface {
|
12
|
+
public readonly protocol: string
|
13
|
+
private readonly components: PerfServiceComponents
|
14
|
+
private started: boolean
|
15
|
+
private readonly databuf: ArrayBuffer
|
16
|
+
private readonly writeBlockSize: number
|
17
|
+
private readonly maxInboundStreams: number
|
18
|
+
private readonly maxOutboundStreams: number
|
19
|
+
private readonly runOnTransientConnection: boolean
|
20
|
+
|
21
|
+
constructor (components: PerfServiceComponents, init: PerfServiceInit = {}) {
|
22
|
+
this.components = components
|
23
|
+
this.started = false
|
24
|
+
this.protocol = init.protocolName ?? PROTOCOL_NAME
|
25
|
+
this.writeBlockSize = init.writeBlockSize ?? WRITE_BLOCK_SIZE
|
26
|
+
this.databuf = new ArrayBuffer(this.writeBlockSize)
|
27
|
+
this.maxInboundStreams = init.maxInboundStreams ?? MAX_INBOUND_STREAMS
|
28
|
+
this.maxOutboundStreams = init.maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
|
29
|
+
this.runOnTransientConnection = init.runOnTransientConnection ?? RUN_ON_TRANSIENT_CONNECTION
|
30
|
+
}
|
31
|
+
|
32
|
+
async start (): Promise<void> {
|
33
|
+
await this.components.registrar.handle(this.protocol, (data: IncomingStreamData) => {
|
34
|
+
void this.handleMessage(data).catch((err) => {
|
35
|
+
log.error('error handling perf protocol message', err)
|
36
|
+
})
|
37
|
+
}, {
|
38
|
+
maxInboundStreams: this.maxInboundStreams,
|
39
|
+
maxOutboundStreams: this.maxOutboundStreams,
|
40
|
+
runOnTransientConnection: this.runOnTransientConnection
|
41
|
+
})
|
42
|
+
this.started = true
|
43
|
+
}
|
44
|
+
|
45
|
+
async stop (): Promise<void> {
|
46
|
+
await this.components.registrar.unhandle(this.protocol)
|
47
|
+
this.started = false
|
48
|
+
}
|
49
|
+
|
50
|
+
isStarted (): boolean {
|
51
|
+
return this.started
|
52
|
+
}
|
53
|
+
|
54
|
+
async handleMessage (data: IncomingStreamData): Promise<void> {
|
55
|
+
const { stream } = data
|
56
|
+
|
57
|
+
try {
|
58
|
+
const writeBlockSize = this.writeBlockSize
|
59
|
+
|
60
|
+
let bytesToSendBack: number | undefined
|
61
|
+
|
62
|
+
for await (const buf of stream.source) {
|
63
|
+
if (bytesToSendBack == null) {
|
64
|
+
// downcast 64 to 52 bits to avoid bigint arithmetic performance penalty
|
65
|
+
bytesToSendBack = Number(buf.getBigUint64(0, false))
|
66
|
+
}
|
67
|
+
// Ingest all the bufs and wait for the read side to close
|
68
|
+
}
|
69
|
+
|
70
|
+
if (bytesToSendBack == null) {
|
71
|
+
throw new Error('bytesToSendBack was not set')
|
72
|
+
}
|
73
|
+
|
74
|
+
const uint8Buf = new Uint8Array(this.databuf)
|
75
|
+
|
76
|
+
await stream.sink(async function * () {
|
77
|
+
while (bytesToSendBack > 0) {
|
78
|
+
let toSend: number = writeBlockSize
|
79
|
+
if (toSend > bytesToSendBack) {
|
80
|
+
toSend = bytesToSendBack
|
81
|
+
}
|
82
|
+
|
83
|
+
bytesToSendBack = bytesToSendBack - toSend
|
84
|
+
yield uint8Buf.subarray(0, toSend)
|
85
|
+
}
|
86
|
+
}())
|
87
|
+
} catch (err: any) {
|
88
|
+
stream.abort(err)
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
async * measurePerformance (ma: Multiaddr, sendBytes: number, receiveBytes: number, options: PerfOptions = {}): AsyncGenerator<PerfOutput> {
|
93
|
+
log('opening stream on protocol %s to %a', this.protocol, ma)
|
94
|
+
|
95
|
+
const uint8Buf = new Uint8Array(this.databuf)
|
96
|
+
const writeBlockSize = this.writeBlockSize
|
97
|
+
|
98
|
+
// start time should include connection establishment
|
99
|
+
const initialStartTime = Date.now()
|
100
|
+
const connection = await this.components.connectionManager.openConnection(ma, {
|
101
|
+
...options,
|
102
|
+
force: options.reuseExistingConnection !== true
|
103
|
+
})
|
104
|
+
const stream = await connection.newStream(this.protocol, options)
|
105
|
+
|
106
|
+
let lastAmountOfBytesSent = 0
|
107
|
+
let lastReportedTime = Date.now()
|
108
|
+
let totalBytesSent = 0
|
109
|
+
|
110
|
+
// tell the remote how many bytes we will send. Up cast to 64 bit number
|
111
|
+
// as if we send as ui32 we limit total transfer size to 4GB
|
112
|
+
const view = new DataView(this.databuf)
|
113
|
+
view.setBigUint64(0, BigInt(receiveBytes), false)
|
114
|
+
|
115
|
+
log('sending %i bytes to %p', sendBytes, connection.remotePeer)
|
116
|
+
|
117
|
+
try {
|
118
|
+
const sendOutput = pushable<PerfOutput>({
|
119
|
+
objectMode: true
|
120
|
+
})
|
121
|
+
|
122
|
+
void stream.sink(async function * () {
|
123
|
+
// Send the number of bytes to receive
|
124
|
+
yield uint8Buf.subarray(0, 8)
|
125
|
+
|
126
|
+
while (sendBytes > 0) {
|
127
|
+
options.signal?.throwIfAborted()
|
128
|
+
|
129
|
+
let toSend: number = writeBlockSize
|
130
|
+
if (toSend > sendBytes) {
|
131
|
+
toSend = sendBytes
|
132
|
+
}
|
133
|
+
sendBytes = sendBytes - toSend
|
134
|
+
yield uint8Buf.subarray(0, toSend)
|
135
|
+
|
136
|
+
if (Date.now() - lastReportedTime > 1000) {
|
137
|
+
sendOutput.push({
|
138
|
+
type: 'intermediary',
|
139
|
+
timeSeconds: (Date.now() - lastReportedTime) / 1000,
|
140
|
+
uploadBytes: lastAmountOfBytesSent,
|
141
|
+
downloadBytes: 0
|
142
|
+
})
|
143
|
+
|
144
|
+
// record last reported time after `console.log` because it can
|
145
|
+
// affect benchmark timings
|
146
|
+
lastReportedTime = Date.now()
|
147
|
+
lastAmountOfBytesSent = 0
|
148
|
+
}
|
149
|
+
|
150
|
+
lastAmountOfBytesSent += toSend
|
151
|
+
totalBytesSent += toSend
|
152
|
+
}
|
153
|
+
|
154
|
+
sendOutput.end()
|
155
|
+
}())
|
156
|
+
.catch(err => {
|
157
|
+
sendOutput.end(err)
|
158
|
+
})
|
159
|
+
|
160
|
+
yield * sendOutput
|
161
|
+
|
162
|
+
// Read the received bytes
|
163
|
+
let lastAmountOfBytesReceived = 0
|
164
|
+
lastReportedTime = Date.now()
|
165
|
+
let totalBytesReceived = 0
|
166
|
+
|
167
|
+
for await (const buf of stream.source) {
|
168
|
+
options.signal?.throwIfAborted()
|
169
|
+
|
170
|
+
if (Date.now() - lastReportedTime > 1000) {
|
171
|
+
yield {
|
172
|
+
type: 'intermediary',
|
173
|
+
timeSeconds: (Date.now() - lastReportedTime) / 1000,
|
174
|
+
uploadBytes: 0,
|
175
|
+
downloadBytes: lastAmountOfBytesReceived
|
176
|
+
}
|
177
|
+
|
178
|
+
// record last reported time after `console.log` because it can
|
179
|
+
// affect benchmark timings
|
180
|
+
lastReportedTime = Date.now()
|
181
|
+
lastAmountOfBytesReceived = 0
|
182
|
+
}
|
183
|
+
|
184
|
+
lastAmountOfBytesReceived += buf.byteLength
|
185
|
+
totalBytesReceived += buf.byteLength
|
186
|
+
}
|
187
|
+
|
188
|
+
if (totalBytesReceived !== receiveBytes) {
|
189
|
+
throw new Error(`Expected to receive ${receiveBytes} bytes, but received ${totalBytesReceived}`)
|
190
|
+
}
|
191
|
+
|
192
|
+
yield {
|
193
|
+
type: 'final',
|
194
|
+
timeSeconds: (Date.now() - initialStartTime) / 1000,
|
195
|
+
uploadBytes: totalBytesSent,
|
196
|
+
downloadBytes: totalBytesReceived
|
197
|
+
}
|
198
|
+
|
199
|
+
log('performed %s to %p', this.protocol, connection.remotePeer)
|
200
|
+
await stream.close()
|
201
|
+
} catch (err: any) {
|
202
|
+
log('error sending %s bytes to %p: %s', totalBytesSent, connection.remotePeer, err)
|
203
|
+
stream.abort(err)
|
204
|
+
throw err
|
205
|
+
}
|
206
|
+
}
|
207
|
+
}
|
package/dist/src/main.d.ts
DELETED
package/dist/src/main.d.ts.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/main.ts"],"names":[],"mappings":"AAgDA,wBAAsB,IAAI,CAAE,SAAS,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAiDrJ"}
|
package/dist/src/main.js
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
import { noise } from '@chainsafe/libp2p-noise';
|
2
|
-
import { yamux } from '@chainsafe/libp2p-yamux';
|
3
|
-
import { unmarshalPrivateKey } from '@libp2p/crypto/keys';
|
4
|
-
import { createFromPrivKey } from '@libp2p/peer-id-factory';
|
5
|
-
import { tcp } from '@libp2p/tcp';
|
6
|
-
import { multiaddr } from '@multiformats/multiaddr';
|
7
|
-
import { createLibp2p } from 'libp2p';
|
8
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
9
|
-
import yargs from 'yargs';
|
10
|
-
import { hideBin } from 'yargs/helpers';
|
11
|
-
import { defaultInit, perfService } from '../src/index.js';
|
12
|
-
const argv = yargs(hideBin(process.argv))
|
13
|
-
.options({
|
14
|
-
'run-server': {
|
15
|
-
type: 'boolean',
|
16
|
-
demandOption: true,
|
17
|
-
default: false,
|
18
|
-
description: 'Whether to run as a server'
|
19
|
-
},
|
20
|
-
'server-address': {
|
21
|
-
type: 'string',
|
22
|
-
demandOption: false,
|
23
|
-
description: 'Server IP address',
|
24
|
-
default: ''
|
25
|
-
},
|
26
|
-
transport: {
|
27
|
-
type: 'string',
|
28
|
-
demandOption: false,
|
29
|
-
description: 'Transport to use',
|
30
|
-
default: 'tcp'
|
31
|
-
},
|
32
|
-
'upload-bytes': {
|
33
|
-
type: 'number',
|
34
|
-
demandOption: false,
|
35
|
-
description: 'Number of bytes to upload',
|
36
|
-
default: 0
|
37
|
-
},
|
38
|
-
'download-bytes': {
|
39
|
-
type: 'number',
|
40
|
-
demandOption: false,
|
41
|
-
description: 'Number of bytes to download',
|
42
|
-
default: 0
|
43
|
-
}
|
44
|
-
})
|
45
|
-
.command('help', 'Print usage information', yargs.help)
|
46
|
-
.parseSync();
|
47
|
-
export async function main(runServer, serverIpAddress, transport, uploadBytes, downloadBytes) {
|
48
|
-
const listenAddrs = [];
|
49
|
-
const { host, port } = splitHostPort(serverIpAddress);
|
50
|
-
// #TODO: right now we only support tcp
|
51
|
-
const tcpMultiaddr = multiaddr(`/ip4/${host}/tcp/${port}`);
|
52
|
-
const config = {
|
53
|
-
transports: [tcp()],
|
54
|
-
streamMuxers: [yamux()],
|
55
|
-
connectionEncryption: [
|
56
|
-
noise()
|
57
|
-
],
|
58
|
-
services: {
|
59
|
-
perf: perfService(defaultInit)
|
60
|
-
}
|
61
|
-
};
|
62
|
-
const testPrivKey = 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8Epdx+7F5p80XAFLDUnELzOzKftvWGZmWfSeDnslwVONkL/1VAzwKy7Ce6hk4SxRE7l2NE2OklSHOzCGU1f78ZzVYKSnS5Ag9YrGjOAmTOXDbKNKN/qIorAQ1bovzGoCwx3iGIatQKFOxyVCyO1PsJYT7JO+kZbhBWRRE+L7l+ppPER9bdLFxs1t5CrKc078h+wuUr05S1P1JjXk68pk3+kQKBgQDeK8AR11373Mzib6uzpjGzgNRMzdYNuExWjxyxAzz53NAR7zrPHvXvfIqjDScLJ4NcRO2TddhXAfZoOPVH5k4PJHKLBPKuXZpWlookCAyENY7+Pd55S8r+a+MusrMagYNljb5WbVTgN8cgdpim9lbbIFlpN6SZaVjLQL3J8TWH6wKBgQDSChzItkqWX11CNstJ9zJyUE20I7LrpyBJNgG1gtvz3ZMUQCn3PxxHtQzN9n1P0mSSYs+jBKPuoSyYLt1wwe10/lpgL4rkKWU3/m1Myt0tveJ9WcqHh6tzcAbb/fXpUFT/o4SWDimWkPkuCb+8j//2yiXk0a/T2f36zKMuZvujqQKBgC6B7BAQDG2H2B/ijofp12ejJU36nL98gAZyqOfpLJ+FeMz4TlBDQ+phIMhnHXA5UkdDapQ+zA3SrFk+6yGk9Vw4Hf46B+82SvOrSbmnMa+PYqKYIvUzR4gg34rL/7AhwnbEyD5hXq4dHwMNsIDq+l2elPjwm/U9V0gdAl2+r50HAoGALtsKqMvhv8HucAMBPrLikhXP/8um8mMKFMrzfqZ+otxfHzlhI0L08Bo3jQrb0Z7ByNY6M8epOmbCKADsbWcVre/AAY0ZkuSZK/CaOXNX/AhMKmKJh8qAOPRY02LIJRBCpfS4czEdnfUhYV/TYiFNnKRj57PPYZdTzUsxa/yVTmECgYBr7slQEjb5Onn5mZnGDh+72BxLNdgwBkhO0OCdpdISqk0F0Pxby22DFOKXZEpiyI9XYP1C8wPiJsShGm2yEwBPWXnrrZNWczaVuCbXHrZkWQogBDG3HGXNdU4MAWCyiYlyinIBpPpoAJZSzpGLmWbMWh28+RJS6AQX6KHrK1o2uw==';
|
63
|
-
const encoded = uint8ArrayFromString(testPrivKey, 'base64pad');
|
64
|
-
const privateKey = await unmarshalPrivateKey(encoded);
|
65
|
-
const peerId = await createFromPrivKey(privateKey);
|
66
|
-
const tcpMultiaddrAddress = `${tcpMultiaddr.toString()}/p2p/${peerId.toString()}`;
|
67
|
-
if (runServer) {
|
68
|
-
listenAddrs.push(tcpMultiaddrAddress);
|
69
|
-
Object.assign(config, {
|
70
|
-
peerId,
|
71
|
-
addresses: {
|
72
|
-
listen: listenAddrs
|
73
|
-
}
|
74
|
-
});
|
75
|
-
}
|
76
|
-
const node = await createLibp2p(config);
|
77
|
-
await node.start();
|
78
|
-
const startTime = Date.now();
|
79
|
-
if (!runServer) {
|
80
|
-
const connection = await node.dial(multiaddr(tcpMultiaddrAddress));
|
81
|
-
const duration = await node.services.perf.measurePerformance(startTime, connection, BigInt(uploadBytes), BigInt(downloadBytes));
|
82
|
-
// Output latency to stdout in seconds
|
83
|
-
// eslint-disable-next-line no-console
|
84
|
-
console.log(JSON.stringify({ latency: duration / 1000 }));
|
85
|
-
await node.stop();
|
86
|
-
}
|
87
|
-
}
|
88
|
-
function splitHostPort(address) {
|
89
|
-
try {
|
90
|
-
const parts = address.split(':');
|
91
|
-
const host = parts[0];
|
92
|
-
const port = parts[1];
|
93
|
-
return {
|
94
|
-
host,
|
95
|
-
port
|
96
|
-
};
|
97
|
-
}
|
98
|
-
catch (error) {
|
99
|
-
throw Error('Invalid server address');
|
100
|
-
}
|
101
|
-
}
|
102
|
-
main(argv['run-server'], argv['server-address'], argv.transport, argv['upload-bytes'], argv['download-bytes']).catch((err) => {
|
103
|
-
// eslint-disable-next-line no-console
|
104
|
-
console.error(err);
|
105
|
-
process.exit(1);
|
106
|
-
});
|
107
|
-
//# sourceMappingURL=main.js.map
|
package/dist/src/main.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AAC/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAA;AAC3D,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AACrC,OAAO,EAAE,UAAU,IAAI,oBAAo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CAAC,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC,CAAA;QAC/H,sCAAsC;QACtC,sCAAsC;QACtC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,QAAQ,GAAG,IAAI,EAAE,CAAC,CAAC,CAAA;QACzD,MAAM,IAAI,CAAC,IAAI,EAAE,CAAA;KAClB;AACH,CAAC;AAED,SAAS,aAAa,CAAE,OAAe;IACrC,IAAI;QACF,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QAChC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;QACrB,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;QACrB,OAAO;YACL,IAAI;YACJ,IAAI;SACL,CAAA;KACF;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,KAAK,CAAC,wBAAwB,CAAC,CAAA;KACtC;AACH,CAAC;AAED,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,IAAI,CAAC,gBAAgB,CAAC,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,cAAc,CAAC,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IAC3H,sCAAsC;IACtC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAClB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;AACjB,CAAC,CAAC,CAAA"}
|
package/dist/typedoc-urls.json
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"PerfService": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfService.html",
|
3
|
-
".:PerfService": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfService.html",
|
4
|
-
"PerfServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfServiceComponents.html",
|
5
|
-
".:PerfServiceComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfServiceComponents.html",
|
6
|
-
"PerfServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfServiceInit.html",
|
7
|
-
".:PerfServiceInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_perf.PerfServiceInit.html",
|
8
|
-
"defaultInit": "https://libp2p.github.io/js-libp2p/variables/_libp2p_perf.defaultInit.html",
|
9
|
-
".:defaultInit": "https://libp2p.github.io/js-libp2p/variables/_libp2p_perf.defaultInit.html",
|
10
|
-
"perfService": "https://libp2p.github.io/js-libp2p/functions/_libp2p_perf.perfService-1.html",
|
11
|
-
".:perfService": "https://libp2p.github.io/js-libp2p/functions/_libp2p_perf.perfService-1.html"
|
12
|
-
}
|
package/src/main.ts
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
import { noise } from '@chainsafe/libp2p-noise'
|
2
|
-
import { yamux } from '@chainsafe/libp2p-yamux'
|
3
|
-
import { unmarshalPrivateKey } from '@libp2p/crypto/keys'
|
4
|
-
import { createFromPrivKey } from '@libp2p/peer-id-factory'
|
5
|
-
import { tcp } from '@libp2p/tcp'
|
6
|
-
import { multiaddr } from '@multiformats/multiaddr'
|
7
|
-
import { createLibp2p } from 'libp2p'
|
8
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
9
|
-
import yargs from 'yargs'
|
10
|
-
import { hideBin } from 'yargs/helpers'
|
11
|
-
import { defaultInit, perfService } from '../src/index.js'
|
12
|
-
|
13
|
-
const argv = yargs(hideBin(process.argv))
|
14
|
-
.options({
|
15
|
-
'run-server': {
|
16
|
-
type: 'boolean',
|
17
|
-
demandOption: true,
|
18
|
-
default: false,
|
19
|
-
description: 'Whether to run as a server'
|
20
|
-
},
|
21
|
-
'server-address': {
|
22
|
-
type: 'string',
|
23
|
-
demandOption: false,
|
24
|
-
description: 'Server IP address',
|
25
|
-
default: ''
|
26
|
-
},
|
27
|
-
transport: {
|
28
|
-
type: 'string',
|
29
|
-
demandOption: false,
|
30
|
-
description: 'Transport to use',
|
31
|
-
default: 'tcp'
|
32
|
-
},
|
33
|
-
'upload-bytes': {
|
34
|
-
type: 'number',
|
35
|
-
demandOption: false,
|
36
|
-
description: 'Number of bytes to upload',
|
37
|
-
default: 0
|
38
|
-
},
|
39
|
-
'download-bytes': {
|
40
|
-
type: 'number',
|
41
|
-
demandOption: false,
|
42
|
-
description: 'Number of bytes to download',
|
43
|
-
default: 0
|
44
|
-
}
|
45
|
-
})
|
46
|
-
.command('help', 'Print usage information', yargs.help)
|
47
|
-
.parseSync()
|
48
|
-
|
49
|
-
export async function main (runServer: boolean, serverIpAddress: string, transport: string, uploadBytes: number, downloadBytes: number): Promise<void> {
|
50
|
-
const listenAddrs: string[] = []
|
51
|
-
|
52
|
-
const { host, port } = splitHostPort(serverIpAddress)
|
53
|
-
// #TODO: right now we only support tcp
|
54
|
-
const tcpMultiaddr = multiaddr(`/ip4/${host}/tcp/${port}`)
|
55
|
-
|
56
|
-
const config = {
|
57
|
-
transports: [tcp()],
|
58
|
-
streamMuxers: [yamux()],
|
59
|
-
connectionEncryption: [
|
60
|
-
noise()
|
61
|
-
],
|
62
|
-
services: {
|
63
|
-
perf: perfService(defaultInit)
|
64
|
-
}
|
65
|
-
}
|
66
|
-
|
67
|
-
const testPrivKey = 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoS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onn5mZnGDh+72BxLNdgwBkhO0OCdpdISqk0F0Pxby22DFOKXZEpiyI9XYP1C8wPiJsShGm2yEwBPWXnrrZNWczaVuCbXHrZkWQogBDG3HGXNdU4MAWCyiYlyinIBpPpoAJZSzpGLmWbMWh28+RJS6AQX6KHrK1o2uw=='
|
68
|
-
const encoded = uint8ArrayFromString(testPrivKey, 'base64pad')
|
69
|
-
const privateKey = await unmarshalPrivateKey(encoded)
|
70
|
-
const peerId = await createFromPrivKey(privateKey)
|
71
|
-
const tcpMultiaddrAddress = `${tcpMultiaddr.toString()}/p2p/${peerId.toString()}`
|
72
|
-
|
73
|
-
if (runServer) {
|
74
|
-
listenAddrs.push(tcpMultiaddrAddress)
|
75
|
-
|
76
|
-
Object.assign(config, {
|
77
|
-
peerId,
|
78
|
-
addresses: {
|
79
|
-
listen: listenAddrs
|
80
|
-
}
|
81
|
-
})
|
82
|
-
}
|
83
|
-
|
84
|
-
const node = await createLibp2p(config)
|
85
|
-
|
86
|
-
await node.start()
|
87
|
-
|
88
|
-
const startTime = Date.now()
|
89
|
-
|
90
|
-
if (!runServer) {
|
91
|
-
const connection = await node.dial(multiaddr(tcpMultiaddrAddress))
|
92
|
-
const duration = await node.services.perf.measurePerformance(startTime, connection, BigInt(uploadBytes), BigInt(downloadBytes))
|
93
|
-
// Output latency to stdout in seconds
|
94
|
-
// eslint-disable-next-line no-console
|
95
|
-
console.log(JSON.stringify({ latency: duration / 1000 }))
|
96
|
-
await node.stop()
|
97
|
-
}
|
98
|
-
}
|
99
|
-
|
100
|
-
function splitHostPort (address: string): { host: string, port?: string } {
|
101
|
-
try {
|
102
|
-
const parts = address.split(':')
|
103
|
-
const host = parts[0]
|
104
|
-
const port = parts[1]
|
105
|
-
return {
|
106
|
-
host,
|
107
|
-
port
|
108
|
-
}
|
109
|
-
} catch (error) {
|
110
|
-
throw Error('Invalid server address')
|
111
|
-
}
|
112
|
-
}
|
113
|
-
|
114
|
-
main(argv['run-server'], argv['server-address'], argv.transport, argv['upload-bytes'], argv['download-bytes']).catch((err) => {
|
115
|
-
// eslint-disable-next-line no-console
|
116
|
-
console.error(err)
|
117
|
-
process.exit(1)
|
118
|
-
})
|