Skip to content
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

contrib: introduce BenchmarkReport type #112

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 73 additions & 57 deletions contrib/cmd/runkperf/commands/bench/node100_job1_pod3k.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/Azure/kperf/api/types"
kperfcmdutils "github.com/Azure/kperf/cmd/kperf/commands/utils"
internaltypes "github.com/Azure/kperf/contrib/internal/types"
"github.com/Azure/kperf/contrib/internal/utils"

"github.com/urfave/cli"
Expand All @@ -30,62 +31,77 @@ that nodes. It repeats to create and delete job. The load profile is fixed.
},
},
Action: func(cliCtx *cli.Context) error {
ctx := context.Background()
kubeCfgPath := cliCtx.GlobalString("kubeconfig")

rgCfgFile, rgCfgFileDone, err := utils.NewLoadProfileFromEmbed(
"loadprofile/node100_job1_pod3k.yaml",
func(spec *types.RunnerGroupSpec) error {
reqs := cliCtx.Int("total-requests")
if reqs < 0 {
return fmt.Errorf("invalid total-requests value: %v", reqs)
}

rgAffinity := cliCtx.GlobalString("rg-affinity")
affinityLabels, err := kperfcmdutils.KeyValuesMap([]string{rgAffinity})
if err != nil {
return fmt.Errorf("failed to parse %s affinity: %w", rgAffinity, err)
}

spec.Profile.Spec.Total = reqs
spec.NodeAffinity = affinityLabels

data, _ := yaml.Marshal(spec)
klog.V(2).InfoS("Load Profile", "config", string(data))
return nil
},
)
if err != nil {
return err
}
defer func() { _ = rgCfgFileDone() }()

vcDone, err := deployVirtualNodepool(ctx, cliCtx, "node100job1pod3k", 100, 110)
if err != nil {
return fmt.Errorf("failed to deploy virtual node: %w", err)
}
defer func() { _ = vcDone() }()

var wg sync.WaitGroup
wg.Add(1)

jobCtx, jobCancel := context.WithCancel(ctx)
go func() {
defer wg.Done()

utils.RepeatJobWith3KPod(jobCtx, kubeCfgPath, "job1pod3k", 5*time.Second)
}()

// TODO(weifu): write result into file
_, derr := utils.DeployRunnerGroup(ctx,
cliCtx.GlobalString("kubeconfig"),
cliCtx.GlobalString("runner-image"),
rgCfgFile,
cliCtx.GlobalString("runner-flowcontrol"),
cliCtx.GlobalString("rg-affinity"),
)
jobCancel()
wg.Wait()
return derr
_, err := renderBenchmarkReportInterceptor(
addAPIServerCoresInfoInterceptor(benchNode100Job1Pod3KCaseRun),
)(cliCtx)
return err
},
}

// benchNode100Job1Pod3KCaseRun is for benchNode100Job1Pod3KCase subcommand.
func benchNode100Job1Pod3KCaseRun(cliCtx *cli.Context) (*internaltypes.BenchmarkReport, error) {
ctx := context.Background()
kubeCfgPath := cliCtx.GlobalString("kubeconfig")

rgCfgFile, rgCfgFileDone, err := utils.NewLoadProfileFromEmbed(
"loadprofile/node100_job1_pod3k.yaml",
func(spec *types.RunnerGroupSpec) error {
reqs := cliCtx.Int("total-requests")
if reqs < 0 {
return fmt.Errorf("invalid total-requests value: %v", reqs)
}

rgAffinity := cliCtx.GlobalString("rg-affinity")
affinityLabels, err := kperfcmdutils.KeyValuesMap([]string{rgAffinity})
if err != nil {
return fmt.Errorf("failed to parse %s affinity: %w", rgAffinity, err)
}

spec.Profile.Spec.Total = reqs
spec.NodeAffinity = affinityLabels

data, _ := yaml.Marshal(spec)
klog.V(2).InfoS("Load Profile", "config", string(data))
return nil
},
)
if err != nil {
return nil, err
}
defer func() { _ = rgCfgFileDone() }()

vcDone, err := deployVirtualNodepool(ctx, cliCtx, "node100job1pod3k", 100, 110)
if err != nil {
return nil, fmt.Errorf("failed to deploy virtual node: %w", err)
}
defer func() { _ = vcDone() }()

var wg sync.WaitGroup
wg.Add(1)

jobCtx, jobCancel := context.WithCancel(ctx)
go func() {
defer wg.Done()

utils.RepeatJobWith3KPod(jobCtx, kubeCfgPath, "job1pod3k", 5*time.Second)
}()

rgResult, derr := utils.DeployRunnerGroup(ctx,
cliCtx.GlobalString("kubeconfig"),
cliCtx.GlobalString("runner-image"),
rgCfgFile,
cliCtx.GlobalString("runner-flowcontrol"),
cliCtx.GlobalString("rg-affinity"),
)
jobCancel()
wg.Wait()

if derr != nil {
return nil, derr
}

return &internaltypes.BenchmarkReport{
RunnerGroupsReport: *rgResult,
Info: make(map[string]interface{}),
}, nil
}
44 changes: 4 additions & 40 deletions contrib/cmd/runkperf/commands/bench/root.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package bench

import (
"context"
"fmt"

kperfcmdutils "github.com/Azure/kperf/cmd/kperf/commands/utils"
"github.com/Azure/kperf/contrib/internal/utils"
"k8s.io/klog/v2"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -51,43 +46,12 @@ var Command = cli.Command{
Usage: "Indicates the target kubernetes cluster is EKS",
Hidden: true,
},
cli.StringFlag{
Name: "result",
Usage: "Path to the file which stores results",
},
},
Subcommands: []cli.Command{
benchNode100Job1Pod3KCase,
},
}

