Skip to content

Commit

Permalink
refactor performance test to be standard golang benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
hickford committed Oct 24, 2023
1 parent 32d8909 commit fb5a885
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 78 deletions.
94 changes: 19 additions & 75 deletions apps/tests/performance/performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"math/rand"
"os"
"testing"
"time"

Expand All @@ -17,7 +16,6 @@ import (
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth/fake"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth/ops/accesstokens"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth/ops/authority"
"github.com/montanaflynn/stats"
)

func fakeClient() (base.Client, error) {
Expand Down Expand Up @@ -67,62 +65,6 @@ func populateCache(users int, tokens int, authParams authority.AuthParams, clien
}
}
}
func calculateStats(users, tokens int, duration []float64) {

fmt.Printf("No of users: %d, No of tokens per user: %d \n", users, tokens)

mean, err := stats.Mean(duration)
if err != nil {
panic(err)
}
meanTime := mean / float64(time.Microsecond)
fmt.Println("Mean")
fmt.Println(meanTime)

median, err := stats.Median(duration)
medianTime := median / float64(time.Microsecond)
if err != nil {
panic(err)
}
fmt.Println("Median")
fmt.Println(medianTime)

stdDev, err := stats.StandardDeviation(duration)
stdDevTime := stdDev / float64(time.Microsecond)
if err != nil {
panic(err)
}
fmt.Println("Standard Deviation")
fmt.Println(stdDevTime)

min, err := stats.Min(duration)
minTime := min / float64(time.Microsecond)
if err != nil {
panic(err)
}
fmt.Println("Min Time")
fmt.Println(minTime)

max, err := stats.Max(duration)
maxTime := max / float64(time.Microsecond)
if err != nil {
panic(err)
}
fmt.Println("Max Time")
fmt.Println(maxTime)

}

func benchMarkObo(users int, tokens int, client base.Client) {
var duration []float64
for start := time.Now(); time.Since(start) < time.Minute*1; {
s := time.Now()
queryCache(users, tokens, client)
e := time.Now()
duration = append(duration, float64(e.Sub(s)))
}
calculateStats(users, tokens, duration)
}

func queryCache(users int, tokens int, client base.Client) {
userAssertion := fmt.Sprintf("fake_access_token%d", rand.Intn(users))
Expand All @@ -137,28 +79,30 @@ func queryCache(users int, tokens int, client base.Client) {
panic(err)
}
}
func TestOnBehalfOfCacheTests(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Skipping testing in CI environment")
}
tests := []struct {
Users int
Tokens int

func BenchmarkQueryCache(b *testing.B) {
benchmarks := []struct {
users, tokens int
}{
{1, 10000},
{1, 100000},
{10, 10000},
{100, 10000},
{1000, 10000},
{1000, 1000},
{10000, 100},
}

for _, test := range tests {
client, err := fakeClient()
if err != nil {
panic(err)
}
authParams := client.AuthParams
populateCache(test.Users, test.Tokens, authParams, client)
benchMarkObo(test.Users, test.Tokens, client)
for _, bm := range benchmarks {
b.Run(fmt.Sprintf("%d users %d tokens", bm.users, bm.tokens), func(b *testing.B) {
client, err := fakeClient()
if err != nil {
panic(err)
}
authParams := client.AuthParams
populateCache(bm.users, bm.tokens, authParams, client)
b.ResetTimer()
for i := 0; i < b.N; i++ {
queryCache(bm.users, bm.tokens, client)
}
})
}
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/google/uuid v1.3.0
github.com/kylelemons/godebug v1.1.0
github.com/montanaflynn/stats v0.7.0
)

require golang.org/x/sys v0.5.0 // indirect
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/montanaflynn/stats v0.7.0 h1:r3y12KyNxj/Sb/iOE46ws+3mS1+MZca1wlHQFPsY/JU=
github.com/montanaflynn/stats v0.7.0/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 comments on commit fb5a885

Please sign in to comment.