...

Source file src/golang.org/x/crypto/ssh/benchmark_test.go

Documentation: golang.org/x/crypto/ssh

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

View as plain text