add bench for handshake

This commit is contained in:
Dustin Stiles 2025-03-09 10:21:48 -04:00
parent 9f35fd4025
commit ab3c288c8b
Signed by: duwstiles
GPG Key ID: BCD9912EC231FC87
2 changed files with 67 additions and 0 deletions

7
shared/dnet/bench.txt Normal file
View File

@ -0,0 +1,7 @@
goos: linux
goarch: amd64
pkg: koti.casa/numenor-labs/dsfx/shared/dnet
cpu: Intel(R) Core(TM) Ultra 9 185H
BenchmarkHandshake-22 537 2179739 ns/op
PASS
ok koti.casa/numenor-labs/dsfx/shared/dnet 1.182s

View File

@ -4,7 +4,9 @@ import (
"bytes"
"context"
"crypto/ecdsa"
"fmt"
"net"
"os"
"sync"
"testing"
@ -79,3 +81,61 @@ func TestHandshake(t *testing.T) {
return
}
}
func BenchmarkHandshake(b *testing.B) {
for b.Loop() {
if err := runSimulation(); err != nil {
fmt.Fprint(os.Stderr, err)
}
}
}
func runSimulation() error {
ctx := context.Background()
// alice, represented by an ecdsa key pair.
alice, _ := dcrypto.GenerateSigningKey()
// bob, also represented by an ecdsa key pair.
bob, _ := dcrypto.GenerateSigningKey()
var (
// the secret that alice should arrive at on her own
// aliceSecret []byte
// any errors produce by alice
aliceErr error
// alice's public key as discovered by bob
// discoveredAlicePublicKey *ecdsa.PublicKey
// the secret that bob should arrive at on his own
// bobSecret []byte
// any errors produce by bob
bobErr error
)
// Create a network pipe to simulate a network connection between alice and bob.
client, server := net.Pipe()
defer client.Close()
defer server.Close()
// Run the handshake in parallel so both sides can proceed concurrently.
// Since they're talking through the network pipe, the entire process should
// be simulated as if it were a real network connection.
var wg sync.WaitGroup
wg.Add(2)
go func() {
_, aliceErr = dnet.Handshake(ctx, client, alice, &bob.PublicKey)
wg.Done()
}()
go func() {
_, _, bobErr = dnet.AcceptHandshake(ctx, server, bob)
wg.Done()
}()
wg.Wait()
if aliceErr != nil {
return aliceErr
}
if bobErr != nil {
return bobErr
}
return nil
}