-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from vegaprotocol/feat/add-benchmark-command
feat: add benchmarking tool for benchmarking Vega APIs
- Loading branch information
Showing
5 changed files
with
362 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"time" | ||
|
||
benchmark "code.vegaprotocol.io/vegatools/grpcapibenchmark" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var benchmarkCmd = &cobra.Command{ | ||
Use: "benchmark", | ||
Short: "A benchmarking tool for vega APIs", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(benchmarkCmd) | ||
benchmarkCmd.PersistentFlags().StringSliceP("urls", "u", []string{"localhost:3007"}, "list of gRPC host:port of the servers to benchmark") | ||
benchmarkCmd.PersistentFlags().DurationP("timeout", "t", time.Minute, "Timeout for each benchmark test") | ||
benchmarkCmd.PersistentFlags().IntP("iterations", "i", 1, "Number of iterations to run") | ||
benchmarkCmd.PersistentFlags().IntP("workers", "w", 1, "Number of concurrent workers to use") | ||
benchmarkCmd.PersistentFlags().IntP("requests", "q", 100, "Number of requests to send per iteration") | ||
|
||
benchmarkCmd.AddCommand(benchmark.ListOrdersCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package grpcapibenchmark | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"time" | ||
|
||
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" | ||
) | ||
|
||
type apiTest func(ctx context.Context, client v2.TradingDataServiceClient) time.Duration | ||
|
||
func worker(ctx context.Context, client v2.TradingDataServiceClient, apiTest apiTest, reqCh <-chan struct{}, resultCh chan<- time.Duration, doneCh chan<- struct{}) { | ||
defer func() { | ||
doneCh <- struct{}{} | ||
}() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case _, ok := <-reqCh: | ||
if !ok { | ||
return | ||
} | ||
elapsed := apiTest(ctx, client) | ||
resultCh <- elapsed | ||
} | ||
} | ||
} | ||
|
||
type averagable interface { | ||
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | time.Duration | ||
} | ||
|
||
func mean[T averagable](values []T) T { | ||
if len(values) == 0 { | ||
return 0 | ||
} | ||
|
||
if len(values) == 1 { | ||
return values[0] | ||
} | ||
|
||
var total T | ||
for _, d := range values { | ||
total += d | ||
} | ||
return total / T(len(values)) | ||
} | ||
|
||
func median[T averagable](values []T) T { | ||
if len(values) == 0 { | ||
return 0 | ||
} | ||
|
||
if len(values) == 1 { | ||
return values[0] | ||
} | ||
|
||
sort.Slice(values, func(i, j int) bool { | ||
return values[i] < values[j] | ||
}) | ||
middle := len(values) / 2 | ||
if len(values)%2 == 0 { | ||
return (values[middle-1] + values[middle]) / 2 | ||
} | ||
return values[middle] | ||
} |
Oops, something went wrong.