fix: re-enable js-libp2p under node.js (#325)

Adds perf tests for js-libp2p 0.46.x and 1.0 (via the `next` tag).

---------

Co-authored-by: achingbrain <achingbrain@users.noreply.github.com>
This commit is contained in:
Alex Potsides 2023-11-21 08:22:56 +00:00 committed by GitHub
parent 7ca92e80ab
commit 9a4c969523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 30500 additions and 22025 deletions

View File

@ -22,7 +22,7 @@ jobs:
perf: perf:
name: Perf name: Perf
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 120 timeout-minutes: 240
defaults: defaults:
run: run:
shell: bash shell: bash

View File

@ -1,19 +1,12 @@
NPM_PACKAGE_VERSION := 1.1.6 DOCKER_IMAGE := node:20-alpine
NPM_PACKAGE := @libp2p/perf DOCKER_RUN := docker run --rm -v "$(shell pwd)":/usr/src/myapp -w /usr/src/myapp $(DOCKER_IMAGE)
NPM_PACKAGE_NAME := $(NPM_PACKAGE)@$(NPM_PACKAGE_VERSION)
SOURCE_DIR := js-libp2p-protocol-perf
DOCKER_IMAGE := node:18.17.1
DOCKER_RUN := docker run --rm -v "$(shell pwd)/$(SOURCE_DIR)":/usr/src/myapp -w /usr/src/myapp $(DOCKER_IMAGE)
all: perf all: perf
perf: perf:
mkdir -p $(SOURCE_DIR) $(DOCKER_RUN) npm ci
$(DOCKER_RUN) npm install $(NPM_PACKAGE_NAME)
clean: clean:
rm -rf js-libp2p-* rm -rf node_modules
.PHONY: all clean perf .PHONY: all clean perf

View File

@ -0,0 +1,104 @@
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { tcp } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2p } from 'libp2p'
import { perfService } from '@libp2p/perf'
import { parseArgs } from 'node:util'
const argv = parseArgs({
options: {
'run-server': {
type: 'string',
default: 'false'
},
'server-address': {
type: 'string'
},
transport: {
type: 'string',
default: 'tcp'
},
'upload-bytes': {
type: 'string',
default: '0'
},
'download-bytes': {
type: 'string',
default: '0'
}
}
})
/**
* @param {boolean} runServer
* @param {string} serverIpAddress
* @param {string} transport
* @param {number} uploadBytes
* @param {number} downloadBytes
*/
export async function main (runServer, serverIpAddress, transport, uploadBytes, downloadBytes) {
const { host, port } = splitHostPort(serverIpAddress)
const config = {
transports: [tcp()],
streamMuxers: [yamux()],
connectionEncryption: [
noise()
],
connectionManager: {
minConnections: 0
},
services: {
perf: perfService()
}
}
if (runServer) {
Object.assign(config, {
addresses: {
listen: [
// #TODO: right now we only support tcp
`/ip4/${host}/tcp/${port}`
]
}
})
}
const node = await createLibp2p(config)
await node.start()
if (!runServer) {
for await (const output of node.services.perf.measurePerformance(multiaddr(`/ip4/${host}/tcp/${port}`), uploadBytes, downloadBytes)) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(output))
}
await node.stop()
}
}
/**
* @param {string} address
* @returns { host: string, port?: string }
*/
function splitHostPort (address) {
try {
const parts = address.split(':')
const host = parts[0]
const port = parts[1]
return {
host,
port
}
} catch (error) {
throw Error('Invalid server address')
}
}
main(argv.values['run-server'] === 'true', argv.values['server-address'], argv.values.transport, Number(argv.values['upload-bytes']), Number(argv.values['download-bytes'])).catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})

1307
perf/impl/js-libp2p/v0.46/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,13 @@
{
"name": "@libp2p/perf-js-libp2p-0-46",
"private": true,
"main": "index.js",
"type": "module",
"dependencies": {
"@chainsafe/libp2p-noise": "^13.0.3",
"@chainsafe/libp2p-yamux": "^5.0.3",
"@libp2p/perf": "^2.0.1",
"@libp2p/tcp": "^8.0.13",
"libp2p": "^0.46.21"
}
}

View File

