...

Source file src/github.com/ThalesIgnite/crypto11/thread_test.go

Documentation: github.com/ThalesIgnite/crypto11

     1  // Copyright 2016, 2017 Thales e-Security, Inc
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining
     4  // a copy of this software and associated documentation files (the
     5  // "Software"), to deal in the Software without restriction, including
     6  // without limitation the rights to use, copy, modify, merge, publish,
     7  // distribute, sublicense, and/or sell copies of the Software, and to
     8  // permit persons to whom the Software is furnished to do so, subject to
     9  // the following conditions:
    10  //
    11  // The above copyright notice and this permission notice shall be
    12  // included in all copies or substantial portions of the Software.
    13  //
    14  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    15  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    16  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    17  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    18  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    19  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    20  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    21  
    22  package crypto11
    23  
    24  import (
    25  	"crypto"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  var threadCount = 32
    33  var signaturesPerThread = 256
    34  
    35  func TestThreadedRSA(t *testing.T) {
    36  	if testing.Short() {
    37  		t.Skip()
    38  	}
    39  
    40  	ctx, err := ConfigureFromFile("config")
    41  	require.NoError(t, err)
    42  
    43  	defer func() {
    44  		require.NoError(t, ctx.Close())
    45  	}()
    46  
    47  	id := randomBytes()
    48  	key, err := ctx.GenerateRSAKeyPair(id, rsaSize)
    49  	require.NoError(t, err)
    50  	defer func(k Signer) { _ = k.Delete() }(key)
    51  
    52  	done := make(chan int)
    53  	started := time.Now()
    54  
    55  	t.Logf("Starting %v threads", threadCount)
    56  
    57  	for i := 0; i < threadCount; i++ {
    58  		go signingRoutine(t, key, done)
    59  
    60  		// CloudHSM falls over if you create sessions too quickly
    61  		time.Sleep(50 * time.Millisecond)
    62  	}
    63  	t.Logf("Waiting for %v threads", threadCount)
    64  	for i := 0; i < threadCount; i++ {
    65  		<-done
    66  	}
    67  	finished := time.Now()
    68  	ticks := finished.Sub(started)
    69  	elapsed := float64(ticks) / 1000000000.0
    70  	t.Logf("Made %v signatures in %v elapsed (%v/s)",
    71  		threadCount*signaturesPerThread,
    72  		elapsed, float64(threadCount*signaturesPerThread)/elapsed)
    73  }
    74  
    75  func signingRoutine(t *testing.T, key crypto.Signer, done chan int) {
    76  	for i := 0; i < signaturesPerThread; i++ {
    77  		testRsaSigningPKCS1v15(t, key, crypto.SHA1)
    78  
    79  		// CloudHSM falls over if you create sessions too quickly
    80  		time.Sleep(50 * time.Millisecond)
    81  	}
    82  	done <- 1
    83  }
    84  

View as plain text