Skip to content

Commit

Permalink
move jitter concern from task to the perf package
Browse files Browse the repository at this point in the history
  • Loading branch information
Omarabdul3ziz committed Dec 4, 2023
1 parent c57df2b commit b11ea82
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
10 changes: 3 additions & 7 deletions pkg/perf/cpubench/cpubench_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"os/exec"
"time"

"github.com/threefoldtech/zos/pkg/perf"
"github.com/threefoldtech/zos/pkg/stubs"
Expand Down Expand Up @@ -65,15 +63,13 @@ func (c *CPUBenchmarkTask) Description() string {
return c.description
}

// Jitter returns the duration the task will sleep for before running.
func (c *CPUBenchmarkTask) Jitter() time.Duration {
jitter := time.Duration(rand.Int31n(int32(c.jitter))) * time.Second
return jitter
// Jitter returns the max number of seconds the job can sleep before actual execution.
func (c *CPUBenchmarkTask) Jitter() uint32 {
return c.jitter
}

// Run executes the CPU benchmark.
func (c *CPUBenchmarkTask) Run(ctx context.Context) (interface{}, error) {
time.Sleep(c.Jitter())
cpubenchOut, err := exec.CommandContext(ctx, "cpubench", "-j").CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to execute cpubench command: %w", err)
Expand Down
10 changes: 3 additions & 7 deletions pkg/perf/iperf/iperf_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net"
"os"
"os/exec"
"path/filepath"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -69,15 +67,13 @@ func (t *IperfTest) Description() string {
return t.description
}

// Jitter returns the duration the task will sleep for before running.
func (t *IperfTest) Jitter() time.Duration {
jitter := time.Duration(rand.Int31n(int32(t.jitter))) * time.Second
return jitter
// Jitter returns the max number of seconds the job can sleep before actual execution.
func (t *IperfTest) Jitter() uint32 {
return t.jitter
}

// Run runs the tcp test and returns the result
func (t *IperfTest) Run(ctx context.Context) (interface{}, error) {
time.Sleep(t.Jitter())
env := environment.MustGet()
g := graphql.NewGraphQl(env.GraphQL)

Expand Down
4 changes: 4 additions & 0 deletions pkg/perf/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package perf

import (
"context"
"math/rand"
"time"

"github.com/go-co-op/gocron"
Expand Down Expand Up @@ -49,6 +50,9 @@ func (pm *PerformanceMonitor) AddTask(task Task) {

// runTask runs the task and store its result
func (pm *PerformanceMonitor) runTask(ctx context.Context, task Task) error {
sleepInterval := time.Duration(rand.Int31n(int32(task.Jitter()))) * time.Second
time.Sleep(sleepInterval)

res, err := task.Run(ctx)
if err != nil {
return errors.Wrapf(err, "failed to run task: %s", task.ID())
Expand Down
26 changes: 10 additions & 16 deletions pkg/perf/publicip/publicip_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"os/exec"
Expand Down Expand Up @@ -46,11 +45,10 @@ const testMacvlan = "pub"
const testNamespace = "pubtestns"

type publicIPValidationTask struct {
taskID string
schedule string
description string
jitter uint32
farmIPsReport map[string]IPReport
taskID string
schedule string
description string
jitter uint32
}

type IPReport struct {
Expand All @@ -62,11 +60,10 @@ var _ perf.Task = (*publicIPValidationTask)(nil)

func NewTask() perf.Task {
return &publicIPValidationTask{
taskID: taskID,
schedule: taskSchedule,
description: taskDescription,
jitter: 10 * 60,
farmIPsReport: make(map[string]IPReport),
taskID: taskID,
schedule: taskSchedule,
description: taskDescription,
jitter: 10 * 60,
}
}

Expand All @@ -82,14 +79,11 @@ func (p *publicIPValidationTask) Description() string {
return p.description
}

func (p *publicIPValidationTask) Jitter() time.Duration {
jitter := time.Duration(rand.Int31n(int32(p.jitter))) * time.Second
return jitter
func (p *publicIPValidationTask) Jitter() uint32 {
return p.jitter
}

func (p *publicIPValidationTask) Run(ctx context.Context) (interface{}, error) {
time.Sleep(p.Jitter())

netNS, err := namespace.GetByName(testNamespace)
if err != nil {
return nil, fmt.Errorf("failed to get namespace %s: %w", testNamespace, err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/perf/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package perf

import (
"context"
"time"
)

type Task interface {
ID() string
Cron() string
Description() string
Jitter() time.Duration
Jitter() uint32
Run(ctx context.Context) (interface{}, error)
}

0 comments on commit b11ea82

Please sign in to comment.