@ -36,7 +36,7 @@ for ((i = 1; i <= $#; i++)); do
done done
# Run perf # Run perf
node impl/js-libp2p/v0.46/js-libp2p-protocol-perf/node_modules/@libp2p/perf/dist/src/main.js --run-server=$run_server --server-address=$server_address --upload-bytes=$upload_bytes --download-bytes=$download_bytes --transport=$transport & node $(dirname "$0")/index.js --run-server=$run_server --server-address=$server_address --upload-bytes=$upload_bytes --download-bytes=$download_bytes --transport=$transport &
node_pid=$! node_pid=$!

View File

@ -0,0 +1,12 @@
DOCKER_IMAGE := node:20-alpine
DOCKER_RUN := docker run --rm -v "$(shell pwd)":/usr/src/myapp -w /usr/src/myapp $(DOCKER_IMAGE)
all: perf
perf:
$(DOCKER_RUN) npm ci
clean:
rm -rf node_modules
.PHONY: all clean perf

View File

@ -0,0 +1,112 @@
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { tcp } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2p } from 'libp2p'
import { perf } from '@libp2p/perf'
import { parseArgs } from 'node:util'
const argv = parseArgs({
options: {
'run-server': {
type: 'string',
default: 'false'
},
'server-address': {
type: 'string'
},
transport: {
type: 'string',
default: 'tcp'
},
'upload-bytes': {
type: 'string',
default: '0'
},
'download-bytes': {
type: 'string',
default: '0'
}
}
})
/**
* @param {boolean} runServer
* @param {string} serverIpAddress
* @param {string} transport
* @param {number} uploadBytes
* @param {number} downloadBytes
*/
export async function main (runServer, serverIpAddress, transport, uploadBytes, downloadBytes) {
const { host, port } = splitHostPort(serverIpAddress)
const config = {
//peerId,
transports: [tcp({
socket: {
noDelay: true
},
server: {
noDelay: true
}
})],
streamMuxers: [yamux()],
connectionEncryption: [
noise()
],
connectionManager: {
minConnections: 0
},
services: {
perf: perf()
}
}
if (runServer) {
Object.assign(config, {
addresses: {
listen: [
// #TODO: right now we only support tcp
`/ip4/${host}/tcp/${port}`
]
}
})
}
const node = await createLibp2p(config)
await node.start()
if (!runServer) {
for await (const output of node.services.perf.measurePerformance(multiaddr(`/ip4/${host}/tcp/${port}`), uploadBytes, downloadBytes)) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(output))
}
await node.stop()
}
}
/**
* @param {string} address
* @returns { host: string, port?: string }
*/
function splitHostPort (address) {
try {
const parts = address.split(':')
const host = parts[0]
const port = parts[1]
return {
host,
port
}
} catch (error) {
throw Error('Invalid server address')
}
}
main(argv.values['run-server'] === 'true', argv.values['server-address'], argv.values.transport, Number(argv.values['upload-bytes']), Number(argv.values['download-bytes'])).catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})

1142
perf/impl/js-libp2p/v1.0/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,13 @@
{
"name": "@libp2p/perf-js-libp2p-1-0",
"private": true,
"main": "index.js",
"type": "module",
"dependencies": {
"@chainsafe/libp2p-noise": "^13.0.3",
"@chainsafe/libp2p-yamux": "^5.0.3",
"@libp2p/perf": "next",
"@libp2p/tcp": "next",
"libp2p": "next"
}
}

45
perf/impl/js-libp2p/v1.0/perf Executable file
View File

@ -0,0 +1,45 @@
#!/bin/bash
# In case this script is `kill`ed, `kill` its child process, namely the `node`
# process below.
cleanup() {
kill $node_pid
}
trap cleanup EXIT TERM
# Find the path to the Node.js executable
node_path=$(which node)
run_server=false
server_address=""
upload_bytes=0
download_bytes=0
transport=""
# Parse named parameters manually
for ((i = 1; i <= $#; i++)); do
if [ "${!i}" == "--server-address" ]; then
server_address="${@:i+1:1}"
fi
if [ "${!i}" == "--upload-bytes" ]; then
upload_bytes="${@:i+1:1}"
fi
if [ "${!i}" == "--download-bytes" ]; then
download_bytes="${@:i+1:1}"
fi
if [ "${!i}" == "--transport" ]; then
transport="${@:i+1:1}"
fi
if [ "${!i}" == "--run-server" ]; then
run_server=true
fi
done
# Run perf
node $(dirname "$0")/index.js --run-server=$run_server --server-address=$server_address --upload-bytes=$upload_bytes --download-bytes=$download_bytes --transport=$transport &
node_pid=$!
# Wait for `node_pid` to finish, or for it to be `kill`ed by the above
# `cleanup`.
wait $node_pid

File diff suppressed because it is too large Load Diff

View File

@ -40,4 +40,14 @@ export const versions: Array<Version> = [
implementation: "go-libp2p", implementation: "go-libp2p",
transportStacks: ["tcp", "quic-v1"] transportStacks: ["tcp", "quic-v1"]
}, },
{
id: "v0.46",
implementation: "js-libp2p",
transportStacks: ["tcp"]
},
{
id: "v1.0",
implementation: "js-libp2p",
transportStacks: ["tcp"]
},
] ]