snapshot of https://github.com/grpc/grpc-go.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
This commit is contained in:
178
benchmark/flags/flags.go
Normal file
178
benchmark/flags/flags.go
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
Package flags provide convenience types and routines to accept specific types
|
||||
of flag values on the command line.
|
||||
*/
|
||||
package flags
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"flag"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// stringFlagWithAllowedValues represents a string flag which can only take a
|
||||
// predefined set of values.
|
||||
type stringFlagWithAllowedValues struct {
|
||||
val string
|
||||
allowed []string
|
||||
}
|
||||
|
||||
// StringWithAllowedValues returns a flag variable of type
|
||||
// stringFlagWithAllowedValues configured with the provided parameters.
|
||||
// 'allowed` is the set of values that this flag can be set to.
|
||||
func StringWithAllowedValues(name, defaultVal, usage string, allowed []string) *string {
|
||||
as := &stringFlagWithAllowedValues{defaultVal, allowed}
|
||||
flag.CommandLine.Var(as, name, usage)
|
||||
return &as.val
|
||||
}
|
||||
|
||||
// String implements the flag.Value interface.
|
||||
func (as *stringFlagWithAllowedValues) String() string {
|
||||
return as.val
|
||||
}
|
||||
|
||||
// Set implements the flag.Value interface.
|
||||
func (as *stringFlagWithAllowedValues) Set(val string) error {
|
||||
for _, a := range as.allowed {
|
||||
if a == val {
|
||||
as.val = val
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("want one of: %v", strings.Join(as.allowed, ", "))
|
||||
}
|
||||
|
||||
type durationSliceValue []time.Duration
|
||||
|
||||
// DurationSlice returns a flag representing a slice of time.Duration objects.
|
||||
func DurationSlice(name string, defaultVal []time.Duration, usage string) *[]time.Duration {
|
||||
ds := make([]time.Duration, len(defaultVal))
|
||||
copy(ds, defaultVal)
|
||||
dsv := (*durationSliceValue)(&ds)
|
||||
flag.CommandLine.Var(dsv, name, usage)
|
||||
return &ds
|
||||
}
|
||||
|
||||
// Set implements the flag.Value interface.
|
||||
func (dsv *durationSliceValue) Set(s string) error {
|
||||
ds := strings.Split(s, ",")
|
||||
var dd []time.Duration
|
||||
for _, n := range ds {
|
||||
d, err := time.ParseDuration(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dd = append(dd, d)
|
||||
}
|
||||
*dsv = durationSliceValue(dd)
|
||||
return nil
|
||||
}
|
||||
|
||||
// String implements the flag.Value interface.
|
||||
func (dsv *durationSliceValue) String() string {
|
||||
var b bytes.Buffer
|
||||
for i, d := range *dsv {
|
||||
if i > 0 {
|
||||
b.WriteRune(',')
|
||||
}
|
||||
b.WriteString(d.String())
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
type intSliceValue []int
|
||||
|
||||
// IntSlice returns a flag representing a slice of ints.
|
||||
func IntSlice(name string, defaultVal []int, usage string) *[]int {
|
||||
is := make([]int, len(defaultVal))
|
||||
copy(is, defaultVal)
|
||||
isv := (*intSliceValue)(&is)
|
||||
flag.CommandLine.Var(isv, name, usage)
|
||||
return &is
|
||||
}
|
||||
|
||||
// Set implements the flag.Value interface.
|
||||
func (isv *intSliceValue) Set(s string) error {
|
||||
is := strings.Split(s, ",")
|
||||
var ret []int
|
||||
for _, n := range is {
|
||||
i, err := strconv.Atoi(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ret = append(ret, i)
|
||||
}
|
||||
*isv = intSliceValue(ret)
|
||||
return nil
|
||||
}
|
||||
|
||||
// String implements the flag.Value interface.
|
||||
func (isv *intSliceValue) String() string {
|
||||
var b bytes.Buffer
|
||||
for i, n := range *isv {
|
||||
if i > 0 {
|
||||
b.WriteRune(',')
|
||||
}
|
||||
b.WriteString(strconv.Itoa(n))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
type stringSliceValue []string
|
||||
|
||||
// StringSlice returns a flag representing a slice of strings.
|
||||
func StringSlice(name string, defaultVal []string, usage string) *[]string {
|
||||
ss := make([]string, len(defaultVal))
|
||||
copy(ss, defaultVal)
|
||||
ssv := (*stringSliceValue)(&ss)
|
||||
flag.CommandLine.Var(ssv, name, usage)
|
||||
return &ss
|
||||
}
|
||||
|
||||
// escapedCommaSplit splits a comma-separated list of strings in the same way
|
||||
// CSV files work (escaping a comma requires double-quotes).
|
||||
func escapedCommaSplit(str string) ([]string, error) {
|
||||
r := csv.NewReader(strings.NewReader(str))
|
||||
ret, err := r.Read()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Set implements the flag.Value interface.
|
||||
func (ss *stringSliceValue) Set(str string) error {
|
||||
var err error
|
||||
*ss, err = escapedCommaSplit(str)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// String implements the flag.Value interface.
|
||||
func (ss *stringSliceValue) String() string {
|
||||
return strings.Join(*ss, ",")
|
||||
}
|
||||
153
benchmark/flags/flags_test.go
Normal file
153
benchmark/flags/flags_test.go
Normal file
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2019 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package flags
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/internal/grpctest"
|
||||
)
|
||||
|
||||
type s struct {
|
||||
grpctest.Tester
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
grpctest.RunSubTests(t, s{})
|
||||
}
|
||||
|
||||
func (s) TestStringWithAllowedValues(t *testing.T) {
|
||||
const defaultVal = "default"
|
||||
tests := []struct {
|
||||
args string
|
||||
allowed []string
|
||||
wantVal string
|
||||
wantErr bool
|
||||
}{
|
||||
{"-workloads=all", []string{"unary", "streaming", "all"}, "all", false},
|
||||
{"-workloads=disallowed", []string{"unary", "streaming", "all"}, defaultVal, true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
|
||||
var w = StringWithAllowedValues("workloads", defaultVal, "usage", test.allowed)
|
||||
err := flag.CommandLine.Parse([]string{test.args})
|
||||
switch {
|
||||
case !test.wantErr && err != nil:
|
||||
t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
|
||||
case test.wantErr && err == nil:
|
||||
t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
|
||||
default:
|
||||
if *w != test.wantVal {
|
||||
t.Errorf("flag value is %v, want %v", *w, test.wantVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s) TestDurationSlice(t *testing.T) {
|
||||
defaultVal := []time.Duration{time.Second, time.Nanosecond}
|
||||
tests := []struct {
|
||||
args string
|
||||
wantVal []time.Duration
|
||||
wantErr bool
|
||||
}{
|
||||
{"-latencies=1s", []time.Duration{time.Second}, false},
|
||||
{"-latencies=1s,2s,3s", []time.Duration{time.Second, 2 * time.Second, 3 * time.Second}, false},
|
||||
{"-latencies=bad", defaultVal, true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
|
||||
var w = DurationSlice("latencies", defaultVal, "usage")
|
||||
err := flag.CommandLine.Parse([]string{test.args})
|
||||
switch {
|
||||
case !test.wantErr && err != nil:
|
||||
t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
|
||||
case test.wantErr && err == nil:
|
||||
t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
|
||||
default:
|
||||
if !reflect.DeepEqual(*w, test.wantVal) {
|
||||
t.Errorf("flag value is %v, want %v", *w, test.wantVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s) TestIntSlice(t *testing.T) {
|
||||
defaultVal := []int{1, 1024}
|
||||
tests := []struct {
|
||||
args string
|
||||
wantVal []int
|
||||
wantErr bool
|
||||
}{
|
||||
{"-kbps=1", []int{1}, false},
|
||||
{"-kbps=1,2,3", []int{1, 2, 3}, false},
|
||||
{"-kbps=20e4", defaultVal, true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
|
||||
var w = IntSlice("kbps", defaultVal, "usage")
|
||||
err := flag.CommandLine.Parse([]string{test.args})
|
||||
switch {
|
||||
case !test.wantErr && err != nil:
|
||||
t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
|
||||
case test.wantErr && err == nil:
|
||||
t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
|
||||
default:
|
||||
if !reflect.DeepEqual(*w, test.wantVal) {
|
||||
t.Errorf("flag value is %v, want %v", *w, test.wantVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s) TestStringSlice(t *testing.T) {
|
||||
defaultVal := []string{"bar", "baz"}
|
||||
tests := []struct {
|
||||
args string
|
||||
wantVal []string
|
||||
wantErr bool
|
||||
}{
|
||||
{"-name=foobar", []string{"foobar"}, false},
|
||||
{"-name=foo,bar", []string{"foo", "bar"}, false},
|
||||
{`-name="foo,bar",baz`, []string{"foo,bar", "baz"}, false},
|
||||
{`-name="foo,bar""",baz`, []string{`foo,bar"`, "baz"}, false},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
|
||||
var w = StringSlice("name", defaultVal, "usage")
|
||||
err := flag.CommandLine.Parse([]string{test.args})
|
||||
switch {
|
||||
case !test.wantErr && err != nil:
|
||||
t.Errorf("failed to parse command line args {%v}: %v", test.args, err)
|
||||
case test.wantErr && err == nil:
|
||||
t.Errorf("flag.Parse(%v) = nil, want non-nil error", test.args)
|
||||
default:
|
||||
if !reflect.DeepEqual(*w, test.wantVal) {
|
||||
t.Errorf("flag value is %v, want %v", *w, test.wantVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user