benchmark_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (C) 2016 The Protocol Authors.
  2. package protocol
  3. import (
  4. "context"
  5. "crypto/tls"
  6. "encoding/binary"
  7. "net"
  8. "testing"
  9. "github.com/syncthing/syncthing/lib/dialer"
  10. )
  11. func BenchmarkRequestsRawTCP(b *testing.B) {
  12. // Benchmarks the rate at which we can serve requests over a single,
  13. // unencrypted TCP channel over the loopback interface.
  14. // Get a connected TCP pair
  15. conn0, conn1, err := getTCPConnectionPair()
  16. if err != nil {
  17. b.Fatal(err)
  18. }
  19. defer conn0.Close()
  20. defer conn1.Close()
  21. // Bench it
  22. benchmarkRequestsConnPair(b, conn0, conn1)
  23. }
  24. func BenchmarkRequestsTLSoTCP(b *testing.B) {
  25. conn0, conn1, err := getTCPConnectionPair()
  26. if err != nil {
  27. b.Fatal(err)
  28. }
  29. defer conn0.Close()
  30. defer conn1.Close()
  31. benchmarkRequestsTLS(b, conn0, conn1)
  32. }
  33. func benchmarkRequestsTLS(b *testing.B, conn0, conn1 net.Conn) {
  34. // Benchmarks the rate at which we can serve requests over a single,
  35. // TLS encrypted channel over the loopback interface.
  36. // Load a certificate, skipping this benchmark if it doesn't exist
  37. cert, err := tls.LoadX509KeyPair("../../test/h1/cert.pem", "../../test/h1/key.pem")
  38. if err != nil {
  39. b.Skip(err)
  40. return
  41. }
  42. /// TLSify them
  43. conn0, conn1 = negotiateTLS(cert, conn0, conn1)
  44. // Bench it
  45. benchmarkRequestsConnPair(b, conn0, conn1)
  46. }
  47. func benchmarkRequestsConnPair(b *testing.B, conn0, conn1 net.Conn) {
  48. // Start up Connections on them
  49. c0 := NewConnection(LocalDeviceID, conn0, conn0, new(fakeModel), "c0", CompressMetadata)
  50. c0.Start()
  51. c1 := NewConnection(LocalDeviceID, conn1, conn1, new(fakeModel), "c1", CompressMetadata)
  52. c1.Start()
  53. // Satisfy the assertions in the protocol by sending an initial cluster config
  54. c0.ClusterConfig(ClusterConfig{})
  55. c1.ClusterConfig(ClusterConfig{})
  56. // Report some useful stats and reset the timer for the actual test
  57. b.ReportAllocs()
  58. b.SetBytes(128 << 10)
  59. b.ResetTimer()
  60. // Request 128 KiB blocks, which will be satisfied by zero copy from the
  61. // other side (we'll get back a full block of zeroes).
  62. var buf []byte
  63. var err error
  64. for i := 0; i < b.N; i++ {
  65. // Use c0 and c1 for each alternating request, so we get as much
  66. // data flowing in both directions.
  67. if i%2 == 0 {
  68. buf, err = c0.Request(context.Background(), "folder", "file", int64(i), 128<<10, nil, 0, false)
  69. } else {
  70. buf, err = c1.Request(context.Background(), "folder", "file", int64(i), 128<<10, nil, 0, false)
  71. }
  72. if err != nil {
  73. b.Fatal(err)
  74. }
  75. if len(buf) != 128<<10 {
  76. b.Fatal("Incorrect returned buf length", len(buf), "!=", 128<<10)
  77. }
  78. // The fake model is supposed to tag the end of the buffer with the
  79. // requested offset, so we can verify that we get back data for this
  80. // block correctly.
  81. if binary.BigEndian.Uint64(buf[128<<10-8:]) != uint64(i) {
  82. b.Fatal("Bad data returned")
  83. }
  84. }
  85. }
  86. // returns the two endpoints of a TCP connection over lo0
  87. func getTCPConnectionPair() (net.Conn, net.Conn, error) {
  88. lst, err := net.Listen("tcp", "127.0.0.1:0")
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. // We run the Accept in the background since it's blocking, and we use
  93. // the channel to make the race thingies happy about writing vs reading
  94. // conn0 and err0.
  95. var conn0 net.Conn
  96. var err0 error
  97. done := make(chan struct{})
  98. go func() {
  99. conn0, err0 = lst.Accept()
  100. close(done)
  101. }()
  102. // Dial the connection
  103. conn1, err := net.Dial("tcp", lst.Addr().String())
  104. if err != nil {
  105. return nil, nil, err
  106. }
  107. // Check any error from accept
  108. <-done
  109. if err0 != nil {
  110. return nil, nil, err0
  111. }
  112. // Set the buffer sizes etc as usual
  113. dialer.SetTCPOptions(conn0)
  114. dialer.SetTCPOptions(conn1)
  115. return conn0, conn1, nil
  116. }
  117. func negotiateTLS(cert tls.Certificate, conn0, conn1 net.Conn) (net.Conn, net.Conn) {
  118. cfg := &tls.Config{
  119. Certificates: []tls.Certificate{cert},
  120. NextProtos: []string{"bep/1.0"},
  121. ClientAuth: tls.RequestClientCert,
  122. SessionTicketsDisabled: true,
  123. InsecureSkipVerify: true,
  124. MinVersion: tls.VersionTLS12,
  125. CipherSuites: []uint16{
  126. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  127. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  128. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  129. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  130. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  131. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  132. },
  133. }
  134. tlsc0 := tls.Server(conn0, cfg)
  135. tlsc1 := tls.Client(conn1, cfg)
  136. return tlsc0, tlsc1
  137. }
  138. // The fake model does nothing much
  139. type fakeModel struct{}
  140. func (m *fakeModel) Index(deviceID DeviceID, folder string, files []FileInfo) error {
  141. return nil
  142. }
  143. func (m *fakeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) error {
  144. return nil
  145. }
  146. func (m *fakeModel) Request(deviceID DeviceID, folder, name string, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error) {
  147. // We write the offset to the end of the buffer, so the receiver
  148. // can verify that it did in fact get some data back over the
  149. // connection.
  150. buf := make([]byte, size)
  151. binary.BigEndian.PutUint64(buf[len(buf)-8:], uint64(offset))
  152. return &fakeRequestResponse{buf}, nil
  153. }
  154. func (m *fakeModel) ClusterConfig(deviceID DeviceID, config ClusterConfig) error {
  155. return nil
  156. }
  157. func (m *fakeModel) Closed(conn Connection, err error) {
  158. }
  159. func (m *fakeModel) DownloadProgress(deviceID DeviceID, folder string, updates []FileDownloadProgressUpdate) error {
  160. return nil
  161. }