Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/async await #202

Merged
merged 15 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

module.exports = {
bundlesize: { maxSize: '225kB' },
karma: {
files: [{
pattern: 'test/test-data/**/*',
Expand Down
16 changes: 15 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
language: node_js
cache: npm

stages:
- check
- test
- cov

node_js:
- '12'
- '10'

os:
Expand All @@ -20,7 +22,7 @@ jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir build --bundlesize
- npx aegir dep-check
- npm run lint

Expand All @@ -36,5 +38,17 @@ jobs:
firefox: latest
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless

- stage: test
name: electron-main
os: osx
script:
- npx aegir test -t electron-main --bail

- stage: test
name: electron-renderer
os: osx
script:
- npx aegir test -t electron-renderer --bail

notifications:
email: false
143 changes: 55 additions & 88 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,76 @@
/* eslint-disable no-console */
'use strict'

const series = require('async/series')
const parallel = require('async/parallel')
const map = require('async/map')
const mapSeries = require('async/mapSeries')
const each = require('async/each')
const _ = require('lodash')
const Block = require('ipfs-block')
const assert = require('assert')
const crypto = require('crypto')
const CID = require('cids')
const multihashing = require('multihashing-async')
const range = require('lodash.range')

const utils = require('../test/utils')
const makeBlock = require('../test/utils/make-block')
const genBitswapNetwork = require('../test/utils/mocks').genBitswapNetwork

const nodes = [2, 5, 10, 20]
const blockFactors = [1, 10, 100]

console.log('-- start')
mapSeries(nodes, (n, cb) => {
mapSeries(blockFactors, (blockFactor, cb) => {
utils.genBitswapNetwork(n, (err, nodeArr) => {
if (err) {
return cb(err)
}

round(nodeArr, blockFactor, n, (err) => {
if (err) {
return cb(err)
}

shutdown(nodeArr, cb)
})
;(async function () {
console.log('-- start')
await Promise.all(
nodes.map(async nodeCount => {
await Promise.all(
blockFactors.map(async blockFactor => {
const nodeArr = await genBitswapNetwork(nodeCount)
await round(nodeArr, blockFactor, nodeCount)
await shutdown(nodeArr)
})
)
})
}, cb)
}, (err) => {
if (err) {
throw err
}
)

console.log('-- finished')
})
})()

function shutdown (nodeArr, cb) {
each(nodeArr, (node, cb) => {
node.bitswap.stop()
node.libp2p.stop(cb)
}, cb)
async function shutdown (nodeArr) {
await Promise.all(
nodeArr.map(async node => {
await node.bitswap.stop()
await node.libp2p.stop()
})
)
}

function round (nodeArr, blockFactor, n, cb) {
createBlocks(n, blockFactor, (err, blocks) => {
if (err) {
return cb(err)
}
const cids = blocks.map((b) => b.cid)
let d
series([
// put blockFactor amount of blocks per node
(cb) => parallel(_.map(nodeArr, (node, i) => (callback) => {
node.bitswap.start()

const data = _.map(_.range(blockFactor), (j) => {
async function round (nodeArr, blockFactor, n) {
const blocks = await makeBlock(n * blockFactor)
const cids = blocks.map((b) => b.cid)

console.info('put blockFactor amount of blocks per node')

await Promise.all(
nodeArr.map(async (node, i) => {
await node.bitswap.start()

await Promise.all(
range(blockFactor).map(async j => {
const index = i * blockFactor + j
return blocks[index]
})
each(
data,
(d, cb) => node.bitswap.put(d, cb),
callback
)
}), cb),
(cb) => {
d = (new Date()).getTime()
cb()
},
// fetch all blocks on every node
(cb) => parallel(_.map(nodeArr, (node, i) => (callback) => {
map(cids, (cid, cb) => node.bitswap.get(cid, cb), (err, res) => {
if (err) {
return callback(err)
}

assert(res.length === blocks.length)
callback()

await node.bitswap.put(blocks[index])
})
}), cb)
], (err) => {
if (err) {
return cb(err)
}
console.log(' %s nodes - %s blocks/node - %sms', n, blockFactor, (new Date()).getTime() - d)
cb()
)
})
})
}
)

console.info('fetch all blocks on every node')

function createBlocks (n, blockFactor, callback) {
map(_.range(n * blockFactor), (i, cb) => {
const data = crypto.randomBytes(n * blockFactor)
multihashing(data, 'sha2-256', (err, hash) => {
if (err) {
return cb(err)
const d = Date.now()

await Promise.all(
nodeArr.map(async node => {
let count = 0

for await (const _ of node.bitswap.getMany(cids)) { // eslint-disable-line no-unused-vars
count++
}
cb(null, new Block(data, new CID(hash)))

assert(count === blocks.length)
})
}, callback)
)

console.log(' %s nodes - %s blocks/node - %sms', n, blockFactor, Date.now() - d)
}
74 changes: 19 additions & 55 deletions benchmarks/put-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,34 @@
'use strict'

const Benchmark = require('benchmark')
const _ = require('lodash')
const Block = require('ipfs-block')
const assert = require('assert')
const series = require('async/series')
const map = require('async/map')
const crypto = require('crypto')
const CID = require('cids')
const multihashing = require('multihashing-async')

const utils = require('../test/utils')
const all = require('async-iterator-all')
const makeBlock = require('../test/utils/make-block')
const genBitswapNetwork = require('../test/utils/mocks').genBitswapNetwork

const suite = new Benchmark.Suite('put-get')

const blockCounts = [1, 10, 1000]
const blockSizes = [10, 1024, 10 * 1024]

utils.genBitswapNetwork(1, (err, nodes) => {
if (err) {
throw err
}
const node = nodes[0]
;(async function () {
const [
node
] = await genBitswapNetwork(1)

const bitswap = node.bitswap

blockCounts.forEach((n) => blockSizes.forEach((k) => {
suite.add(`put-get ${n} blocks of size ${k}`, (defer) => {
createBlocks(n, k, (err, blocks) => {
if (err) {
throw err
}
series([
(cb) => bitswap.putMany(blocks, cb),
(cb) => get(blocks, bitswap, cb)
], (err) => {
if (err) {
throw err
}
defer.resolve()
})
})
suite.add(`put-get ${n} blocks of size ${k}`, async (defer) => {
const blocks = await makeBlock(n, k)

await bitswap.putMany(blocks)

const res = await all(bitswap.getMany(blocks.map(block => block.cid)))

assert(res.length === blocks.length)

defer.resolve()
}, {
defer: true
})
Expand All @@ -57,29 +46,4 @@ utils.genBitswapNetwork(1, (err, nodes) => {
.run({
async: true
})
})

function createBlocks (n, k, callback) {
map(_.range(n), (i, cb) => {
const data = crypto.randomBytes(k)
multihashing(data, 'sha2-256', (err, hash) => {
if (err) {
return cb(err)
}
cb(null, new Block(data, new CID(hash)))
})
}, callback)
}

function get (blocks, bs, callback) {
map(blocks, (b, cb) => {
bs.get(b.cid, cb)
}, (err, res) => {
if (err) {
return callback(err)
}

assert(res.length === blocks.length)
callback()
})
}
})()
29 changes: 16 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,42 @@
"homepage": "https://github.com/ipfs/js-ipfs-bitswap#readme",
"devDependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"aegir": "^18.2.1",
"aegir": "^20.3.1",
"async-iterator-all": "^1.0.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"ipfs-repo": "~0.26.3",
"libp2p": "~0.24.2",
"libp2p-kad-dht": "~0.15.0",
"libp2p-mplex": "~0.8.4",
"ipfs-repo": "ipfs/js-ipfs-repo#blockstore-accept-async-iterator",
"libp2p": "^0.26.1",
"libp2p-kad-dht": "^0.16.0",
"libp2p-mplex": "^0.8.0",
"libp2p-secio": "~0.11.1",
"libp2p-tcp": "~0.13.0",
"lodash": "^4.17.11",
"libp2p-tcp": "^0.13.0",
"lodash.difference": "^4.5.0",
"lodash.flatten": "^4.4.0",
"lodash.range": "^3.2.0",
"lodash.without": "^4.4.0",
"ncp": "^2.0.0",
"p-event": "^4.1.0",
"peer-book": "~0.9.0",
"peer-id": "~0.12.0",
"peer-info": "~0.15.0",
"rimraf": "^2.6.2",
"peer-id": "^0.12.2",
"peer-info": "~0.15.1",
"promisify-es6": "^1.0.3",
"rimraf": "^3.0.0",
"safe-buffer": "^5.1.2",
"stats-lite": "^2.2.0",
"uuid": "^3.3.2"
},
"dependencies": {
"async": "^2.6.1",
"bignumber.js": "^8.0.1",
"bignumber.js": "^9.0.0",
"cids": "~0.7.0",
"debug": "^4.1.0",
"ipfs-block": "~0.8.0",
"just-debounce-it": "^1.1.0",
"lodash.isequalwith": "^4.4.0",
"moving-average": "^1.0.0",
"multicodec": "~0.5.0",
"multihashing-async": "~0.5.1",
"multihashing-async": "^0.8.0",
"protons": "^1.0.1",
"pull-length-prefixed": "^1.3.1",
"pull-stream": "^3.6.9",
Expand Down
Loading