github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/crypto/ssh/benchmark_test.go (about)

     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  
     5  package ssh
     6  
     7  import (
     8  	"errors"
     9  	"io"
    10  	"net"
    11  	"testing"
    12  )
    13  
    14  type server struct {
    15  	*ServerConn
    16  	chans <-chan NewChannel
    17  }
    18  
    19  func newServer(c net.Conn, conf *ServerConfig) (*server, error) {
    20  	sconn, chans, reqs, err := NewServerConn(c, conf)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	go DiscardRequests(reqs)
    25  	return &server{sconn, chans}, nil
    26  }
    27  
    28  func (s *server) Accept() (NewChannel, error) {
    29  	n, ok := <-s.chans
    30  	if !ok {
    31  		return nil, io.EOF
    32  	}
    33  	return n, nil
    34  }
    35  
    36  func sshPipe() (Conn, *server, error) {
    37  	c1, c2, err := netPipe()
    38  	if err != nil {
    39  		return nil, nil, err
    40  	}
    41  
    42  	clientConf := ClientConfig{
    43  		User:            "user",
    44  		HostKeyCallback: InsecureIgnoreHostKey(),
    45  	}
    46  	serverConf := ServerConfig{
    47  		NoClientAuth: true,
    48  	}
    49  	serverConf.AddHostKey(testSigners["ecdsa"])
    50  	done := make(chan *server, 1)
    51  	go func() {
    52  		server, err := newServer(c2, &serverConf)
    53  		if err != nil {
    54  			done <- nil
    55  		}
    56  		done <- server
    57  	}()
    58  
    59  	client, _, reqs, err := NewClientConn(c1, "", &clientConf)
    60  	if err != nil {
    61  		return nil, nil, err
    62  	}
    63  
    64  	server := <-done
    65  	if server == nil {
    66  		return nil, nil, errors.New("server handshake failed.")
    67  	}
    68  	go DiscardRequests(reqs)
    69  
    70  	return client, server, nil
    71  }
    72  
    73  func BenchmarkEndToEnd(b *testing.B) {
    74  	b.StopTimer()
    75  
    76  	client, server, err := sshPipe()
    77  	if err != nil {
    78  		b.Fatalf("sshPipe: %v", err)
    79  	}
    80  
    81  	defer client.Close()
    82  	defer server.Close()
    83  
    84  	size := (1 << 20)
    85  	input := make([]byte, size)
    86  	output := make([]byte, size)
    87  	b.SetBytes(int64(size))
    88  	done := make(chan int, 1)
    89  
    90  	go func() {
    91  		newCh, err := server.Accept()
    92  		if err != nil {
    93  			b.Fatalf("Client: %v", err)
    94  		}
    95  		ch, incoming, err := newCh.Accept()
    96  		if err != nil {
    97  			b.Fatalf("Accept: %v", err)
    98  		}
    99  		go DiscardRequests(incoming)
   100  		for i := 0; i < b.N; i++ {
   101  			if _, err := io.ReadFull(ch, output); err != nil {
   102  				b.Fatalf("ReadFull: %v", err)
   103  			}
   104  		}
   105  		ch.Close()
   106  		done <- 1
   107  	}()
   108  
   109  	ch, in, err := client.OpenChannel("speed", nil)
   110  	if err != nil {
   111  		b.Fatalf("OpenChannel: %v", err)
   112  	}
   113  	go DiscardRequests(in)
   114  
   115  	b.ResetTimer()
   116  	b.StartTimer()
   117  	for i := 0; i < b.N; i++ {
   118  		if _, err := ch.Write(input); err != nil {
   119  			b.Fatalf("WriteFull: %v", err)
   120  		}
   121  	}
   122  	ch.Close()
   123  	b.StopTimer()
   124  
   125  	<-done
   126  }