github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/test/tools/LTE/experiments/insert_txs_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package experiments
    18  
    19  import (
    20  	"sync"
    21  	"testing"
    22  
    23  	"fmt"
    24  
    25  	"github.com/hyperledger/fabric/test/tools/LTE/chainmgmt"
    26  	"github.com/hyperledger/fabric/test/tools/LTE/common"
    27  )
    28  
    29  // BenchmarkInsertTxs starts fresh chains and inserts the Key-values by simulating writes-only transactions
    30  // For each of the chains, this test launches the parallel clients (based on the configuration) and the clients
    31  // simulate and commit write-only transactions. The keys are divided among clients such that one key is written
    32  // only once and all the keys are inserted.
    33  //
    34  // For instance, if this benchmark is invoked with the following test parameters
    35  // -testParams=-NumChains=2, -NumParallelTxPerChain=2, -NumKVs=100
    36  // then client_1 on chain_1 will insert Key_1 to key_25 and client_2 on chain_1 will insert Key_26 to key_50
    37  // similarly client_3 on chain_2 will insert Key_1 to key_25 and client_4 on chain_2 will insert Key_26 to key_50
    38  // where, client_1 and client_2 run in parallel on chain_1 and client_3 and client_4 run in parallel on chain_2
    39  func BenchmarkInsertTxs(b *testing.B) {
    40  	if b.N != 1 {
    41  		panic(fmt.Errorf(`This benchmark should be called with N=1 only. Run this with more volume of data`))
    42  	}
    43  	testEnv := chainmgmt.InitTestEnv(conf.chainMgrConf, conf.batchConf, chainmgmt.ChainInitOpCreate)
    44  	for _, chain := range testEnv.Chains() {
    45  		go runInsertClientsForChain(chain)
    46  	}
    47  	testEnv.WaitForTestCompletion()
    48  }
    49  
    50  func runInsertClientsForChain(chain *chainmgmt.Chain) {
    51  	numKVsForChain := calculateShare(conf.dataConf.numKVs, conf.chainMgrConf.NumChains, int(chain.ID))
    52  	numClients := conf.txConf.numParallelTxsPerChain
    53  	wg := &sync.WaitGroup{}
    54  	wg.Add(numClients)
    55  	startKey := 0
    56  	for i := 0; i < numClients; i++ {
    57  		numKVsForClient := calculateShare(numKVsForChain, numClients, i)
    58  		endKey := startKey + numKVsForClient - 1
    59  		go runInsertClient(chain, startKey, endKey, wg)
    60  		startKey = endKey + 1
    61  	}
    62  	wg.Wait()
    63  	chain.Done()
    64  }
    65  
    66  func runInsertClient(chain *chainmgmt.Chain, startKey, endKey int, wg *sync.WaitGroup) {
    67  	numKeysPerTx := conf.txConf.numKeysInEachTx
    68  	kvSize := conf.dataConf.kvSize
    69  
    70  	currentKey := startKey
    71  	for currentKey <= endKey {
    72  		simulator, err := chain.NewTxSimulator()
    73  		common.PanicOnError(err)
    74  		for i := 0; i < numKeysPerTx; i++ {
    75  			common.PanicOnError(simulator.SetState(
    76  				chaincodeName, constructKey(currentKey), constructValue(currentKey, kvSize)))
    77  			currentKey++
    78  			if currentKey > endKey {
    79  				break
    80  			}
    81  		}
    82  		simulator.Done()
    83  		sr, err := simulator.GetTxSimulationResults()
    84  		common.PanicOnError(err)
    85  		chain.SubmitTx(sr)
    86  	}
    87  	wg.Done()
    88  }