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

Commit

Permalink
test: move tests and add benchmark runs
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed May 15, 2018
1 parent e29e137 commit 62b5446
Show file tree
Hide file tree
Showing 12 changed files with 776 additions and 1 deletion.
13 changes: 13 additions & 0 deletions benchmarks/libp2p-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
18 changes: 18 additions & 0 deletions benchmarks/libp2p-mplex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "libp2p-mplex-profile",
"version": "1.0.0",
"description": "",
"main": "profile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"async": "^2.6.0",
"libp2p-mplex": "0.7.0",
"pull-generate": "^2.2.0",
"pull-pair": "^1.1.0",
"pull-stream": "^3.6.8"
}
}
85 changes: 85 additions & 0 deletions benchmarks/libp2p-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('libp2p-mplex')

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/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/pull-mplex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "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/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)
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
"devDependencies": {
"aegir": "^13.1.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"chunky": "0.0.0",
"dirty-chai": "^2.0.1",
"interface-stream-muxer": "~0.5.9",
"libp2p-tcp": "^0.12.0",
"libp2p-websockets": "~0.11.0",
"pre-commit": "^1.2.2",
"pull-pair": "^1.1.0"
"pull-pair": "^1.1.0",
"pull-stream-to-stream": "^1.3.4"
},
"dependencies": {
"async": "^2.6.0",
Expand Down
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const WSlibp2p = require('libp2p-websockets')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
Expand Down
1 change: 1 addition & 0 deletions test/mplex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const pair = require('pull-pair/duplex')
const pull = require('pull-stream')

Expand Down
11 changes: 11 additions & 0 deletions test/old-new-interop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Old node stream based interop tests

### What is this?

This will install libp2p-mplex version `0.7.0` and run tests against it using the new `pull-mplex` implementation. This needs to be a separate packages because we can't install `libp2p-mplex` into `libp2p-mplex` ;(

### To run the tests do:

```
npm i && npm run test
```
18 changes: 18 additions & 0 deletions test/old-new-interop/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "old-new-interop",
"version": "1.0.0",
"description": "",
"main": "stream-mplex-interop.spec.js",
"scripts": {
"test": "aegir test -t node browser -f test/stream-mplex-interop.js"
},
"author": "",
"license": "MIT",
"devDependencies": {
"chunky": "0.0.0",
"concat-stream": "^1.6.2",
"libp2p-mplex": "0.7.0",
"pull-stream-to-stream": "^1.3.4",
"through2": "^2.0.3"
}
}
Loading

0 comments on commit 62b5446

Please sign in to comment.