mirror of
https://github.com/openziti/zrok.git
synced 2025-02-02 03:20:26 +01:00
zrokloop run -> zrok test loop (#40)
This commit is contained in:
parent
ca9ff75f6b
commit
5edea66578
@ -24,10 +24,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(newRun().cmd)
|
testCmd.AddCommand(newLoopCmd().cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
type run struct {
|
type loopCmd struct {
|
||||||
cmd *cobra.Command
|
cmd *cobra.Command
|
||||||
loopers int
|
loopers int
|
||||||
iterations int
|
iterations int
|
||||||
@ -38,13 +38,13 @@ type run struct {
|
|||||||
maxPayload int
|
maxPayload int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRun() *run {
|
func newLoopCmd() *loopCmd {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "run",
|
Use: "loop",
|
||||||
Short: "Start a loop agent",
|
Short: "Start a loop agent",
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
}
|
}
|
||||||
r := &run{cmd: cmd}
|
r := &loopCmd{cmd: cmd}
|
||||||
cmd.Run = r.run
|
cmd.Run = r.run
|
||||||
cmd.Flags().IntVarP(&r.loopers, "loopers", "l", 1, "Number of current loopers to start")
|
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")
|
cmd.Flags().IntVarP(&r.iterations, "iterations", "i", 1, "Number of iterations per looper")
|
||||||
@ -56,7 +56,7 @@ func newRun() *run {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *run) run(_ *cobra.Command, _ []string) {
|
func (r *loopCmd) run(_ *cobra.Command, _ []string) {
|
||||||
var loopers []*looper
|
var loopers []*looper
|
||||||
for i := 0; i < r.loopers; i++ {
|
for i := 0; i < r.loopers; i++ {
|
||||||
l := newLooper(i, r)
|
l := newLooper(i, r)
|
||||||
@ -81,7 +81,7 @@ func (r *run) run(_ *cobra.Command, _ []string) {
|
|||||||
|
|
||||||
type looper struct {
|
type looper struct {
|
||||||
id int
|
id int
|
||||||
r *run
|
cmd *loopCmd
|
||||||
env *zrokdir.Environment
|
env *zrokdir.Environment
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
listener edge.Listener
|
listener edge.Listener
|
||||||
@ -96,10 +96,10 @@ type looper struct {
|
|||||||
stopTime time.Time
|
stopTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLooper(id int, r *run) *looper {
|
func newLooper(id int, cmd *loopCmd) *looper {
|
||||||
return &looper{
|
return &looper{
|
||||||
id: id,
|
id: id,
|
||||||
r: r,
|
cmd: cmd,
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -174,26 +174,26 @@ func (l *looper) startup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *looper) dwell() {
|
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() {
|
func (l *looper) iterate() {
|
||||||
l.startTime = time.Now()
|
l.startTime = time.Now()
|
||||||
defer func() { l.stopTime = time.Now() }()
|
defer func() { l.stopTime = time.Now() }()
|
||||||
|
|
||||||
for i := 0; i < l.r.iterations; i++ {
|
for i := 0; i < l.cmd.iterations; i++ {
|
||||||
if i > 0 && i%l.r.statusEvery == 0 {
|
if i > 0 && i%l.cmd.statusEvery == 0 {
|
||||||
logrus.Infof("looper #%d: iteration #%d", l.id, i)
|
logrus.Infof("looper #%d: iteration #%d", l.id, i)
|
||||||
}
|
}
|
||||||
sz := l.r.maxPayload
|
sz := l.cmd.maxPayload
|
||||||
if l.r.maxPayload-l.r.minPayload > 0 {
|
if l.cmd.maxPayload-l.cmd.minPayload > 0 {
|
||||||
sz = rand.Intn(l.r.maxPayload-l.r.minPayload) + l.r.minPayload
|
sz = rand.Intn(l.cmd.maxPayload-l.cmd.minPayload) + l.cmd.minPayload
|
||||||
}
|
}
|
||||||
outpayload := make([]byte, sz)
|
outpayload := make([]byte, sz)
|
||||||
outbase64 := base64.StdEncoding.EncodeToString(outpayload)
|
outbase64 := base64.StdEncoding.EncodeToString(outpayload)
|
||||||
rand.Read(outpayload)
|
rand.Read(outpayload)
|
||||||
if req, err := http.NewRequest("POST", l.proxyEndpoint, bytes.NewBufferString(outbase64)); err == nil {
|
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 {
|
if resp, err := client.Do(req); err == nil {
|
||||||
inpayload := new(bytes.Buffer)
|
inpayload := new(bytes.Buffer)
|
||||||
io.Copy(inpayload, resp.Body)
|
io.Copy(inpayload, resp.Body)
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user