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

feat(perf): add nim-libp2p v1.1 #262

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 7 additions & 3 deletions perf/impl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ GO_SUBDIRS := $(wildcard go-libp2p/*/.)
RUST_SUBDIRS := $(wildcard rust-libp2p/*/.)
HTTPS_SUBDIRS := $(wildcard https/*/.)
QUIC_GO_SUBDIRS := $(wildcard quic-go/*/.)
NIM_SUBDIRS := $(wildcard nim-libp2p/*/.)
JS_SUBDIRS := $(wildcard js-libp2p/*/.)

all: $(RUST_SUBDIRS) $(GO_SUBDIRS) $(HTTPS_SUBDIRS) $(QUIC_GO_SUBDIRS) $(JS_SUBDIRS)
all: $(RUST_SUBDIRS) $(GO_SUBDIRS) $(HTTPS_SUBDIRS) $(QUIC_GO_SUBDIRS) $(NIM_SUBDIRS) $(JS_SUBDIRS)

$(RUST_SUBDIRS):
$(MAKE) -C $@
$(GO_SUBDIRS):
Expand All @@ -13,12 +15,14 @@ $(HTTPS_SUBDIRS):
$(MAKE) -C $@
$(QUIC_GO_SUBDIRS):
$(MAKE) -C $@
$(NIM_SUBDIRS):
$(MAKE) -C $@
$(JS_SUBDIRS):
$(MAKE) -C $@

clean: $(RUST_SUBDIRS:%=%clean) $(GO_SUBDIRS:%=%clean) $(HTTPS_SUBDIRS:%=%clean) $(QUIC_GO_SUBDIRS:%=%clean) $(JS_SUBDIRS:%=%clean)
clean: $(RUST_SUBDIRS:%=%clean) $(GO_SUBDIRS:%=%clean) $(HTTPS_SUBDIRS:%=%clean) $(QUIC_GO_SUBDIRS:%=%clean) $(NIM_SUBDIRS:%=%clean) $(JS_SUBDIRS:%=%clean)

%clean:
$(MAKE) -C $* clean

.PHONY: $(RUST_SUBDIRS) $(GO_SUBDIRS) $(HTTPS_SUBDIRS) $(QUIC_GO_SUBDIRS) $(JS_SUBDIRS) all clean
.PHONY: $(RUST_SUBDIRS) $(GO_SUBDIRS) $(HTTPS_SUBDIRS) $(QUIC_GO_SUBDIRS) $(NIM_SUBDIRS) $(JS_SUBDIRS) all clean
29 changes: 29 additions & 0 deletions perf/impl/nim-libp2p/v1.1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
commitSha := 61929aed6c49b4753dbb8414b9c3ed572b4a2abc

all: perf

perf: perf.nim nim-libp2p
docker run --rm \
-v "$(shell pwd)":/usr/src/myapp -w /usr/src/myapp nimlang/nim:1.6.14 \
sh -c ' \
cd nim-libp2p && \
nimble install_pinned && cd ../ && \
nim c --NimblePath:nim-libp2p/nimbledeps/pkgs -p:nim-libp2p -d:chronicles_log_level=WARN --threads:off -d:release perf.nim && \
chown -R $(shell id -u):$(shell id -g) .'

nim-libp2p: nim-libp2p-${commitSha}
rm -rf nim-libp2p
ln -s nim-libp2p-${commitSha} nim-libp2p

nim-libp2p-${commitSha}: nim-libp2p-${commitSha}.zip
unzip -o nim-libp2p-${commitSha}.zip

nim-libp2p-${commitSha}.zip:
wget -O $@ "https://github.com/status-im/nim-libp2p/archive/${commitSha}.zip"

clean:
rm -f perf
rm -f nim-libp2p
rm -rf nim-libp2p-*

.PHONY: all clean
87 changes: 87 additions & 0 deletions perf/impl/nim-libp2p/v1.1/perf.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os, strutils, strformat
import chronos, bearssl/rand, bearssl/hash
import ./nim-libp2p/libp2p,
./nim-libp2p/libp2p/protocols/perf/client,
./nim-libp2p/libp2p/protocols/perf/server,
./nim-libp2p/libp2p/protocols/perf/core

const fixedPeerId = "12D3KooWPnQpbXGqzgESFrkaFh1xvCrB64ADnLQQRYfMhnbSuFHF"

type
Flags = object
runServer: bool
serverIpAddress: TransportAddress
transport: string
uploadBytes: uint
downloadBytes: uint

proc seededRng(): ref HmacDrbgContext =
var seed: cint = 0
var rng = (ref HmacDrbgContext)()
hmacDrbgInit(rng[], addr sha256Vtable, cast[pointer](addr seed), sizeof(seed).uint)
return rng

proc runServer(address: TransportAddress) {.async.} =
let endlessFut = newFuture[void]()
var switch = SwitchBuilder.new()
.withRng(seededRng())
.withAddresses(@[ MultiAddress.init(address).tryGet() ])
.withTcpTransport()
# .withQuicTransport() TODO: Remove comment when quic transport is done
.withMplex()
.withNoise()
.build()
switch.mount(Perf.new())
await switch.start()
await endlessFut # Await forever, exit on interrupt

proc runClient(f: Flags) {.async.} =
let switchBuilder = SwitchBuilder.new()
.withRng(newRng())
.withAddress(MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet())
.withMplex()
.withNoise()
let switch =
case f.transport:
of "tcp": switchBuilder.withTcpTransport().build()
# TODO: Remove comment when quic transport is done
# of "quic": switchBuilder.withQuicTransport().build()
else: raise (ref Defect)()
await switch.start()
let start = Moment.now()
let conn = await switch.dial(PeerId.init(fixedPeerId).tryGet(),
@[ MultiAddress.init(f.serverIpAddress).tryGet() ],
PerfCodec)
var dur = Moment.now() - start
dur = dur + (await PerfClient.perf(conn, f.uploadBytes, f.downloadBytes))
let ns = dur.nanos
let s = Second.nanos
echo "{\"latency\": ", fmt"{ns div s}.{ns mod s:09}", "}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the output format has changed with #276.

The pull request updated the documentation. You can find the new output format here:

https://github.com/libp2p/test-plans/blob/master/perf/README.md#adding-a-new-implementation-or-a-new-version


proc main() {.async.} =
var i = 1
var flags = Flags(transport: "tcp")
while i < paramCount():
case paramStr(i)
of "--run-server": flags.runServer = true
of "--server-ip-address":
flags.serverIpAddress = initTAddress(paramStr(i + 1))
i += 1
of "--transport":
flags.transport = paramStr(i + 1)
i += 1
of "--upload-bytes":
flags.uploadBytes = parseUInt(paramStr(i + 1))
i += 1
of "--download-bytes":
flags.downloadBytes = parseUInt(paramStr(i + 1))
i += 1
else: discard
i += 1

if flags.runServer:
await runServer(flags.serverIpAddress)
else:
await runClient(flags)

waitFor(main())
2 changes: 1 addition & 1 deletion perf/runner/benchmark-results.json
Original file line number Diff line number Diff line change
Expand Up @@ -4394,4 +4394,4 @@
4120000000
]
}
}
}
7 changes: 6 additions & 1 deletion perf/runner/src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ export const versions: Array<Version> = [
implementation: "go-libp2p",
transportStacks: ["tcp", "quic-v1"]
},
{
id: "v1.1",
implementation: "nim-libp2p",
transportStacks: ["tcp"]
},
{
id: "v0.46",
implementation: "js-libp2p",
transportStacks: ["tcp"]
}
},
]