Skip to content

Commit

Permalink
add code to reuse connection and accept protobuf content type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sara Wei committed Jan 4, 2024
1 parent e4d6054 commit 42be2de
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@ bin/
# vendor/

# Go workspace file
go.work

#tmp folder which contains .yaml files
tmp/
go.work
2 changes: 2 additions & 0 deletions api/types/load_traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type LoadProfileSpec struct {
Total int `json:"total" yaml:"total"`
// Conns defines total number of long connections used for traffic.
Conns int `json:"conns" yaml:"conns"`
// Client defines total number of HTTP clients.
Client int `json:"client" yaml:"client"`
// Requests defines the different kinds of requests with weights.
// The executor should randomly pick by weight.
Requests []*WeightedRequest
Expand Down
61 changes: 61 additions & 0 deletions cmd/kperf/commands/runner/runner.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package runner

Check failure on line 1 in cmd/kperf/commands/runner/runner.go

View workflow job for this annotation

GitHub Actions / linter

: # github.com/Azure/kperf/cmd/kperf/commands/runner

import (
"context"
"context"
"fmt"
"os"
"sort"

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

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

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

// Command represents runner subcommand.
"gopkg.in/yaml.v2"

Check failure on line 23 in cmd/kperf/commands/runner/runner.go

View workflow job for this annotation

GitHub Actions / test

syntax error: non-declaration statement outside function body

Check failure on line 23 in cmd/kperf/commands/runner/runner.go

View workflow job for this annotation

GitHub Actions / build

syntax error: non-declaration statement outside function body

Check failure on line 23 in cmd/kperf/commands/runner/runner.go

View workflow job for this annotation

GitHub Actions / linter

syntax error: non-declaration statement outside function body (typecheck)

Check failure on line 23 in cmd/kperf/commands/runner/runner.go

View workflow job for this annotation

GitHub Actions / linter

expected declaration, found "gopkg.in/yaml.v2" (typecheck)

Check failure on line 23 in cmd/kperf/commands/runner/runner.go

View workflow job for this annotation

GitHub Actions / linter

syntax error: non-declaration statement outside function body) (typecheck)
)

// Command represents runner subcommand.
var Command = cli.Command{
Name: "runner",
Expand All @@ -22,6 +32,54 @@ var Command = cli.Command{
},
}

var runCommand = cli.Command{
Name: "run",
Usage: "run a benchmark test to kube-apiserver",
Flags: []cli.Flag{
cli.StringFlag{
Name: "kubeconfig",
Usage: "Path to the kubeconfig file",
},
cli.IntFlag{
Name: "client",
Usage: "Total number of HTTP clients",
Value: 1,
},
cli.StringFlag{
Name: "config",
Usage: "Path to the configuration file",
Required: true,
},
cli.IntFlag{
Name: "conns",
Usage: "Total number of connections. It can override corresponding value defined by --config",
Value: 1,
},
cli.StringFlag{
Name: "content-type",
Usage: "Content type (json or protobuf)",
Value: "json",
},
cli.IntFlag{
Name: "rate",
Usage: "Maximum requests per second (Zero means no limitation). It can override corresponding value defined by --config",
},
cli.IntFlag{
Name: "total",
Usage: "Total number of requests. It can override corresponding value defined by --config",
Value: 1000,
},
cli.StringFlag{
Name: "user-agent",
Usage: "User Agent",
},
},
Usage: "Setup benchmark to kube-apiserver from one endpoint",
Subcommands: []cli.Command{
runCommand,
},
}

var runCommand = cli.Command{
Name: "run",
Usage: "run a benchmark test to kube-apiserver",
Expand Down Expand Up @@ -114,6 +172,9 @@ func loadConfig(cliCtx *cli.Context) (*types.LoadProfile, error) {
if v := "conns"; cliCtx.IsSet(v) || profileCfg.Spec.Conns == 0 {
profileCfg.Spec.Conns = cliCtx.Int(v)
}
if v := "client"; cliCtx.IsSet(v) || profileCfg.Spec.Client == 0 {
profileCfg.Spec.Client = cliCtx.Int(v)
}
if v := "total"; cliCtx.IsSet(v) || profileCfg.Spec.Total == 0 {
profileCfg.Spec.Total = cliCtx.Int(v)
}
Expand Down
5 changes: 0 additions & 5 deletions request/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@ import (
//
// 1. Is it possible to build one http2 client with multiple connections?
// 2. How to monitor HTTP2 GOAWAY frame?
// 3. Support Protobuf as accepted content
func NewClients(kubeCfgPath string, ConnsNum int, userAgent string, qps int, contentType string) ([]rest.Interface, error) {
restCfg, err := clientcmd.BuildConfigFromFlags("", kubeCfgPath)

if err != nil {
return nil, err
}

if qps == 0 {
qps = math.MaxInt32
}

restCfg.QPS = float32(qps)
restCfg.NegotiatedSerializer = scheme.Codecs.WithoutConversion()

restCfg.UserAgent = userAgent

if restCfg.UserAgent == "" {
restCfg.UserAgent = rest.DefaultKubernetesUserAgent()
}
Expand All @@ -52,7 +48,6 @@ func NewClients(kubeCfgPath string, ConnsNum int, userAgent string, qps int, con

restCli, err := rest.UnversionedRESTClientFor(&cfgShallowCopy)
if err != nil {
fmt.Printf("Failed to create rest client: %v\n", err)
return nil, err
}
restClients = append(restClients, restCli)
Expand Down
6 changes: 3 additions & 3 deletions request/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
const defaultTimeout = 60 * time.Second

// Schedule files requests to apiserver based on LoadProfileSpec.
func Schedule(ctx context.Context, clientNum int, spec *types.LoadProfileSpec, restCli []rest.Interface) (*types.ResponseStats, error) {
func Schedule(ctx context.Context, spec *types.LoadProfileSpec, restCli []rest.Interface) (*types.ResponseStats, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand All @@ -36,8 +36,8 @@ func Schedule(ctx context.Context, clientNum int, spec *types.LoadProfileSpec, r
var wg sync.WaitGroup

respMetric := metrics.NewResponseMetric()
for i := 0; i < clientNum; i++ {
//reuse connection if client > conns
for i := 0; i < spec.Client; i++ {
// reuse connection if clients > conns
cli := restCli[i%len(restCli)]
wg.Add(1)
go func(cli rest.Interface) {
Expand Down

0 comments on commit 42be2de

Please sign in to comment.