You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.2 KiB

  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. }
  38. serverConf := ServerConfig{
  39. NoClientAuth: true,
  40. }
  41. serverConf.AddHostKey(testSigners["ecdsa"])
  42. done := make(chan *server, 1)
  43. go func() {
  44. server, err := newServer(c2, &serverConf)
  45. if err != nil {
  46. done <- nil
  47. }
  48. done <- server
  49. }()
  50. client, _, reqs, err := NewClientConn(c1, "", &clientConf)
  51. if err != nil {
  52. return nil, nil, err
  53. }
  54. server := <-done
  55. if server == nil {
  56. return nil, nil, errors.New("server handshake failed.")
  57. }
  58. go DiscardRequests(reqs)
  59. return client, server, nil
  60. }
  61. func BenchmarkEndToEnd(b *testing.B) {
  62. b.StopTimer()
  63. client, server, err := sshPipe()
  64. if err != nil {
  65. b.Fatalf("sshPipe: %v", err)
  66. }
  67. defer client.Close()
  68. defer server.Close()
  69. size := (1 << 20)
  70. input := make([]byte, size)
  71. output := make([]byte, size)
  72. b.SetBytes(int64(size))
  73. done := make(chan int, 1)
  74. go func() {
  75. newCh, err := server.Accept()
  76. if err != nil {
  77. b.Fatalf("Client: %v", err)
  78. }
  79. ch, incoming, err := newCh.Accept()
  80. go DiscardRequests(incoming)
  81. for i := 0; i < b.N; i++ {
  82. if _, err := io.ReadFull(ch, output); err != nil {
  83. b.Fatalf("ReadFull: %v", err)
  84. }
  85. }
  86. ch.Close()
  87. done <- 1
  88. }()
  89. ch, in, err := client.OpenChannel("speed", nil)
  90. if err != nil {
  91. b.Fatalf("OpenChannel: %v", err)
  92. }
  93. go DiscardRequests(in)
  94. b.ResetTimer()
  95. b.StartTimer()
  96. for i := 0; i < b.N; i++ {
  97. if _, err := ch.Write(input); err != nil {
  98. b.Fatalf("WriteFull: %v", err)
  99. }
  100. }
  101. ch.Close()
  102. b.StopTimer()
  103. <-done
  104. }