-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CPU benchmark task #2066
Add CPU benchmark task #2066
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package perf | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/threefoldtech/zos/pkg/stubs" | ||
) | ||
|
||
const cpuBenchmarkTaskID = "CPUBenchmark" | ||
const cpuBenchmarkCronSchedule = "0 0 */6 * * *" | ||
|
||
// CPUBenchmarkTask defines CPU benchmark task data. | ||
type CPUBenchmarkTask struct { | ||
// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the Single/Multi process or a thread? can we add a few more info and even change the name to a include the full name (or that's because of the json marshalling?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change the name of the fields but the data returned from |
||
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: cpuBenchmarkTaskID, | ||
schedule: cpuBenchmarkCronSchedule, | ||
} | ||
} | ||
|
||
// ID returns task ID. | ||
func (c *CPUBenchmarkTask) ID() string { | ||
return c.taskID | ||
} | ||
|
||
// Cron returns task cron schedule. | ||
func (c *CPUBenchmarkTask) Cron() string { | ||
return c.schedule | ||
} | ||
|
||
// Run executes the CPU benchmark. | ||
func (c *CPUBenchmarkTask) Run(ctx context.Context) (interface{}, error) { | ||
cpubenchOut, err := exec.CommandContext(ctx, "cpubench", "-j").CombinedOutput() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to execute cpubench command: %w", err) | ||
} | ||
cpuBenchmarkResult := CPUBenchmarkResult{} | ||
err = json.Unmarshal(cpubenchOut, &cpuBenchmarkResult) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse cpubench output: %w", err) | ||
} | ||
client := GetZbusClient(ctx) | ||
statistics := stubs.NewStatisticsStub(client) | ||
|
||
workloads, err := statistics.Workloads(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get workloads number: %w", err) | ||
} | ||
|
||
cpuBenchmarkResult.Workloads = workloads | ||
return cpuBenchmarkResult, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add more explanation on the fields, e.g taskID is that a sequential string?a uuid?or else?
schedule: i'm assuming that's a cron string? let's clear the ambiguity please