Skip to content

Commit

Permalink
adding changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mchinta7 committed Jan 16, 2024
1 parent e3e183f commit becca4e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
61 changes: 56 additions & 5 deletions cmd/kperf/commands/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package runner

import (
"context"
"encoding/json"

"flag"
"fmt"
"os"
"path/filepath"
"strconv"

"github.com/Azure/kperf/api/types"
"github.com/Azure/kperf/request"

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

// Command represents runner subcommand.
Expand Down Expand Up @@ -67,8 +72,30 @@ var runCommand = cli.Command{
Name: "result",
Usage: "Path to the file which stores results",
},
cli.BoolFlag{
Name: "raw-data",
Usage: "write ResponseStats to file in .json format",
},
cli.StringFlag{
Name: "v",
Usage: "log level for V logs",
Value: "0",
},
},
Action: func(cliCtx *cli.Context) error {
// initialize klog
klog.InitFlags(nil)

vFlag, err := strconv.Atoi(cliCtx.String("v"))
if err != nil || vFlag < 0 {
return fmt.Errorf("invalid value \"%v\" for flag -v: value must be a non-negative integer", cliCtx.String("v"))
}
if err := flag.Set("v", strconv.Itoa(cliCtx.Int("v"))); err != nil {
return fmt.Errorf("failed to set log level: %w", err)
}
defer klog.Flush()
flag.Parse()

profileCfg, err := loadConfig(cliCtx)
if err != nil {
return err
Expand All @@ -79,6 +106,7 @@ var runCommand = cli.Command{
kubeCfgPath := cliCtx.String("kubeconfig")
userAgent := cliCtx.String("user-agent")
outputFilePath := cliCtx.String("result")
rawDataFlagIncluded := cliCtx.Bool("result")

conns := profileCfg.Spec.Conns
rate := profileCfg.Spec.Rate
Expand Down Expand Up @@ -111,8 +139,11 @@ var runCommand = cli.Command{
defer f.Close()
}

//TODO: add printResponseStats for .json format
printResponseStats(f, stats)
err = printResponseStats(f, rawDataFlagIncluded, stats)
if err != nil {
return fmt.Errorf("error while printing response stats: %w", err)
}

return nil
},
}
Expand Down Expand Up @@ -152,7 +183,27 @@ func loadConfig(cliCtx *cli.Context) (*types.LoadProfile, error) {
return &profileCfg, nil
}

// TODO: Complete this function
func printResponseStats(f *os.File, stats *request.Result) {
fmt.Fprintf(f, "Response Stat: %v\n", stats)
func printResponseStats(f *os.File, rawDataFlagIncluded bool, stats *request.Result) error {
output := types.RunnerMetricReport{
Total: stats.Total,
FailureList: stats.FailureList,
Duration: stats.Duration,
Latencies: stats.Latencies,
TotalReceivedBytes: stats.TotalReceivedBytes,
}

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

if !rawDataFlagIncluded {
output.Latencies = nil
}

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

return nil

}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
k8s.io/apimachinery v0.28.4
k8s.io/cli-runtime v0.28.4
k8s.io/client-go v0.28.4
k8s.io/klog/v2 v2.100.1
k8s.io/kubectl v0.28.4
)

Expand Down Expand Up @@ -135,7 +136,6 @@ require (
k8s.io/apiextensions-apiserver v0.28.4 // indirect
k8s.io/apiserver v0.28.4 // indirect
k8s.io/component-base v0.28.4 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
oras.land/oras-go v1.2.4 // indirect
Expand Down
1 change: 1 addition & 0 deletions manifests/virtualcluster/nodes/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
apiVersion: v1,
"name": "virtualnodes",
"version": "0.0.1"
}
2 changes: 1 addition & 1 deletion manifests/virtualcluster/nodes/values.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ""
name: "vc-testing"
controllerNodeSelectors: {}
replicas: 0
nodeLabels: {}
Expand Down
5 changes: 5 additions & 0 deletions request/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"golang.org/x/time/rate"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)

const defaultTimeout = 60 * time.Second
Expand Down Expand Up @@ -55,7 +56,10 @@ func Schedule(ctx context.Context, spec *types.LoadProfileSpec, restCli []rest.I
for builder := range reqBuilderCh {
_, req := builder.Build(cli)

klog.V(9).Infof("Request URL: %s", req.URL())

if err := limiter.Wait(ctx); err != nil {
klog.V(9).Infof("Rate limiter wait failed: %v", err)
cancel()
return
}
Expand All @@ -77,6 +81,7 @@ func Schedule(ctx context.Context, spec *types.LoadProfileSpec, restCli []rest.I

if err != nil {
respMetric.ObserveFailure(err)
klog.V(9).Infof("Request stream failed: %v", err)
}
}()
}
Expand Down

0 comments on commit becca4e

Please sign in to comment.