Skip to content

Commit a8227fd

Browse files
committed
[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 <benweintraub34@gmail.com>
1 parent f6bb64b commit a8227fd

File tree

3 files changed

+17
-28
lines changed

3 files changed

+17
-28
lines changed

orderer/common/performance/utils.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
"github.com/hyperledger/fabric/common/localmsp"
1616
"github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
1717
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
18-
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
19-
"github.com/hyperledger/fabric/orderer/common/localconfig"
18+
2019
cb "github.com/hyperledger/fabric/protos/common"
2120
ab "github.com/hyperledger/fabric/protos/orderer"
2221
protosutils "github.com/hyperledger/fabric/protos/utils"
@@ -27,25 +26,9 @@ const (
2726
Kilo = 1024 // TODO Consider adding a unit pkg
2827
)
2928

30-
var conf *config.TopLevel
31-
3229
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
3330

34-
var channelProfile *genesisconfig.Profile
35-
36-
func init() {
37-
rand.Seed(time.Now().UnixNano())
38-
39-
conf = config.Load()
40-
41-
// Load local MSP
42-
err := mspmgmt.LoadLocalMsp(conf.General.LocalMSPDir, conf.General.BCCSP, conf.General.LocalMSPID)
43-
if err != nil {
44-
panic(fmt.Errorf("Failed to initialize local MSP: %s", err))
45-
}
46-
47-
channelProfile = genesisconfig.Load(genesisconfig.SampleSingleMSPChannelV11Profile)
48-
}
31+
var seedOnce sync.Once
4932

5033
// MakeNormalTx creates a properly signed transaction that could be used against `broadcast` API
5134
func MakeNormalTx(channelID string, size int) *cb.Envelope {
@@ -94,6 +77,8 @@ func OrdererExec(f func(s *BenchmarkServer)) {
9477

9578
// RandomID generates a random string of num chars
9679
func RandomID(num int) string {
80+
seedOnce.Do(func() { rand.Seed(time.Now().UnixNano()) })
81+
9782
b := make([]rune, num)
9883
for i := range b {
9984
b[i] = letterRunes[rand.Intn(len(letterRunes))]
@@ -102,7 +87,7 @@ func RandomID(num int) string {
10287
}
10388

10489
// CreateChannel creates a channel with randomly generated ID of length 10
105-
func CreateChannel(server *BenchmarkServer) string {
90+
func CreateChannel(server *BenchmarkServer, channelProfile *genesisconfig.Profile) string {
10691
client := server.CreateBroadcastClient()
10792
defer client.Close()
10893

orderer/common/server/benchmark_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const (
110110
// be less than 13 KB.
111111
AbsoluteMaxBytes = 15 // KB
112112
PreferredMaxBytes = 10 // KB
113+
ChannelProfile = localconfig.SampleSingleMSPChannelV11Profile
113114
)
114115

115116
var envvars = map[string]string{
@@ -356,6 +357,15 @@ func benchmarkOrderer(
356357
numOfOrderer int,
357358
multiplex bool,
358359
) {
360+
// Initialization shared by all orderers
361+
conf := config.Load()
362+
initializeLoggingLevel(conf)
363+
initializeLocalMsp(conf)
364+
perf.InitializeServerPool(numOfOrderer)
365+
366+
// Load sample channel profile
367+
channelProfile := localconfig.Load(ChannelProfile)
368+
359369
// Calculate intermediate variables used internally. See the comment at the beginning
360370
// of this file for the purpose of these vars.
361371
txPerClient := totalTx / (broadcastClientPerChannel * numOfChannels * numOfOrderer)
@@ -376,12 +386,6 @@ func benchmarkOrderer(
376386

377387
var txCount uint64 // Atomic counter to keep track of actual tx sent
378388

379-
// Initialization shared by all orderers
380-
conf := config.Load()
381-
initializeLoggingLevel(conf)
382-
initializeLocalMsp(conf)
383-
perf.InitializeServerPool(numOfOrderer)
384-
385389
// Generate a random system channel id for each test run,
386390
// so it does not recover ledgers from previous run.
387391
systemchannel := "system-channel-" + perf.RandomID(5)
@@ -419,7 +423,7 @@ func benchmarkOrderer(
419423
channelIDs := make([]string, numOfChannels)
420424
txs := make(map[string]*cb.Envelope)
421425
for i := 0; i < numOfChannels; i++ {
422-
id := perf.CreateChannel(benchmarkServers[0]) // We only need to create channel on one orderer
426+
id := perf.CreateChannel(benchmarkServers[0], channelProfile) // We only need to create channel on one orderer
423427
channelIDs[i] = id
424428
txs[id] = perf.MakeNormalTx(id, msgSize)
425429
}

sampleconfig/configtx.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Profiles:
5959
- <<: *SampleOrg
6060
AdminPrincipal: Role.MEMBER
6161

62-
# SampleDevModeSoloV1.1 mimics the SampleDevModeKafka definition
62+
# SampleDevModeSoloV1.1 mimics the SampleDevModeSolo definition
6363
# but additionally defines the v1.1 only capabilities which do
6464
# not allow a mixed v1.0.x v1.1.x network
6565
SampleDevModeSoloV1_1:

0 commit comments

Comments
 (0)