-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test/manual/measure-reconnect.js
- Loading branch information
1 parent
6f7434e
commit b497d68
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* 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 seed = Buffer.alloc(32).fill('billie-fast-reconnect') | ||
const topic = seed | ||
|
||
const swarm = new Hyperswarm({ 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) |