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