Skip to content

Commit

Permalink
add test/manual/measure-reconnect.js (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose authored Jun 21, 2024
1 parent 6f7434e commit 0fb70ee
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/manual/measure-reconnect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* The goal of this test is to measure how quickly a client reconnects
* after manually switching networks / e.g. from wifi to mobile data.
*
* It requires some extra modules to get the relays:
* npm install --no-save hypertrace hypercore-id-encoding @holepunchto/keet-default-config
*/

function customLogger (data) {
console.log(` ... ${data.id} ${Object.keys(data.caller.props || []).join(',')} ${data.caller.filename}:${data.caller.line}:${data.caller.column}`)
}
require('hypertrace').setTraceFunction(customLogger)

const { DEV_BLIND_RELAY_KEYS } = require('@holepunchto/keet-default-config')
const HypercoreId = require('hypercore-id-encoding')
const DEV_RELAY_KEYS = DEV_BLIND_RELAY_KEYS.map(HypercoreId.decode)
const relayThrough = (force) => force ? DEV_RELAY_KEYS : null

const Hyperswarm = require('../..')

const topic = Buffer.alloc(32).fill('measure-reconnect')
const seed = Buffer.alloc(32).fill('measure-reconnect' + require('os').hostname())

const swarm = new Hyperswarm({ seed, relayThrough })

swarm.dht.on('network-change', () => {
console.log('NETWORK CHANGE')
console.time('RECONNECTION TIME')
})

let connected = false

swarm.on('connection', async (conn) => {
console.log(conn.rawStream.remoteHost)
conn.on('error', console.log.bind(console))
conn.on('close', console.log.bind(console))
conn.on('data', (data) => console.log(data.toString('utf8')))
conn.setKeepAlive(5000)
conn.write('hello')
if (!connected) {
connected = true
console.timeEnd('INITIAL CONNECTION TIME')
return
}
console.timeEnd('RECONNECTION TIME')
})

console.time('INITIAL CONNECTION TIME')
swarm.join(topic)

// process.on('SIGINT', () => {
// swarm.leave(topic).then(() => process.exit())
// })

0 comments on commit 0fb70ee

Please sign in to comment.