Skip to content

Commit 9dc28a2

Browse files
committed
refactor bytes per second to bytes per tick and tick interval
1 parent 731b4db commit 9dc28a2

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

cli/scaletest.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,8 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd {
897897
func (r *RootCmd) scaletestTrafficGen() *clibase.Cmd {
898898
var (
899899
duration time.Duration
900-
bps int64
900+
tickInterval time.Duration
901+
bytesPerTick int64
901902
client = &codersdk.Client{}
902903
tracingFlags = &scaletestTracingFlags{}
903904
strategy = &scaletestStrategyFlags{}
@@ -975,10 +976,10 @@ func (r *RootCmd) scaletestTrafficGen() *clibase.Cmd {
975976

976977
// Setup our workspace agent connection.
977978
config := trafficgen.Config{
978-
AgentID: agentID,
979-
BytesPerSecond: bps,
980-
Duration: duration,
981-
TicksPerSecond: 10,
979+
AgentID: agentID,
980+
BytesPerTick: bytesPerTick,
981+
Duration: duration,
982+
TickInterval: tickInterval,
982983
}
983984

984985
if err := config.Validate(); err != nil {
@@ -1029,11 +1030,18 @@ func (r *RootCmd) scaletestTrafficGen() *clibase.Cmd {
10291030
Value: clibase.DurationOf(&duration),
10301031
},
10311032
{
1032-
Flag: "bps",
1033-
Env: "CODER_SCALETEST_TRAFFICGEN_BPS",
1033+
Flag: "bytes-per-tick",
1034+
Env: "CODER_SCALETEST_TRAFFICGEN_BYTES_PER_TICK",
10341035
Default: "1024",
1035-
Description: "How much traffic to generate in bytes per second.",
1036-
Value: clibase.Int64Of(&bps),
1036+
Description: "How much traffic to generate per tick.",
1037+
Value: clibase.Int64Of(&bytesPerTick),
1038+
},
1039+
{
1040+
Flag: "tick-interval",
1041+
Env: "CODER_SCALETEST_TRAFFICGEN_TICK_INTERVAL",
1042+
Default: "100ms",
1043+
Description: "How often to send traffic.",
1044+
Value: clibase.DurationOf(&tickInterval),
10371045
},
10381046
}
10391047

cli/scaletest_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ func TestScaleTestTrafficGen(t *testing.T) {
261261

262262
inv, root := clitest.New(t, "scaletest", "trafficgen", ws.Name,
263263
"--duration", "1s",
264-
"--bps", "100",
264+
"--bytes-per-tick", "1024",
265+
"--tick-interval", "100ms",
265266
)
266267
clitest.SetupConfig(t, client, root)
267268
var stdout, stderr bytes.Buffer

scaletest/trafficgen/config.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,33 @@ import (
1010
type Config struct {
1111
// AgentID is the workspace agent ID to which to connect.
1212
AgentID uuid.UUID `json:"agent_id"`
13-
// BytesPerSecond is the number of bytes to send to the agent.
1413

15-
BytesPerSecond int64 `json:"bytes_per_second"`
14+
// BytesPerTick is the number of bytes to send to the agent per tick.
15+
BytesPerTick int64 `json:"bytes_per_tick"`
1616

1717
// Duration is the total duration for which to send traffic to the agent.
1818
Duration time.Duration `json:"duration"`
1919

20-
// TicksPerSecond specifies how many times per second we send traffic.
21-
TicksPerSecond int64 `json:"ticks_per_second"`
20+
// TicksInterval specifies how many times per second we send traffic.
21+
TickInterval time.Duration `json:"tick_interval"`
2222
}
2323

2424
func (c Config) Validate() error {
2525
if c.AgentID == uuid.Nil {
2626
return xerrors.Errorf("validate agent_id: must not be nil")
2727
}
2828

29-
if c.BytesPerSecond <= 0 {
30-
return xerrors.Errorf("validate bytes_per_second: must be greater than zero")
29+
if c.BytesPerTick <= 0 {
30+
return xerrors.Errorf("validate bytes_per_tick: must be greater than zero")
3131
}
3232

3333
if c.Duration <= 0 {
3434
return xerrors.Errorf("validate duration: must be greater than zero")
3535
}
3636

37-
if c.TicksPerSecond <= 0 {
38-
return xerrors.Errorf("validate ticks_per_second: must be greater than zero")
37+
if c.TickInterval <= 0 {
38+
return xerrors.Errorf("validate tick_interval: must be greater than zero")
3939
}
40+
4041
return nil
4142
}

scaletest/trafficgen/run.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func (r *Runner) Run(ctx context.Context, _ string, logs io.Writer) error {
5252
reconnect = uuid.New()
5353
height uint16 = 25
5454
width uint16 = 80
55-
tickInterval = time.Second / time.Duration(r.cfg.TicksPerSecond)
56-
bytesPerTick = r.cfg.BytesPerSecond / r.cfg.TicksPerSecond
55+
tickInterval = r.cfg.TickInterval
56+
bytesPerTick = r.cfg.BytesPerTick
5757
)
5858

5959
logger.Info(ctx, "config",
@@ -149,7 +149,10 @@ func (*Runner) Cleanup(context.Context, string) error {
149149
// drain drains from src until it returns io.EOF or ctx times out.
150150
func drain(src io.Reader) error {
151151
if _, err := io.Copy(io.Discard, src); err != nil {
152-
if xerrors.Is(err, context.DeadlineExceeded) || xerrors.Is(err, websocket.CloseError{}) {
152+
if xerrors.Is(err, context.DeadlineExceeded) {
153+
return nil
154+
}
155+
if xerrors.As(err, &websocket.CloseError{}) {
153156
return nil
154157
}
155158
return err
@@ -166,7 +169,10 @@ func writeRandomData(dst io.Writer, size int64, tick <-chan time.Time) error {
166169
payload := "#" + mustRandStr(size-1)
167170
ptyReq.Data = payload
168171
if err := enc.Encode(ptyReq); err != nil {
169-
if xerrors.Is(err, context.DeadlineExceeded) || xerrors.Is(err, websocket.CloseError{}) {
172+
if xerrors.Is(err, context.DeadlineExceeded) {
173+
return nil
174+
}
175+
if xerrors.As(err, &websocket.CloseError{}) {
170176
return nil
171177
}
172178
return err
@@ -213,8 +219,11 @@ func (w *countReadWriter) BytesWritten() int64 {
213219
return w.bytesWritten.Load()
214220
}
215221

216-
func mustRandStr(len int64) string {
217-
randStr, err := cryptorand.String(int(len))
222+
func mustRandStr(l int64) string {
223+
if l < 1 {
224+
l = 1
225+
}
226+
randStr, err := cryptorand.String(int(l))
218227
if err != nil {
219228
panic(err)
220229
}

0 commit comments

Comments
 (0)