Skip to content

Commit

Permalink
Adjust uTP benchmarking tests to use multiple nodes (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeme authored Feb 7, 2025
1 parent c640d3c commit 51f56b0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
58 changes: 34 additions & 24 deletions tests/utp/test_discv5_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,10 @@ procSuite "uTP over discovery v5 protocol":
await node1.closeWait()
await node2.closeWait()

asyncTest "Data transfer over multiple sockets":
asyncTest "Data transfer over multiple sockets over multiple nodes":
const
amountOfTransfers = 25
amountOfTransfers = 5
amountOfNodes = 5
dataToSend: seq[byte] = repeat(byte 0xA0, 1_000_000)

var readFutures: seq[Future[void]]
Expand Down Expand Up @@ -268,21 +269,26 @@ procSuite "uTP over discovery v5 protocol":
noCancel fut

let
address1 = localAddress(20302)
address2 = localAddress(20303)
node1 = initDiscoveryNode(
rng, PrivateKey.random(rng[]), address1)
node2 = initDiscoveryNode(
rng, PrivateKey.random(rng[]), address2)

utp1 = UtpDiscv5Protocol.new(
node1, utpProtId, handleIncomingConnectionDummy)
utp2 {.used.} = UtpDiscv5Protocol.new(
node2, utpProtId, handleIncomingConnection)

# nodes must have session between each other
check:
(await node1.ping(node2.localNode)).isOk()
sendingNode = initDiscoveryNode(
rng, PrivateKey.random(rng[]), localAddress(20302))
sendingUtpNode = UtpDiscv5Protocol.new(
sendingNode, utpProtId, handleIncomingConnectionDummy)

var nodeList: seq[discv5_protocol.Protocol]
var utpNodeList: seq[UtpDiscv5Protocol]
for i in 0..<amountOfNodes:
let
node = initDiscoveryNode(
rng, PrivateKey.random(rng[]), localAddress(20303 + i))
utpNode = UtpDiscv5Protocol.new(
node, utpProtId, handleIncomingConnection)

nodeList.add(node)
utpNodeList.add(utpNode)

# nodes must have discv5 session between each other
check:
(await sendingNode.ping(node.localNode)).isOk()

proc connectSendAndCheck(
utpProto: UtpDiscv5Protocol,
Expand All @@ -301,22 +307,26 @@ procSuite "uTP over discovery v5 protocol":

let t0 = Moment.now()
for i in 0..<amountOfTransfers:
asyncSpawn utp1.connectSendAndCheck(
NodeAddress.init(node2.localNode.id, address2))
for j in 0..<amountOfNodes:
asyncSpawn sendingUtpNode.connectSendAndCheck(
NodeAddress.init(nodeList[j].localNode.id, nodeList[j].localNode.address.value()))

while readFutures.len() < amountOfTransfers:
while readFutures.len() < amountOfTransfers * amountOfNodes:
await sleepAsync(milliseconds(100))

await allFutures(readFutures)
let elapsed = Moment.now() - t0

await utp1.shutdownWait()
await utp2.shutdownWait()
await sendingUtpNode.shutdownWait()
await sendingNode.closeWait()
for i in 0..<amountOfNodes:
await utpNodeList[i].shutdownWait()
await nodeList[i].closeWait()

let megabitsSent = amountOfTransfers * dataToSend.len() * 8 / 1_000_000
let megabitsSent = amountOfTransfers * amountOfNodes * dataToSend.len() * 8 / 1_000_000
let seconds = float(elapsed.nanoseconds) / 1_000_000_000
let throughput = megabitsSent / seconds

echo ""
echo "Sent ", amountOfTransfers, " asynchronous uTP transfers in ", seconds,
echo "Sent ", amountOfTransfers, " asynchronous uTP transfers to ", amountOfNodes, " nodes in ", seconds,
" seconds, payload throughput: ", throughput, " Mbit/s"
33 changes: 21 additions & 12 deletions tests/utp/test_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,10 @@ procSuite "uTP over UDP protocol":

await s.close()

asyncTest "Data transfer over multiple sockets":
asyncTest "Data transfer over multiple sockets over multiple nodes":
const
amountOfTransfers = 100
amountOfTransfers = 20
amountOfNodes = 5
dataToSend: seq[byte] = repeat(byte 0xA0, 1_000_000)

var readFutures: seq[Future[void]]
Expand Down Expand Up @@ -585,10 +586,16 @@ procSuite "uTP over UDP protocol":
noCancel fut

let
address1 = initTAddress("127.0.0.1", 9079)
utpProto1 = UtpProtocol.new(handleIncomingConnectionDummy, address1)
address2 = initTAddress("127.0.0.1", 9080)
utpProto2 = UtpProtocol.new(handleIncomingConnection, address2)
address = initTAddress("127.0.0.1", 9079)
utpNode = UtpProtocol.new(handleIncomingConnectionDummy, address)

var utpNodeList: seq[UtpProtocol]
for i in 0..<amountOfNodes:
let
address = initTAddress("127.0.0.1", 9080 + i)
utpNode = UtpProtocol.new(handleIncomingConnection, address)

utpNodeList.add(utpNode)

proc connectSendAndCheck(
utpProto: UtpProtocol,
Expand All @@ -607,21 +614,23 @@ procSuite "uTP over UDP protocol":

let t0 = Moment.now()
for i in 0..<amountOfTransfers:
asyncSpawn utpProto1.connectSendAndCheck(address2)
for j in 0..<amountOfNodes:
asyncSpawn utpNode.connectSendAndCheck(initTAddress("127.0.0.1", 9080 + j))

while readFutures.len() < amountOfTransfers:
while readFutures.len() < amountOfTransfers * amountOfNodes:
await sleepAsync(milliseconds(100))

await allFutures(readFutures)
let elapsed = Moment.now() - t0

await utpProto1.shutdownWait()
await utpProto2.shutdownWait()
await utpNode.shutdownWait()
for i in 0..<amountOfNodes:
await utpNodeList[i].shutdownWait()

let megabitsSent = amountOfTransfers * dataToSend.len() * 8 / 1_000_000
let megabitsSent = amountOfTransfers * amountOfNodes * dataToSend.len() * 8 / 1_000_000
let seconds = float(elapsed.nanoseconds) / 1_000_000_000
let throughput = megabitsSent / seconds

echo ""
echo "Sent ", amountOfTransfers, " asynchronous uTP transfers in ", seconds,
echo "Sent ", amountOfTransfers, " asynchronous uTP transfers to ", amountOfNodes, " nodes in ", seconds,
" seconds, payload throughput: ", throughput, " Mbit/s"

0 comments on commit 51f56b0

Please sign in to comment.