benchmark_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ssh
  5. import (
  6. "errors"
  7. "io"
  8. "net"
  9. "testing"
  10. )
  11. type server struct {
  12. *ServerConn
  13. chans <-chan NewChannel
  14. }
  15. func newServer(c net.Conn, conf *ServerConfig) (*server, error) {
  16. sconn, chans, reqs, err := NewServerConn(c, conf)
  17. if err != nil {
  18. return nil, err
  19. }
  20. go DiscardRequests(reqs)
  21. return &server{sconn, chans}, nil
  22. }
  23. func (s *server) Accept() (NewChannel, error) {
  24. n, ok := <-s.chans
  25. if !ok {
  26. return nil, io.EOF
  27. }
  28. return n, nil
  29. }
  30. func sshPipe() (Conn, *server, error) {
  31. c1, c2, err := netPipe()
  32. if err != nil {
  33. return nil, nil, err
  34. }
  35. clientConf := ClientConfig{
  36. User: "user",
  37. HostKeyCallback: InsecureIgnoreHostKey(),
  38. }
  39. serverConf := ServerConfig{
  40. NoClientAuth: true,
  41. }
  42. serverConf.AddHostKey(testSigners["ecdsa"])
  43. done := make(chan *server, 1)
  44. go func() {
  45. server, err := newServer(c2, &serverConf)
  46. if err != nil {
  47. done <- nil
  48. }
  49. done <- server
  50. }()
  51. client, _, reqs, err := NewClientConn(c1, "", &clientConf)
  52. if err != nil {
  53. return nil, nil, err
  54. }
  55. server := <-done
  56. if server == nil {
  57. return nil, nil, errors.New("server handshake failed.")
  58. }
  59. go DiscardRequests(reqs)
  60. return client, server, nil
  61. }
  62. func BenchmarkEndToEnd(b *testing.B) {
  63. b.StopTimer()
  64. client, server, err := sshPipe()
  65. if err != nil {
  66. b.Fatalf("sshPipe: %v", err)
  67. }
  68. defer client.Close()
  69. defer server.Close()
  70. size := (1 << 20)
  71. input := make([]byte, size)
  72. output := make([]byte, size)
  73. b.SetBytes(int64(size))
  74. done := make(chan int, 1)
  75. go func() {
  76. newCh, err := server.Accept()
  77. if err != nil {
  78. b.Fatalf("Client: %v", err)
  79. }
  80. ch, incoming, err := newCh.Accept()
  81. if err != nil {
  82. b.Fatalf("Accept: %v", err)
  83. }
  84. go DiscardRequests(incoming)
  85. for i := 0; i < b.N; i++ {
  86. if _, err := io.ReadFull(ch, output); err != nil {
  87. b.Fatalf("ReadFull: %v", err)
  88. }
  89. }
  90. ch.Close()
  91. done <- 1
  92. }()
  93. ch, in, err := client.OpenChannel("speed", nil)
  94. if err != nil {
  95. b.Fatalf("OpenChannel: %v", err)
  96. }
  97. go DiscardRequests(in)
  98. b.ResetTimer()
  99. b.StartTimer()
  100. for i := 0; i < b.N; i++ {
  101. if _, err := ch.Write(input); err != nil {
  102. b.Fatalf("WriteFull: %v", err)
  103. }
  104. }
  105. ch.Close()
  106. b.StopTimer()
  107. <-done
  108. }