Skip to content

Commit

Permalink
introduce kingpin as the cmd framework
Browse files Browse the repository at this point in the history
Currently, only the run and help subcommands are introduced.
Run subcommand has two flags: config and number.
Config flag is the config file's path, whose short flag name is -c.
Number flag is the number of tx for shot, whose short flag name is -n.
Help subcommand just show context-sensitive help, which is provided by the framework.

Signed-off-by: wuxu <wuxu1103@163.com>
  • Loading branch information
wuxuer committed Dec 11, 2020
1 parent c58f0d6 commit 645484a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/ms

#### Binary

Execute `./tape config.yaml 40000` to generate 40000 transactions to Fabric.
Execute `./tape -c config.yaml -n 40000` to generate 40000 transactions to Fabric.

#### Docker

```
docker run -v $PWD:/tmp guoger/tape tape $CONFIG_FILE 40000
docker run -v $PWD:/tmp guoger/tape tape -c $CONFIG_FILE -n 40000
```

*Set this to integer times of batchsize, so that last block is not cut due to timeout*. For example, if you have batch size of 500, set this to 500, 1000, 40000, 100000, etc.
Expand Down
42 changes: 30 additions & 12 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,39 @@ var _ = Describe("Mock test", func() {
})

Context("E2E with Error Cases", func() {
When("Config error", func() {
It("should hit usage", func() {
cmd := exec.Command(tapeBin)
When("Command error", func() {
It("should return unexpected command", func() {
cmd := exec.Command(tapeBin, "wrongCommand")
tapeSession, err := gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("error input parameters for tape: tape config.yaml 500"))
Eventually(tapeSession.Err).Should(Say("tape: error: unexpected wrongCommand, try --help"))
})
It("should hit if not number", func() {
cmd := exec.Command(tapeBin, "NoExitFile", "abc")

It("should return required flag config", func() {
cmd := exec.Command(tapeBin, "-n", "500")
tapeSession, err := gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("error input parameters for tape: tape config.yaml 500"))
Eventually(tapeSession.Err).Should(Say("tape: error: required flag --config not provided, try --help"))
})

It("should return required flag number", func() {
cmd := exec.Command(tapeBin, "-c", "TestFile")
tapeSession, err := gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("tape: error: required flag --number not provided, try --help"))
})

It("should return help info", func() {
cmd := exec.Command(tapeBin, "--help")
tapeSession, err := gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("--help Show context-sensitive help*"))
})
})

When("Config error", func() {
It("should return file not exist", func() {
cmd := exec.Command(tapeBin, "NoExitFile", "500")
cmd := exec.Command(tapeBin, "-c", "NoExitFile", "-n", "500")
tapeSession, err := gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("NoExitFile"))
Expand All @@ -85,7 +103,7 @@ var _ = Describe("Mock test", func() {
Addr: "N/A",
}
generateConfigFile(config.Name(), configValue)
cmd := exec.Command(tapeBin, config.Name(), "500")
cmd := exec.Command(tapeBin, "-c", config.Name(), "-n", "500")
tapeSession, err := gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("error loading priv key"))
Expand All @@ -103,7 +121,7 @@ var _ = Describe("Mock test", func() {
}
generateConfigFile(config.Name(), configValue)

cmd := exec.Command(tapeBin, config.Name(), "500")
cmd := exec.Command(tapeBin, "-c", config.Name(), "-n", "500")
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Err).Should(Say("error connecting to invalid_addr"))
Expand Down Expand Up @@ -132,7 +150,7 @@ var _ = Describe("Mock test", func() {
}
generateConfigFile(config.Name(), configValue)

cmd := exec.Command(tapeBin, config.Name(), "500")
cmd := exec.Command(tapeBin, "-c", config.Name(), "-n", "500")
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Out).Should(Say("Time.*Block.*Tx.*10.*"))
Expand Down Expand Up @@ -176,7 +194,7 @@ var _ = Describe("Mock test", func() {

generateConfigFile(config.Name(), configValue)

cmd := exec.Command(tapeBin, config.Name(), "500")
cmd := exec.Command(tapeBin, "-c", config.Name(), "-n", "500")
tapeSession, err = gexec.Start(cmd, nil, nil)
Expect(err).NotTo(HaveOccurred())
Eventually(tapeSession.Out).Should(Say("Time.*Block.*Tx.*10.*"))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed // indirect
google.golang.org/genproto v0.0.0-20191028173616-919d9bdd9fe6 // indirect
google.golang.org/grpc v1.24.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/jcmturner/goidentity.v3 v3.0.0 // indirect
gopkg.in/yaml.v2 v2.3.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ github.com/Shopify/sarama v1.23.1/go.mod h1:XLH1GYJnLVE0XCr6KdJGVJRTwY30moWNJ4sE
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -293,6 +295,7 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
27 changes: 15 additions & 12 deletions pkg/infra/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ package infra
import (
"fmt"
"os"
"strconv"
"time"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
)

const loglevel = "TAPE_LOGLEVEL"

var (
app = kingpin.New("tape", "A performance test tool for Hyperledger Fabric")

run = app.Command("run", "Start the tape program").Default()
con = run.Flag("config", "Path to config file").Required().Short('c').String()
num = run.Flag("number", "Number of tx for shot").Required().Short('n').Int()
)

func Main() {
logger := log.New()
logger.SetLevel(log.WarnLevel)
Expand All @@ -29,14 +37,9 @@ func Main() {
}

func process(logger *log.Logger) error {
if len(os.Args) != 3 {
return errors.Errorf("error input parameters for tape: tape config.yaml 500")
}
N, err := strconv.Atoi(os.Args[2])
if err != nil {
return errors.Errorf("error input parameters for tape: tape config.yaml 500")
}
config, err := LoadConfig(os.Args[1])
kingpin.MustParse(app.Parse(os.Args[1:]))

config, err := LoadConfig(*con)
if err != nil {
return err
}
Expand Down Expand Up @@ -80,9 +83,9 @@ func process(logger *log.Logger) error {
}

start := time.Now()
go observer.Start(N, errorCh, finishCh, start)
go observer.Start(*num, errorCh, finishCh, start)
go func() {
for i := 0; i < N; i++ {
for i := 0; i < *num; i++ {
prop, err := CreateProposal(
crypto,
config.Channel,
Expand All @@ -107,7 +110,7 @@ func process(logger *log.Logger) error {
close(done)

logger.Infof("Completed processing transactions.")
fmt.Printf("tx: %d, duration: %+v, tps: %f\n", N, duration, float64(N)/duration.Seconds())
fmt.Printf("tx: %d, duration: %+v, tps: %f\n", *num, duration, float64(*num)/duration.Seconds())
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ case $1 in
esac

cd "$DIR"
docker run -e TAPE_LOGLEVEL=debug --network host -v $PWD:/config tape tape $CONFIG_FILE 500
docker run -e TAPE_LOGLEVEL=debug --network host -v $PWD:/config tape tape -c $CONFIG_FILE -n 500

0 comments on commit 645484a

Please sign in to comment.