From 5edea665783efe84bee080622948fc959c71037e Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 6 Oct 2022 13:14:35 -0400 Subject: [PATCH] zrokloop run -> zrok test loop (#40) --- cmd/{zrokloop => zrok}/loop.go | 32 +++++++++++++++---------------- cmd/zrokloop/main.go | 35 ---------------------------------- 2 files changed, 16 insertions(+), 51 deletions(-) rename cmd/{zrokloop => zrok}/loop.go (91%) delete mode 100644 cmd/zrokloop/main.go diff --git a/cmd/zrokloop/loop.go b/cmd/zrok/loop.go similarity index 91% rename from cmd/zrokloop/loop.go rename to cmd/zrok/loop.go index c0e6ea5d..b3666c4c 100644 --- a/cmd/zrokloop/loop.go +++ b/cmd/zrok/loop.go @@ -24,10 +24,10 @@ import ( ) func init() { - rootCmd.AddCommand(newRun().cmd) + testCmd.AddCommand(newLoopCmd().cmd) } -type run struct { +type loopCmd struct { cmd *cobra.Command loopers int iterations int @@ -38,13 +38,13 @@ type run struct { maxPayload int } -func newRun() *run { +func newLoopCmd() *loopCmd { cmd := &cobra.Command{ - Use: "run", + Use: "loop", Short: "Start a loop agent", Args: cobra.ExactArgs(0), } - r := &run{cmd: cmd} + r := &loopCmd{cmd: cmd} cmd.Run = r.run cmd.Flags().IntVarP(&r.loopers, "loopers", "l", 1, "Number of current loopers to start") cmd.Flags().IntVarP(&r.iterations, "iterations", "i", 1, "Number of iterations per looper") @@ -56,7 +56,7 @@ func newRun() *run { return r } -func (r *run) run(_ *cobra.Command, _ []string) { +func (r *loopCmd) run(_ *cobra.Command, _ []string) { var loopers []*looper for i := 0; i < r.loopers; i++ { l := newLooper(i, r) @@ -81,7 +81,7 @@ func (r *run) run(_ *cobra.Command, _ []string) { type looper struct { id int - r *run + cmd *loopCmd env *zrokdir.Environment done chan struct{} listener edge.Listener @@ -96,10 +96,10 @@ type looper struct { stopTime time.Time } -func newLooper(id int, r *run) *looper { +func newLooper(id int, cmd *loopCmd) *looper { return &looper{ id: id, - r: r, + cmd: cmd, done: make(chan struct{}), } } @@ -174,26 +174,26 @@ func (l *looper) startup() { } func (l *looper) dwell() { - time.Sleep(time.Duration(l.r.dwellSeconds) * time.Second) + time.Sleep(time.Duration(l.cmd.dwellSeconds) * time.Second) } func (l *looper) iterate() { l.startTime = time.Now() defer func() { l.stopTime = time.Now() }() - for i := 0; i < l.r.iterations; i++ { - if i > 0 && i%l.r.statusEvery == 0 { + for i := 0; i < l.cmd.iterations; i++ { + if i > 0 && i%l.cmd.statusEvery == 0 { logrus.Infof("looper #%d: iteration #%d", l.id, i) } - sz := l.r.maxPayload - if l.r.maxPayload-l.r.minPayload > 0 { - sz = rand.Intn(l.r.maxPayload-l.r.minPayload) + l.r.minPayload + sz := l.cmd.maxPayload + if l.cmd.maxPayload-l.cmd.minPayload > 0 { + sz = rand.Intn(l.cmd.maxPayload-l.cmd.minPayload) + l.cmd.minPayload } outpayload := make([]byte, sz) outbase64 := base64.StdEncoding.EncodeToString(outpayload) rand.Read(outpayload) if req, err := http.NewRequest("POST", l.proxyEndpoint, bytes.NewBufferString(outbase64)); err == nil { - client := &http.Client{Timeout: time.Second * time.Duration(l.r.timeoutSeconds)} + client := &http.Client{Timeout: time.Second * time.Duration(l.cmd.timeoutSeconds)} if resp, err := client.Do(req); err == nil { inpayload := new(bytes.Buffer) io.Copy(inpayload, resp.Body) diff --git a/cmd/zrokloop/main.go b/cmd/zrokloop/main.go deleted file mode 100644 index d70d1c64..00000000 --- a/cmd/zrokloop/main.go +++ /dev/null @@ -1,35 +0,0 @@ -package main - -import ( - "github.com/michaelquigley/pfxlog" - "github.com/openziti-test-kitchen/zrok/zrokdir" - "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "os" - "path/filepath" - "strings" -) - -func init() { - pfxlog.GlobalInit(logrus.InfoLevel, pfxlog.DefaultOptions().SetTrimPrefix("github.com/openziti-test-kitchen/")) - rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging") - zrokdir.AddZrokApiEndpointFlag(&apiEndpoint, rootCmd.PersistentFlags()) -} - -var rootCmd = &cobra.Command{ - Use: strings.TrimSuffix(filepath.Base(os.Args[0]), filepath.Ext(os.Args[0])), - Short: "zrok loopback harness", - PersistentPreRun: func(_ *cobra.Command, _ []string) { - if verbose { - logrus.SetLevel(logrus.DebugLevel) - } - }, -} -var verbose bool -var apiEndpoint string - -func main() { - if err := rootCmd.Execute(); err != nil { - panic(err) - } -}