Skip to content

Commit

Permalink
Add more docs and remove anonymous struct
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanElawady committed Oct 3, 2023
1 parent 8249738 commit 4ed761d
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions pkg/perf/cpubench_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,32 @@ import (
"github.com/threefoldtech/zos/pkg/stubs"
)

const cpuBenchmarkTaskID = "CPUBenchmark"
const cpuBenchmarkCronScheule = "0 0 */6 * * *"

// CPUBenchmarkTask defines CPU benchmark task data.
type CPUBenchmarkTask struct {
taskID string
// taskID is a unique string ID for the task.
taskID string
// schedule is a 6 field cron schedule (unlike unix cron).
schedule string
}

// CPUBenchmarkResult holds CPU benchmark results with the workloads number during the benchmark.
type CPUBenchmarkResult struct {
Single float64 `json:"single"`
Multi float64 `json:"multi"`
Threads int `json:"threads"`
Workloads int `json:"workloads"`
SingleThreaded float64 `json:"single"`
MultiThreaded float64 `json:"multi"`
Threads int `json:"threads"`
Workloads int `json:"workloads"`
}

var _ Task = (*CPUBenchmarkTask)(nil)

// NewCPUBenchmarkTask returns a new CPU benchmark task.
func NewCPUBenchmarkTask() CPUBenchmarkTask {
return CPUBenchmarkTask{
taskID: "CPUBenchmark",
schedule: "0 0 */6 * * *",
taskID: cpuBenchmarkTaskID,
schedule: cpuBenchmarkCronScheule,
}
}

Expand All @@ -49,12 +54,8 @@ func (c *CPUBenchmarkTask) Run(ctx context.Context) (interface{}, error) {
if err != nil {
return nil, fmt.Errorf("failed to execute cpubench command: %w", err)
}
cpubenchData := struct {
Single float64 `json:"single"`
Multi float64 `json:"multi"`
Threads int `json:"threads"`
}{}
err = json.Unmarshal(cpubenchOut, &cpubenchData)
cpuBenchmarkResult := CPUBenchmarkResult{}
err = json.Unmarshal(cpubenchOut, &cpuBenchmarkResult)
if err != nil {
return nil, fmt.Errorf("failed to parse cpubench output: %w", err)
}
Expand All @@ -66,10 +67,6 @@ func (c *CPUBenchmarkTask) Run(ctx context.Context) (interface{}, error) {
return nil, fmt.Errorf("failed to get workloads number: %w", err)
}

return CPUBenchmarkResult{
Single: cpubenchData.Single,
Multi: cpubenchData.Multi,
Threads: cpubenchData.Threads,
Workloads: workloads,
}, nil
cpuBenchmarkResult.Workloads = workloads
return cpuBenchmarkResult, nil
}

0 comments on commit 4ed761d

Please sign in to comment.