benchmark_test.go 5.0 KB

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