Skip to content

Commit

Permalink
Gloohelpers measure (#8458)
Browse files Browse the repository at this point in the history
* add Measure(), comments

* changelog

* use Measure() in MeasureIgnore0ns()

* fix dot-import conflict

* fix import conflict

---------

Co-authored-by: soloio-bulldozer[bot] <48420018+soloio-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
bewebi and soloio-bulldozer[bot] authored Jul 12, 2023
1 parent 052564a commit 2e1c43b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
9 changes: 9 additions & 0 deletions changelog/v1.15.0-beta18/gloohelpers-measure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
changelog:
- type: NON_USER_FACING
issueLink: https://github.com/solo-io/solo-projects/issues/5038
resolvesIssue: false
description: >-
Add Measure function to gloohelpers for cases where we do not care about the 0ns error
skipCI-kube-tests:true
skipCI-docs-build:true
11 changes: 10 additions & 1 deletion test/helpers/benchmark_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ import (
// MeasureIgnore0ns as implemented here for Mac/Darwin is meant to be used in performance tests when developing locally
// It is a less-precise method for measuring than the Linux implementation, and targets should be derived based on
// performance when running on the Linux GHA runner we use for Nightly tests
// Ignore will always be false
func MeasureIgnore0ns(f func()) (Result, bool, error) {
res, err := Measure(f)
return res, false, err
}

// Measure as implemented here for Mac/Darwin is meant to be used in performance tests when developing locally
// It is a less-precise method for measuring than the Linux implementation, and targets should be derived based on
// performance when running on the Linux GHA runner we use for Nightly tests
func Measure(f func()) (Result, error) {
before := time.Now()
f()
elapsed := time.Since(before)
return Result{
Total: elapsed,
}, false, nil
}, nil
}
12 changes: 11 additions & 1 deletion test/helpers/benchmark_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import (
"github.com/solo-io/go-utils/testutils/benchmarking"
)

// MeasureIgnore0ns wraps benchmarking.Measure, checking error values for the 0ns error and returning true as the middle
// argument if that is the error
// The 0ns error occurs when measuring very short durations (~100µs) when measurements may round down to 0ns
// This function should be used in circumstances where we want to ignore that particular error but not others
func MeasureIgnore0ns(f func()) (benchmarking.Result, bool, error) {
res, err := benchmarking.Measure(f)
res, err := Measure(f)
if err != nil && strings.Contains(err.Error(), "total execution time was 0 ns") {
return res, true, nil
}
return res, false, err
}

// Measure wraps benchmarking.Measure 1:1
// It is redefined here so that we can build with a darwin-compatible version depending on the machine being used
func Measure(f func()) (benchmarking.Result, error) {
return benchmarking.Measure(f)
}
10 changes: 5 additions & 5 deletions test/helpers/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
kubev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -74,7 +74,7 @@ type Params struct {
func GetCerts(params Params) (string, string) {

if len(params.Hosts) == 0 {
Fail("Missing required --host parameter")
ginkgo.Fail("Missing required --host parameter")
}

var priv interface{}
Expand All @@ -98,7 +98,7 @@ func GetCerts(params Params) (string, string) {
case "P521":
priv, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
default:
Fail(fmt.Sprintf("Unrecognized elliptic curve: %q", params.EcdsaCurve))
ginkgo.Fail(fmt.Sprintf("Unrecognized elliptic curve: %q", params.EcdsaCurve))
}
Expect(err).NotTo(HaveOccurred())
}
Expand All @@ -120,7 +120,7 @@ func GetCerts(params Params) (string, string) {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
Fail(fmt.Sprintf("failed to generate serial number: %s", err))
ginkgo.Fail(fmt.Sprintf("failed to generate serial number: %s", err))
}

template := x509.Certificate{
Expand Down Expand Up @@ -153,7 +153,7 @@ func GetCerts(params Params) (string, string) {

derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
if err != nil {
Fail(fmt.Sprintf("Failed to create certificate: %s", err))
ginkgo.Fail(fmt.Sprintf("Failed to create certificate: %s", err))
}

var certOut bytes.Buffer
Expand Down
14 changes: 7 additions & 7 deletions test/helpers/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ package helpers_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/solo-io/gloo/test/helpers"
"github.com/solo-io/gloo/test/helpers"
)

var _ = Describe("PercentileIndex", func() {
It("panics on percentile <= 0", func() {
Expect(func() { PercentileIndex(100, -1) }).To(Panic())
Expect(func() { helpers.PercentileIndex(100, -1) }).To(Panic())
})

It("panics on percentile > 100", func() {
Expect(func() { PercentileIndex(100, 101) }).To(Panic())
Expect(func() { helpers.PercentileIndex(100, 101) }).To(Panic())
})

It("returns 0 for 1st percentile for len <=100", func() {
for i := 1; i <= 100; i++ {
Expect(PercentileIndex(i, 1)).To(Equal(0))
Expect(helpers.PercentileIndex(i, 1)).To(Equal(0))
}
})

It("returns 1 for 1st percentile for len >100, <=200", func() {
for i := 101; i <= 200; i++ {
Expect(PercentileIndex(i, 1)).To(Equal(1))
Expect(helpers.PercentileIndex(i, 1)).To(Equal(1))
}
})

It("always returns len-1 for 100th percentile", func() {
for i := 1; i <= 200; i++ {
Expect(PercentileIndex(i, 100)).To(Equal(i - 1))
Expect(helpers.PercentileIndex(i, 100)).To(Equal(i - 1))
}
})

It("returns index 3 for 80th percentile and length 5", func() {
Expect(PercentileIndex(5, 80)).To(Equal(3))
Expect(helpers.PercentileIndex(5, 80)).To(Equal(3))
})
})

0 comments on commit 2e1c43b

Please sign in to comment.