Skip to content

Commit

Permalink
add jitter for the task
Browse files Browse the repository at this point in the history
jitter is the max number of seconds a task can wait for before actual
running.
  • Loading branch information
Omarabdul3ziz committed Dec 4, 2023
1 parent 7e2e347 commit c57df2b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
12 changes: 12 additions & 0 deletions pkg/perf/cpubench/cpubench_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"os/exec"
"time"

"github.com/threefoldtech/zos/pkg/perf"
"github.com/threefoldtech/zos/pkg/stubs"
Expand All @@ -24,6 +26,8 @@ type CPUBenchmarkTask struct {
schedule string
// description briefly describe what a task do.
description string
// jitter is max number of seconds the job can sleep before actual execution.
jitter uint32
}

// CPUBenchmarkResult holds CPU benchmark results with the workloads number during the benchmark.
Expand All @@ -42,6 +46,7 @@ func NewCPUBenchmarkTask() CPUBenchmarkTask {
taskID: cpuBenchmarkTaskID,
schedule: cpuBenchmarkCronSchedule,
description: cpuBenchmarkDescription,
jitter: 10 * 60,
}
}

Expand All @@ -60,8 +65,15 @@ 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
}

// 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
11 changes: 11 additions & 0 deletions pkg/perf/iperf/iperf_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net"
"os"
"os/exec"
"path/filepath"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand All @@ -22,6 +24,7 @@ type IperfTest struct {
taskID string
schedule string
description string
jitter uint32
}

// IperfResult for iperf test results
Expand All @@ -47,6 +50,7 @@ func NewTask() perf.Task {
taskID: "iperf",
schedule: "0 0 */6 * * *",
description: "Test public nodes network performance with both UDP and TCP over IPv4 and IPv6",
jitter: 10 * 60,
}
}

Expand All @@ -65,8 +69,15 @@ 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
}

// 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
23 changes: 17 additions & 6 deletions pkg/perf/publicip/publicip_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"os/exec"
Expand Down Expand Up @@ -45,9 +46,11 @@ const testMacvlan = "pub"
const testNamespace = "pubtestns"

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

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

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

Expand All @@ -77,7 +82,13 @@ 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) Run(ctx context.Context) (interface{}, error) {
time.Sleep(p.Jitter())

netNS, err := namespace.GetByName(testNamespace)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/perf/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package perf

import (
"context"
"time"
)

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

0 comments on commit c57df2b

Please sign in to comment.