Skip to content

Commit

Permalink
added throughput flag: todo add auto frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
ugol committed Jul 25, 2023
1 parent 1743339 commit 6c17661
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/cmd/templateRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ugol/jr/pkg/constants"
"github.com/ugol/jr/pkg/emitter"
"github.com/ugol/jr/pkg/functions"
"log"
"time"
)

Expand All @@ -53,6 +54,7 @@ jr template run --template "{{name}}"
num, _ := cmd.Flags().GetInt("num")
frequency, _ := cmd.Flags().GetDuration("frequency")
duration, _ := cmd.Flags().GetDuration("duration")
throughputString, _ := cmd.Flags().GetString("throughput")
seed, _ := cmd.Flags().GetInt64("seed")
topic, _ := cmd.Flags().GetString("topic")
preload, _ := cmd.Flags().GetInt("preload")
Expand All @@ -72,6 +74,15 @@ jr template run --template "{{name}}"
eTemplate = ""
}

throughput, err := parseThroughput(throughputString)
if err != nil {
log.Panicf("Throughput format error:%v", err)
}

if throughput > 0 {
// @TODO
}

cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Changed {
switch f.Name {
Expand Down Expand Up @@ -129,6 +140,7 @@ func init() {
templateRunCmd.Flags().IntP("num", "n", constants.NUM, "Number of elements to create for each pass")
templateRunCmd.Flags().DurationP("frequency", "f", constants.FREQUENCY, "how much time to wait for next generation pass")
templateRunCmd.Flags().DurationP("duration", "d", constants.YEAR, "If frequency is enabled, with Duration you can set a finite amount of time")
templateRunCmd.Flags().String("throughput", "", "You can set throughput, JR will calculate frequency automatically.")

templateRunCmd.Flags().Int64("seed", time.Now().UTC().UnixNano(), "Seed to init pseudorandom generator")

Expand Down
72 changes: 72 additions & 0 deletions pkg/cmd/throughput.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"fmt"
"regexp"
"strconv"
)

type Throughput float64

const (
bitMultiplier = 8
)

func parseThroughput(input string) (Throughput, error) {

if input == "" {
return -1, nil
}
re := regexp.MustCompile(`^((?:0|[1-9]\d*)(?:\.?\d*))([KkMmGgTt][Bb])/([smhd])$`)
match := re.FindStringSubmatch(input)

if len(match) != 4 {
return 0, fmt.Errorf("invalid input format: %s", input)
}

valueStr := match[1]
unitStr := match[2]
timeStr := match[3]
value, err := strconv.ParseFloat(valueStr, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse numeric value: %w", err)
}

switch timeStr {
case "s":
// nothing to do
case "m":
value = value / 60
case "h":
value = value / 3600
case "d":
value = value / 86400
default:
return 0, fmt.Errorf("unsupported time unit: %s", unitStr)
}

switch unitStr {
case "b":
return Throughput(value * bitMultiplier), nil
case "B":
return Throughput(value), nil
case "kb", "Kb":
return Throughput(value * 1024 * bitMultiplier), nil
case "mb", "Mb":
return Throughput(value * 1024 * 1024 * bitMultiplier), nil
case "gb", "Gb":
return Throughput(value * 1024 * 1024 * 1024 * bitMultiplier), nil
case "tb", "Tb":
return Throughput(value * 1024 * 1024 * 1024 * 1024 * bitMultiplier), nil
case "kB", "KB":
return Throughput(value * 1024), nil
case "mB", "MB":
return Throughput(value * 1024 * 1024), nil
case "gB", "GB":
return Throughput(value * 1024 * 1024 * 1024), nil
case "tB", "TB":
return Throughput(value * 1024 * 1024 * 1024 * 1024), nil
default:
return 0, fmt.Errorf("unsupported unit: %s", unitStr)
}
}

0 comments on commit 6c17661

Please sign in to comment.