Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit ed7531c

Browse files
committed
Fix pubsub load test progress output. Add check for streams buffering too much. Add bigger timeout for 10k load test so that js-ipfs has enough time to run it.
1 parent c0c2b3c commit ed7531c

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/pubsub.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,14 @@ module.exports = (common) => {
332332
})
333333
})
334334

335-
describe('load tests', () => {
336-
it('send/receive 10k messages', (done) => {
335+
describe('load tests', function () {
336+
// Write the progress to stdout when in Node.js, silent when in the browser
337+
const log = process && process.stdout ? (s) => process.stdout.write(s) : () => {}
338+
339+
it('send/receive 10k messages', function (done) {
340+
// js-ipfs is a little slow atm, so make sure we have enough time
341+
this.timeout(2 * 60 * 1000)
342+
337343
const expectedString = 'hello'
338344
const count = 10000
339345
let sendCount = 0
@@ -350,23 +356,36 @@ module.exports = (common) => {
350356
expect(err).to.not.exists
351357

352358
const outputProgress = () => {
353-
/*
354-
process.stdout.write(' \r')
355-
process.stdout.write('Sent: ' + sendCount + ' of ' + count + ', Received: ' + receivedCount + '\r')
356-
*/
359+
log(' \r')
360+
log('Sent: ' + sendCount + ' of ' + count + ', Received: ' + receivedCount + '\r')
357361
}
358362

359363
subscription.on('data', (d) => {
360364
expect(d.data.toString()).to.equal(expectedString)
365+
361366
receivedCount++
362367
outputProgress()
363368
if (receivedCount >= count) {
364369
const duration = new Date().getTime() - startTime
365-
console.log(`Send/Receive 10k messages took: ${duration} ms, ${Math.floor(count / (duration / 1000))} ops / s`)
370+
log(`Send/Receive 10k messages took: ${duration} ms, ${Math.floor(count / (duration / 1000))} ops / s\n`)
366371
subscription.cancel()
367372
.then(() => subscription2.cancel())
368373
.then(done)
369374
}
375+
376+
// Check for early cancel.
377+
// We had bugs in the past where all 10k messages would be
378+
// buffered first and only then the receiving stream would
379+
// process them.
380+
// This check will make sure, that while the sending peer is
381+
// sending messages the receiving peer has started receiving
382+
// and processing them.
383+
if (sendCount - (count / 10) > receivedCount) {
384+
sendCount = count
385+
subscription.cancel()
386+
.then(() => subscription2.cancel())
387+
.then(() => done(new Error(`Didn't receive enough messages on time!`)))
388+
}
370389
})
371390

372391
function loop () {
@@ -396,10 +415,8 @@ module.exports = (common) => {
396415
function loop () {
397416
if (sendCount < count) {
398417
sendCount++
399-
/*
400-
process.stdout.write(' \r')
401-
process.stdout.write('Sending messages: ' + sendCount + ' of ' + count + '\r')
402-
*/
418+
log(' \r')
419+
log('Sending messages: ' + sendCount + ' of ' + count + '\r')
403420
ipfs1.pubsub.publish(topic, expectedString, (err) => {
404421
expect(err).to.not.exist
405422
process.nextTick(() => loop())
@@ -420,10 +437,8 @@ module.exports = (common) => {
420437
function loop () {
421438
if (sendCount < count) {
422439
sendCount++
423-
/*
424-
process.stdout.write(' \r')
425-
process.stdout.write('Subscribing: ' + sendCount + ' of ' + count + '\r')
426-
*/
440+
log(' \r')
441+
log('Subscribing: ' + sendCount + ' of ' + count + '\r')
427442
ipfs1.pubsub.subscribe(topic, (err, res) => {
428443
receivedCount++
429444
// First call should go through normally
@@ -447,13 +462,10 @@ module.exports = (common) => {
447462
it('subscribe/unsubscribe 1k times', (done) => {
448463
const count = 1000
449464
let sendCount = 0
450-
// let receivedCount = 0
451465

452466
function outputProgress () {
453-
/*
454-
process.stdout.write(' \r')
455-
process.stdout.write('Subscribe: ' + sendCount + ' of ' + count + ', Unsubscribe: ' + receivedCount + '\r')
456-
*/
467+
log(' \r')
468+
log('Subscribe/Unsubscribe: ' + sendCount + ' of ' + count + '\r')
457469
}
458470

459471
function loop () {
@@ -464,7 +476,6 @@ module.exports = (common) => {
464476
expect(err).to.not.exist
465477
subscription.cancel((err) => {
466478
expect(err).to.not.exist
467-
// receivedCount++
468479
outputProgress()
469480
process.nextTick(() => loop())
470481
})

0 commit comments

Comments
 (0)