Skip to content

Commit d11a35c

Browse files
authored
chore: update benchmark (#82)
1 parent b8b2d93 commit d11a35c

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

benchmarks/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Floodsub Benchmarks
2+
==========
3+
4+
Benchmark for the `libp2p-floodsub` implementation.
5+
6+
This simple benchmark calculates the number of messages we are able to send from one peer to another.
7+
8+
## Testing
9+
10+
For running the benchmarks, it is required to install the dependencies of the `libp2p-floodsub`. With those installed, you only need to run the `index.js` file as follows:
11+
12+
```sh
13+
$ npm install
14+
$ cd benchmarks
15+
$ node index.js
16+
publish and receive x 781 ops/sec ±11.96% (64 runs sampled)
17+
```

benchmarks/index.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Benchmark = require('benchmark')
44
const crypto = require('crypto')
55
const map = require('async/map')
66
const parallel = require('async/parallel')
7+
const series = require('async/series')
78

89
const PSG = require('../src')
910
const utils = require('../test/utils')
@@ -14,14 +15,25 @@ const suite = new Benchmark.Suite('pubsub')
1415
// one node to another.
1516

1617
map([0, 1], (i, cb) => {
17-
utils.createNode('/ip4/127.0.0.1/tcp/0', (err, node) => {
18+
utils.createNode((err, node) => {
1819
if (err) {
1920
return cb(err)
2021
}
2122

22-
cb(null, {
23-
libp2p: node,
24-
ps: new PSG(node)
23+
const ps = new PSG(node)
24+
25+
series([
26+
(cb) => node.start(cb),
27+
(cb) => ps.start(cb)
28+
], (err) => {
29+
if (err) {
30+
return cb(err)
31+
}
32+
33+
cb(null, {
34+
libp2p: node,
35+
ps
36+
})
2537
})
2638
})
2739
}, (err, peers) => {
@@ -30,16 +42,17 @@ map([0, 1], (i, cb) => {
3042
}
3143

3244
parallel([
33-
(cb) => peers[0].libp2p.dialByPeerInfo(peers[1].libp2p.peerInfo, cb),
45+
(cb) => peers[0].libp2p.dial(peers[1].libp2p.peerInfo, cb),
3446
(cb) => setTimeout(() => {
35-
peers[0].ps.subscribe('Z')
36-
peers[1].ps.subscribe('Z')
47+
peers[0].ps.subscribe('Z', () => {}, () => {})
48+
peers[1].ps.subscribe('Z', () => {}, () => {})
3749
cb(null, peers)
3850
}, 200)
3951
], (err, res) => {
4052
if (err) {
4153
throw err
4254
}
55+
4356
const peers = res[1]
4457

4558
suite.add('publish and receive', (deferred) => {

0 commit comments

Comments
 (0)