// deployVirtualNodepool deploys virtual nodepool.
func deployVirtualNodepool(ctx context.Context, cliCtx *cli.Context, target string, nodes, maxPods int) (func() error, error) {
klog.V(0).InfoS("Deploying virtual nodepool", "name", target)

kubeCfgPath := cliCtx.GlobalString("kubeconfig")
virtualNodeAffinity := cliCtx.GlobalString("vc-affinity")

kr := utils.NewKperfRunner(kubeCfgPath, "")

var sharedProviderID string
var err error

if cliCtx.GlobalBool("eks") {
sharedProviderID, err = utils.FetchNodeProviderIDByType(ctx, kubeCfgPath, utils.EKSIdleNodepoolInstanceType)
if err != nil {
return nil, fmt.Errorf("failed to get EKS idle node (type: %s) providerID: %w",
utils.EKSIdleNodepoolInstanceType, err)
}
}

klog.V(0).InfoS("Trying to delete nodepool if necessary", "name", target)
if err = kr.DeleteNodepool(ctx, 0, target); err != nil {
klog.V(0).ErrorS(err, "failed to delete nodepool", "name", target)
}

err = kr.NewNodepool(ctx, 0, target, nodes, maxPods, virtualNodeAffinity, sharedProviderID)
if err != nil {
return nil, fmt.Errorf("failed to create nodepool %s: %w", target, err)
}

return func() error {
return kr.DeleteNodepool(ctx, 0, target)
}, nil
}
122 changes: 122 additions & 0 deletions contrib/cmd/runkperf/commands/bench/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package bench

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"

internaltypes "github.com/Azure/kperf/contrib/internal/types"
"github.com/Azure/kperf/contrib/internal/utils"

"github.com/urfave/cli"
"k8s.io/klog/v2"
)

// subcmdActionFunc is to unify each subcommand's interface. They should return
// benchmark report as result.
type subcmdActionFunc func(*cli.Context) (*internaltypes.BenchmarkReport, error)

// addAPIServerCoresInfoInterceptor adds apiserver's cores into benchmark report.
func addAPIServerCoresInfoInterceptor(handler subcmdActionFunc) subcmdActionFunc {
return func(cliCtx *cli.Context) (*internaltypes.BenchmarkReport, error) {
ctx := context.Background()
kubeCfgPath := cliCtx.GlobalString("kubeconfig")

beforeCores, ferr := utils.FetchAPIServerCores(ctx, kubeCfgPath)
if ferr != nil {
klog.ErrorS(ferr, "failed to fetch apiserver cores")
}

report, err := handler(cliCtx)
if err != nil {
return nil, err
}

afterCores, ferr := utils.FetchAPIServerCores(ctx, kubeCfgPath)
if ferr != nil {
klog.ErrorS(ferr, "failed to fetch apiserver cores")
}

report.Info["apiserver"] = map[string]interface{}{
"cores": map[string]interface{}{
"before": beforeCores,
"after": afterCores,
},
}
return report, nil
}
}

// renderBenchmarkReportInterceptor renders benchmark report into file or stdout.
func renderBenchmarkReportInterceptor(handler subcmdActionFunc) subcmdActionFunc {
return func(cliCtx *cli.Context) (*internaltypes.BenchmarkReport, error) {
report, err := handler(cliCtx)
if err != nil {
return nil, err
}

outF := os.Stdout
if targetFile := cliCtx.GlobalString("result"); targetFile != "" {
targetFileDir := filepath.Dir(targetFile)

_, err = os.Stat(targetFileDir)
if err != nil && os.IsNotExist(err) {
err = os.MkdirAll(targetFileDir, 0750)
}
if err != nil {
return nil, fmt.Errorf("failed to ensure output's dir %s: %w", targetFileDir, err)
}

outF, err = os.Create(targetFile)
if err != nil {
return nil, err
}
defer outF.Close()
}

encoder := json.NewEncoder(outF)
encoder.SetIndent("", " ")

if err := encoder.Encode(report); err != nil {
return nil, fmt.Errorf("failed to encode json: %w", err)
}
return report, nil
}
}

// deployVirtualNodepool deploys virtual nodepool.
func deployVirtualNodepool(ctx context.Context, cliCtx *cli.Context, target string, nodes, maxPods int) (func() error, error) {
klog.V(0).InfoS("Deploying virtual nodepool", "name", target)

kubeCfgPath := cliCtx.GlobalString("kubeconfig")
virtualNodeAffinity := cliCtx.GlobalString("vc-affinity")

kr := utils.NewKperfRunner(kubeCfgPath, "")

var sharedProviderID string
var err error

if cliCtx.GlobalBool("eks") {
sharedProviderID, err = utils.FetchNodeProviderIDByType(ctx, kubeCfgPath, utils.EKSIdleNodepoolInstanceType)
if err != nil {
return nil, fmt.Errorf("failed to get EKS idle node (type: %s) providerID: %w",
utils.EKSIdleNodepoolInstanceType, err)
}
}

klog.V(0).InfoS("Trying to delete nodepool if necessary", "name", target)
if err = kr.DeleteNodepool(ctx, 0, target); err != nil {
klog.V(0).ErrorS(err, "failed to delete nodepool", "name", target)
}

err = kr.NewNodepool(ctx, 0, target, nodes, maxPods, virtualNodeAffinity, sharedProviderID)
if err != nil {
return nil, fmt.Errorf("failed to create nodepool %s: %w", target, err)
}

return func() error {
return kr.DeleteNodepool(ctx, 0, target)
}, nil
}
Loading
Loading