Skip to content

Commit

Permalink
[FAB-7544] Refactor orderer benchmark init
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
iowaguy committed Jan 5, 2018
1 parent f6bb64b commit a8227fd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
25 changes: 5 additions & 20 deletions orderer/common/performance/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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))]
Expand All @@ -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()

Expand Down
18 changes: 11 additions & 7 deletions orderer/common/server/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const (
// be less than 13 KB.
AbsoluteMaxBytes = 15 // KB
PreferredMaxBytes = 10 // KB
ChannelProfile = localconfig.SampleSingleMSPChannelV11Profile
)

var envvars = map[string]string{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion sampleconfig/configtx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a8227fd

Please sign in to comment.