Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat(benchmarks): adding benchmarks
Browse files Browse the repository at this point in the history
benchmarks include:
- multiplex
- pull-mplex
- libp2p-mplex (and libp2p-mplex using pull-mplex)

License: MIT
Signed-off-by: Dmitriy Ryajov <dryajov@gmail.com>
  • Loading branch information
dryajov committed Jun 19, 2018
1 parent 751965e commit af3f60f
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 7 deletions.
13 changes: 13 additions & 0 deletions benchmarks/libp2p-pull-mplex/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package-lock.json
yarn.lock
docs

**/node_modules
**/*.log
test/setup/tmp-disposable-nodes-addrs.json
dist
coverage
**/*.swp
examples/sub-module/**/bundle.js
examples/sub-module/**/*-minified.js
examples/sub-module/*-bundle.js
17 changes: 17 additions & 0 deletions benchmarks/libp2p-pull-mplex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "libp2p-pull-mplex-profile",
"version": "1.0.0",
"description": "",
"main": "profile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"async": "^2.6.0",
"pull-generate": "^2.2.0",
"pull-pair": "^1.1.0",
"pull-stream": "^3.6.8"
}
}
85 changes: 85 additions & 0 deletions benchmarks/libp2p-pull-mplex/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict'

const pair = require('pull-pair/duplex')
const pull = require('pull-stream')
const generate = require('pull-generate')
const each = require('async/each')
const eachLimit = require('async/eachLimit')
const setImmediate = require('async/setImmediate')

const Plex = require('../../src')

const spawn = (nStreams, nMsg, done, limit) => {
const p = pair()

const check = marker(2 * nStreams, done)

const msg = 'simple msg'

const listener = Plex.dialer(p[0])
const dialer = Plex.listener(p[1])

listener.on('stream', (stream) => {
pull(
stream,
pull.onEnd((err) => {
if (err) { return done(err) }
check()
pull(pull.empty(), stream)
})
)
})

const numbers = []
for (let i = 0; i < nStreams; i++) {
numbers.push(i)
}

const spawnStream = (n, cb) => {
const stream = dialer.newStream()
pull(
generate(0, (s, cb) => {
setImmediate(() => {
cb(s === nMsg ? true : null, msg, s + 1)
})
}),
stream,
pull.collect((err) => {
if (err) { return done(err) }
check()
cb()
})
)
}

if (limit) {
eachLimit(numbers, limit, spawnStream, () => {})
} else {
each(numbers, spawnStream, () => {})
}
}

function marker (n, done) {
let i = 0
return (err) => {
i++

// console.log(`${i} out of ${n} interactions`)
if (err) {
console.error('Failed after %s iterations', i)
return done(err)
}

if (i === n) {
done()
}
}
}

spawn(1000, 1000, (err) => {
if (err) {
throw err
}
console.log('Done')
process.exit(0)
}, 50000)
13 changes: 13 additions & 0 deletions benchmarks/multiplex/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package-lock.json
yarn.lock
docs

**/node_modules
**/*.log
test/setup/tmp-disposable-nodes-addrs.json
dist
coverage
**/*.swp
examples/sub-module/**/bundle.js
examples/sub-module/**/*-minified.js
examples/sub-module/*-bundle.js
15 changes: 15 additions & 0 deletions benchmarks/multiplex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "multiplex-profile",
"version": "1.0.0",
"description": "",
"main": "profile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"async": "^2.6.0",
"multiplex": "^6.7.0"
}
}
79 changes: 79 additions & 0 deletions benchmarks/multiplex/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict'

const each = require('async/each')
const eachLimit = require('async/eachLimit')

const Plex = require('multiplex')

const spawn = (nStreams, nMsg, done, limit) => {
const check = marker(2 * nStreams, done)

const msg = 'simple msg'

const listener = new Plex()
const dialer = new Plex()

listener.on('stream', (stream) => {
stream.once('data', () => {
check()
})

stream.once('error', (err) => {
done(err)
})
})

dialer.pipe(listener).pipe(dialer)

const numbers = []
for (let i = 1; i <= nStreams; i++) {
numbers.push(i)
}

const spawnStream = (n, cb) => {
const stream = dialer.createStream()

stream.once('error', (err) => {
return done(err)
})

for (let i = 0; i <= nMsg; i++) {
stream.write(msg + i)
}

check()
stream.end()
cb()
}

if (limit) {
eachLimit(numbers, limit, spawnStream, () => {})
} else {
each(numbers, spawnStream, () => {})
}
}

function marker (n, done) {
let i = 0
return (err) => {
i++

// console.log(`${i} out of ${n} interactions`)
if (err) {
console.error('Failed after %s iterations', i)
return done(err)
}

if (i === n) {
done()
}
}
}

spawn(1000, 10000, (err) => {
if (err) {
throw err
}
console.log('Done')
process.exit(0)
}, 50000)
7 changes: 4 additions & 3 deletions benchmarks/pull-mplex/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "pull-mplex-profile",
"name": "libp2p-pull-mplex-profile",
"version": "1.0.0",
"description": "",
"main": "profile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"license": "MIT",
"dependencies": {
"async": "^2.6.0",
"pull-generate": "^2.2.0",
"pull-pair": "^1.1.0",
"pull-stream": "^3.6.8"
"pull-stream": "^3.6.8",
"pull-mplex": "0.0.1"
}
}
11 changes: 7 additions & 4 deletions benchmarks/pull-mplex/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const each = require('async/each')
const eachLimit = require('async/eachLimit')
const setImmediate = require('async/setImmediate')

const Plex = require('../../src')
const Plex = require('pull-mplex')

const spawn = (nStreams, nMsg, done, limit) => {
const p = pair()
Expand All @@ -16,8 +16,11 @@ const spawn = (nStreams, nMsg, done, limit) => {

const msg = 'simple msg'

const listener = Plex.dialer(p[0])
const dialer = Plex.listener(p[1])
const listener = new Plex(false)
const dialer = new Plex(true)

pull(dialer, p[0], dialer)
pull(listener, p[1], listener)

listener.on('stream', (stream) => {
pull(
Expand All @@ -36,7 +39,7 @@ const spawn = (nStreams, nMsg, done, limit) => {
}

const spawnStream = (n, cb) => {
const stream = dialer.newStream()
const stream = dialer.createStream()
pull(
generate(0, (s, cb) => {
setImmediate(() => {
Expand Down

0 comments on commit af3f60f

Please sign in to comment.