From a8227fdba381917e5594e75abd20779ac2741141 Mon Sep 17 00:00:00 2001 From: Ben Weintraub Date: Sun, 24 Dec 2017 10:23:53 -0800 Subject: [PATCH] [FAB-7544] Refactor orderer benchmark init The performance package is used in production code, but its init function did setup for objects and variables that are only used in the orderer's benchmark mode. That means that when the performance package is imported, some unneccessary initializations were being done. This fix for [FAB-7544] moves those initializations out of the init function, so they will only happen when they are needed. No unit tests have been added because all of these lines of code are already tested by the orderer's benchmark test. Some refactoring of the benchmarkOrderer was also needed. The initializations were moved to the beginning of the function. This was needed because MakeNormalTx was failing as it was unable to get an MSP-based signer. It was working before because the the init function in the performance package had already loaded a local MSP, so when benchmarkOrderer was doing it again later, it was actually doing nothing. Change-Id: I9b222e4d5468f8b6dbb676007e357e2a457b6ac6 Signed-off-by: Ben Weintraub --- orderer/common/performance/utils.go | 25 +++++-------------------- orderer/common/server/benchmark_test.go | 18 +++++++++++------- sampleconfig/configtx.yaml | 2 +- 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/orderer/common/performance/utils.go b/orderer/common/performance/utils.go index c5447008a7a..3f51ff254a4 100644 --- a/orderer/common/performance/utils.go +++ b/orderer/common/performance/utils.go @@ -15,8 +15,7 @@ import ( "github.com/hyperledger/fabric/common/localmsp" "github.com/hyperledger/fabric/common/tools/configtxgen/encoder" genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig" - mspmgmt "github.com/hyperledger/fabric/msp/mgmt" - "github.com/hyperledger/fabric/orderer/common/localconfig" + cb "github.com/hyperledger/fabric/protos/common" ab "github.com/hyperledger/fabric/protos/orderer" protosutils "github.com/hyperledger/fabric/protos/utils" @@ -27,25 +26,9 @@ const ( Kilo = 1024 // TODO Consider adding a unit pkg ) -var conf *config.TopLevel - var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz") -var channelProfile *genesisconfig.Profile - -func init() { - rand.Seed(time.Now().UnixNano()) - - conf = config.Load() - - // Load local MSP - err := mspmgmt.LoadLocalMsp(conf.General.LocalMSPDir, conf.General.BCCSP, conf.General.LocalMSPID) - if err != nil { - panic(fmt.Errorf("Failed to initialize local MSP: %s", err)) - } - - channelProfile = genesisconfig.Load(genesisconfig.SampleSingleMSPChannelV11Profile) -} +var seedOnce sync.Once // MakeNormalTx creates a properly signed transaction that could be used against `broadcast` API func MakeNormalTx(channelID string, size int) *cb.Envelope { @@ -94,6 +77,8 @@ func OrdererExec(f func(s *BenchmarkServer)) { // RandomID generates a random string of num chars func RandomID(num int) string { + seedOnce.Do(func() { rand.Seed(time.Now().UnixNano()) }) + b := make([]rune, num) for i := range b { b[i] = letterRunes[rand.Intn(len(letterRunes))] @@ -102,7 +87,7 @@ func RandomID(num int) string { } // CreateChannel creates a channel with randomly generated ID of length 10 -func CreateChannel(server *BenchmarkServer) string { +func CreateChannel(server *BenchmarkServer, channelProfile *genesisconfig.Profile) string { client := server.CreateBroadcastClient() defer client.Close() diff --git a/orderer/common/server/benchmark_test.go b/orderer/common/server/benchmark_test.go index 132c1558cf2..c5978bd15f3 100644 --- a/orderer/common/server/benchmark_test.go +++ b/orderer/common/server/benchmark_test.go @@ -110,6 +110,7 @@ const ( // be less than 13 KB. AbsoluteMaxBytes = 15 // KB PreferredMaxBytes = 10 // KB + ChannelProfile = localconfig.SampleSingleMSPChannelV11Profile ) var envvars = map[string]string{ @@ -356,6 +357,15 @@ func benchmarkOrderer( numOfOrderer int, multiplex bool, ) { + // Initialization shared by all orderers + conf := config.Load() + initializeLoggingLevel(conf) + initializeLocalMsp(conf) + perf.InitializeServerPool(numOfOrderer) + + // Load sample channel profile + channelProfile := localconfig.Load(ChannelProfile) + // Calculate intermediate variables used internally. See the comment at the beginning // of this file for the purpose of these vars. txPerClient := totalTx / (broadcastClientPerChannel * numOfChannels * numOfOrderer) @@ -376,12 +386,6 @@ func benchmarkOrderer( var txCount uint64 // Atomic counter to keep track of actual tx sent - // Initialization shared by all orderers - conf := config.Load() - initializeLoggingLevel(conf) - initializeLocalMsp(conf) - perf.InitializeServerPool(numOfOrderer) - // Generate a random system channel id for each test run, // so it does not recover ledgers from previous run. systemchannel := "system-channel-" + perf.RandomID(5) @@ -419,7 +423,7 @@ func benchmarkOrderer( channelIDs := make([]string, numOfChannels) txs := make(map[string]*cb.Envelope) for i := 0; i < numOfChannels; i++ { - id := perf.CreateChannel(benchmarkServers[0]) // We only need to create channel on one orderer + id := perf.CreateChannel(benchmarkServers[0], channelProfile) // We only need to create channel on one orderer channelIDs[i] = id txs[id] = perf.MakeNormalTx(id, msgSize) } diff --git a/sampleconfig/configtx.yaml b/sampleconfig/configtx.yaml index 779e7308b13..b13189b88fe 100644 --- a/sampleconfig/configtx.yaml +++ b/sampleconfig/configtx.yaml @@ -59,7 +59,7 @@ Profiles: - <<: *SampleOrg AdminPrincipal: Role.MEMBER - # SampleDevModeSoloV1.1 mimics the SampleDevModeKafka definition + # SampleDevModeSoloV1.1 mimics the SampleDevModeSolo definition # but additionally defines the v1.1 only capabilities which do # not allow a mixed v1.0.x v1.1.x network SampleDevModeSoloV1_1: