Skip to content

Commit

Permalink
fix: consolidate to 1 file
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Feb 14, 2019
1 parent 51b1a2b commit 63415a4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 98 deletions.
4 changes: 2 additions & 2 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Benchmarks for both pull-mplex and libp2p-mplex
This test will send a ping message from the dialer to the listener, which is echo'd back to the dialer.
A new stream will be created for each echo.

**pull-mplex**: `node bench.js --lib=pull-mplex --msgs=100 --runs=3`
**libp2p-mplex**: `node bench.js --lib=libp2p-mplex --msgs=100 --runs=3`
**pull-mplex**: `node bench.js --lib=pull-mplex --repeat=100 --runs=3`
**libp2p-mplex**: `node bench.js --lib=libp2p-mplex --repeat=100 --runs=3`

You should see results like:
```sh
Expand Down
54 changes: 30 additions & 24 deletions benchmarks/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
const minimist = require('minimist')
const bench = require('fastbench')
const net = require('net')
const childProcess = require('child_process')
const path = require('path')
const parallel = require('fastparallel')({
results: false
})
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')

let msgs
let repeat
let runs

const argv = minimist(process.argv.slice(2), {
Expand All @@ -21,38 +19,46 @@ const argv = minimist(process.argv.slice(2), {
port: 3000,
host: 'localhost',
lib: 'pull-mplex',
msgs: 100,
repeat: 100,
runs: 3
}
})

function buildPingPong (cb) {
let child
function buildPingPong (callback) {
let dialer
const mplex = require(argv.lib || 'pull-mplex')
msgs = argv.msgs
repeat = argv.repeat
runs = argv.runs

if (argv.child) {
child = childProcess.fork(path.join(__dirname, 'mplex-echo.js'), {
stdio: 'inherit'
})

child.on('message', start)
start(argv)

child.on('error', cb)
function startServer (addr, cb) {
const server = net.createServer(function (socket) {
const connection = toPull.duplex(socket)
const listener = mplex.listener(connection)

child.on('exit', console.log)
} else {
start(argv)
listener.on('stream', (stream) => {
pull(
stream,
stream
)
})
})
server.listen(addr.port, function (err) {
if (err) throw err
cb()
})
}

function start (addr) {
const client = net.connect(addr.port, addr.host)
client.on('connect', function () {
const connection = toPull.duplex(client)
dialer = mplex.dialer(connection)
cb(null, benchPingPong)
startServer(addr, function() {
// Create the dialer
dialer = net.connect(addr.port, addr.host)
dialer.on('connect', function () {
const connection = toPull.duplex(dialer)
dialer = mplex.dialer(connection)
callback(null, benchPingPong)
})
})
}

Expand Down Expand Up @@ -92,8 +98,8 @@ function times (num, run, cb) {
buildPingPong(function (err, benchPingPong) {
if (err) throw err

// ping pong `msgs` many messages
const run = bench([benchPingPong], msgs)
// run benchPingPong `repeat` many times
const run = bench([benchPingPong], repeat)

// Do it `runs` many times
times(runs, run, function () {
Expand Down
72 changes: 0 additions & 72 deletions benchmarks/mplex-echo.js

This file was deleted.

0 comments on commit 63415a4

Please sign in to comment.