-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove abortable iterator from muxer (#63)
* fix: remove abortable iterator from muxer We use an abortable source for every connection - this introduces a lot of latency. This PR removes abortable iterator to just listen on the abort event on the shutdown controller's abort signal and close the connection's iterator. This is the same thing that abortable source does except we don't race every `next` from the source. I've added a simple benchmark - in my testing it increases throughput by 30% or so, this seems to translate to 10-15% in "real world" performance using the `@libp2p/perf` testing protocol. * chore: log error if causing sink to return fails
- Loading branch information
1 parent
fe69086
commit 064bf1c
Showing
4 changed files
with
99 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "yamux-benchmarks", | ||
"version": "0.0.1", | ||
"description": "Benchmark for Libp2p's Yamux specification in Js", | ||
"main": "benchmark.js", | ||
"type": "module", | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"it-drain": "^3.0.3", | ||
"it-pair": "^2.0.6", | ||
"it-pipe": "^3.0.1", | ||
"uint8arraylist": "^2.4.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import drain from 'it-drain' | ||
import { duplexPair } from 'it-pair/duplex' | ||
import { pipe } from 'it-pipe' | ||
import { Uint8ArrayList } from 'uint8arraylist' | ||
import { yamux } from '../dist/src/index.js' | ||
|
||
const DATA_LENGTH = 1024 * 1024 * 1024 * 5 | ||
const CHUNK_SIZE = (1024 * 1024) / 4 | ||
const ITERATIONS = 10 | ||
|
||
const results = [] | ||
|
||
for (let i = 0; i < ITERATIONS; i++) { | ||
const p = duplexPair() | ||
const muxerA = yamux()().createStreamMuxer({ | ||
direction: 'outbound' | ||
}) | ||
const muxerB = yamux()().createStreamMuxer({ | ||
direction: 'inbound', | ||
onIncomingStream: (stream) => { | ||
// echo stream back to itself | ||
pipe(stream, stream) | ||
} | ||
}) | ||
|
||
// pipe data through muxers | ||
pipe(p[0], muxerA, p[0]) | ||
pipe(p[1], muxerB, p[1]) | ||
|
||
const stream = await muxerA.newStream() | ||
|
||
const start = Date.now() | ||
|
||
await pipe( | ||
async function * () { | ||
for (let i = 0; i < DATA_LENGTH; i += CHUNK_SIZE) { | ||
yield * new Uint8ArrayList(new Uint8Array(CHUNK_SIZE)) | ||
} | ||
}, | ||
stream, | ||
(source) => drain(source) | ||
) | ||
|
||
const finish = Date.now() - start | ||
|
||
muxerA.close() | ||
muxerB.close() | ||
|
||
results.push(finish) | ||
} | ||
|
||
const megs = DATA_LENGTH / (1024 * 1024) | ||
const secs = (results.reduce((acc, curr) => acc + curr, 0) / results.length) / 1000 | ||
|
||
// eslint-disable-next-line no-console | ||
console.info((megs / secs).toFixed(2), 'MB/s') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters