From ab3c288c8b6c983f82bc2091d30048fc8a7ad9ab Mon Sep 17 00:00:00 2001 From: Dustin Stiles Date: Sun, 9 Mar 2025 10:21:48 -0400 Subject: [PATCH] add bench for handshake --- shared/dnet/bench.txt | 7 ++++ shared/dnet/handshake_test.go | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 shared/dnet/bench.txt diff --git a/shared/dnet/bench.txt b/shared/dnet/bench.txt new file mode 100644 index 0000000..d1d74c4 --- /dev/null +++ b/shared/dnet/bench.txt @@ -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 diff --git a/shared/dnet/handshake_test.go b/shared/dnet/handshake_test.go index 0c22570..bc33b79 100644 --- a/shared/dnet/handshake_test.go +++ b/shared/dnet/handshake_test.go @@ -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 +}