mirror of
https://git.numenor-labs.us/dsfx.git
synced 2025-04-29 08:10:34 +00:00
add bench for handshake
This commit is contained in:
parent
9f35fd4025
commit
ab3c288c8b
7
shared/dnet/bench.txt
Normal file
7
shared/dnet/bench.txt
Normal 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
|
@ -4,7 +4,9 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -79,3 +81,61 @@ func TestHandshake(t *testing.T) {
|
|||||||
return
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user