github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/crypto/spipe/spipe_test.go (about)

     1  package spipe
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
     7  
     8  	ci "github.com/jbenet/go-ipfs/crypto"
     9  	"github.com/jbenet/go-ipfs/peer"
    10  	"github.com/jbenet/go-ipfs/util"
    11  	"github.com/jbenet/go-ipfs/util/pipes"
    12  )
    13  
    14  func getPeer(tb testing.TB) peer.Peer {
    15  	privk, pubk, err := ci.GenerateKeyPair(ci.RSA, 1024)
    16  	if err != nil {
    17  		tb.Fatal(err)
    18  	}
    19  
    20  	p, err := peer.WithKeyPair(privk, pubk)
    21  	if err != nil {
    22  		tb.Fatal(err)
    23  	}
    24  
    25  	return p
    26  }
    27  
    28  func bindDuplexNoCopy(a, b pipes.Duplex) {
    29  	go func() {
    30  		for m := range b.Out {
    31  			a.In <- m
    32  		}
    33  	}()
    34  	for m := range a.Out {
    35  		b.In <- m
    36  	}
    37  }
    38  
    39  var globuf = make([]byte, 4*1024*1024)
    40  
    41  func bindDuplexWithCopy(a, b pipes.Duplex) {
    42  	dup := func(byt []byte) []byte {
    43  		n := globuf[:len(byt)]
    44  		copy(n, byt)
    45  		return n
    46  	}
    47  	go func() {
    48  		for m := range b.Out {
    49  			a.In <- dup(m)
    50  		}
    51  	}()
    52  	for m := range a.Out {
    53  		b.In <- dup(m)
    54  	}
    55  }
    56  
    57  func BenchmarkDataEncryptDefault(b *testing.B) {
    58  	SupportedExchanges = "P-256,P-224,P-384,P-521"
    59  	SupportedCiphers = "AES-256,AES-128"
    60  	SupportedHashes = "SHA256,SHA512,SHA1"
    61  
    62  	runEncryptBenchmark(b)
    63  }
    64  
    65  func BenchmarkDataEncryptLite(b *testing.B) {
    66  	SupportedExchanges = "P-256"
    67  	SupportedCiphers = "AES-128"
    68  	SupportedHashes = "SHA1"
    69  
    70  	runEncryptBenchmark(b)
    71  }
    72  
    73  func BenchmarkDataEncryptBlowfish(b *testing.B) {
    74  	SupportedExchanges = "P-256"
    75  	SupportedCiphers = "Blowfish"
    76  	SupportedHashes = "SHA1"
    77  
    78  	runEncryptBenchmark(b)
    79  }
    80  
    81  func runEncryptBenchmark(b *testing.B) {
    82  	pstore := peer.NewPeerstore()
    83  	ctx := context.TODO()
    84  	bufsize := 1024 * 1024
    85  
    86  	pa := getPeer(b)
    87  	pb := getPeer(b)
    88  	duplexa := pipes.NewDuplex(16)
    89  	duplexb := pipes.NewDuplex(16)
    90  
    91  	go bindDuplexNoCopy(duplexa, duplexb)
    92  
    93  	var spb *SecurePipe
    94  	done := make(chan struct{})
    95  	go func() {
    96  		var err error
    97  		spb, err = NewSecurePipe(ctx, bufsize, pb, pstore, duplexb)
    98  		if err != nil {
    99  			b.Fatal(err)
   100  		}
   101  		done <- struct{}{}
   102  	}()
   103  
   104  	spa, err := NewSecurePipe(ctx, bufsize, pa, pstore, duplexa)
   105  	if err != nil {
   106  		b.Fatal(err)
   107  	}
   108  
   109  	<-done
   110  
   111  	go func() {
   112  		for _ = range spa.In {
   113  			// Throw it all away,
   114  			// all of your hopes and dreams
   115  			// piped out to /dev/null...
   116  			done <- struct{}{}
   117  		}
   118  	}()
   119  
   120  	data := make([]byte, 1024*512)
   121  	util.NewTimeSeededRand().Read(data)
   122  	// Begin actual benchmarking
   123  	b.ResetTimer()
   124  
   125  	for i := 0; i < b.N; i++ {
   126  		b.SetBytes(int64(len(data)))
   127  		spb.Out <- data
   128  		<-done
   129  	}
   130  
   131  }
   132  
   133  func BenchmarkDataTransfer(b *testing.B) {
   134  	duplexa := pipes.NewDuplex(16)
   135  	duplexb := pipes.NewDuplex(16)
   136  
   137  	go bindDuplexWithCopy(duplexa, duplexb)
   138  
   139  	done := make(chan struct{})
   140  	go func() {
   141  		for _ = range duplexa.In {
   142  			// Throw it all away,
   143  			// all of your hopes and dreams
   144  			// piped out to /dev/null...
   145  			done <- struct{}{}
   146  		}
   147  	}()
   148  
   149  	data := make([]byte, 1024*512)
   150  	util.NewTimeSeededRand().Read(data)
   151  	// Begin actual benchmarking
   152  	b.ResetTimer()
   153  
   154  	for i := 0; i < b.N; i++ {
   155  		b.SetBytes(int64(len(data)))
   156  		duplexb.Out <- data
   157  		<-done
   158  	}
   159  
   160  }