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

test: update measure_reconnect test with relay events #179

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dependencies": {
"b4a": "^1.3.1",
"bare-events": "^2.2.0",
"hyperdht": "^6.11.0",
"hyperdht": "^6.15.2",
"safety-catch": "^1.0.2",
"shuffled-priority-queue": "^2.1.0"
},
Expand Down
37 changes: 27 additions & 10 deletions test/manual/measure-reconnect.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
#!/usr/bin/env npx npm-auto
/* While this script *can* be run via node, you may have to install random dependencies. It is easier to run it through npm-auto */

/**
* 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
* It requires some extra packages that are private to Holepunch to get the relay keys,
* which is why these aren't devDependencies.
*/

const goodbye = require('graceful-goodbye')
const pc = require('picocolors')
function customLogger (data) {
console.log(` ... ${data.id} ${Object.keys(data.caller.props || []).join(',')} ${data.caller.filename}:${data.caller.line}:${data.caller.column}`)
const className = pc.gray(`[${data.object.className}]`)
const event = pc.blue(data.id)
const filename = pc.gray(`${data.caller.filename.replace(process.cwd(), '.')}:${data.caller.line}:${data.caller.column}`)
const props = `{ ${Object.keys(data.caller.props || []).join(',')} }`
console.log(`${className} ${event} ${props} ${filename}`)
}
require('hypertrace').setTraceFunction(customLogger)

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)
console.log('DEV_RELAY_KEYS', DEV_RELAY_KEYS.map(b => b.toString('hex').slice(0, 8) + '...'))
const relayThrough = (force) => force ? DEV_RELAY_KEYS : null

const Hyperswarm = require('../..')
Expand All @@ -22,6 +32,7 @@ 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 })
console.log(`PUBLIC_KEY ${swarm.keyPair.publicKey.toString('hex').slice(0, 8)}...`)

swarm.dht.on('network-change', () => {
console.log('NETWORK CHANGE')
Expand All @@ -31,9 +42,13 @@ swarm.dht.on('network-change', () => {
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))
console.log(conn.rawStream.remoteHost + ':' + conn.rawStream.remotePort)

conn.relay.on('relay', () => console.log('RELAY: RELAY'))
conn.relay.on('unrelay', () => console.log('RELAY: UNRELAY'))

conn.on('error', (...args) => console.log('error:', ...args))
conn.on('close', (...args) => console.log('close:', ...args))
conn.on('data', (data) => console.log(data.toString('utf8')))
conn.setKeepAlive(5000)
conn.write('hello')
Expand All @@ -48,6 +63,8 @@ swarm.on('connection', async (conn) => {
console.time('INITIAL CONNECTION TIME')
swarm.join(topic)

// process.on('SIGINT', () => {
// swarm.leave(topic).then(() => process.exit())
// })
goodbye(async () => {
console.log('\nleaving topic...')
await swarm.leave(topic)
console.log('bye')
})
Loading