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 Jun 26, 2023
1 parent 5d7178b commit 0f2e760
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 81 deletions.
111 changes: 33 additions & 78 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,98 +65,55 @@ 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)
func queryCache(users int, tokens int, client base.Client) {
userAssertion := fmt.Sprintf("fake_access_token%d", rand.Intn(users))
scope := []string{fmt.Sprintf("scope%d", rand.Intn(tokens))}
params := base.AcquireTokenOnBehalfOfParameters{
Scopes: scope,
UserAssertion: userAssertion,
Credential: &accesstokens.Credential{Secret: "fake_secret"},
}
meanTime := mean / float64(time.Microsecond)
fmt.Println("Mean")
fmt.Println(meanTime)

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

stdDev, err := stats.StandardDeviation(duration)
stdDevTime := stdDev / float64(time.Microsecond)
func benchmark(b *testing.B, users int, tokens int) {
client, err := fakeClient()
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)
authParams := client.AuthParams
populateCache(users, tokens, authParams, client)
b.ResetTimer()
for i := 0; i < b.N; i++ {
queryCache(users, tokens, client)
}
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 BenchmarkQueryCache1User10000Tokens(b *testing.B) {
benchmark(b, 1, 10000)
}

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 BenchmarkQueryCache1User100000Tokens(b *testing.B) {
benchmark(b, 1, 100000)
}

func queryCache(users int, tokens int, client base.Client) {
userAssertion := fmt.Sprintf("fake_access_token%d", rand.Intn(users))
scope := []string{fmt.Sprintf("scope%d", rand.Intn(tokens))}
params := base.AcquireTokenOnBehalfOfParameters{
Scopes: scope,
UserAssertion: userAssertion,
Credential: &accesstokens.Credential{Secret: "fake_secret"},
}
_, err := client.AcquireTokenOnBehalfOf(context.Background(), params)
if err != nil {
panic(err)
}
func BenchmarkQueryCache10Users10000Tokens(b *testing.B) {
benchmark(b, 10, 10000)
}
func TestOnBehalfOfCacheTests(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Skipping testing in CI environment")
}
tests := []struct {
Users int
Tokens int
}{
{1, 10000},
{1, 100000},
{100, 10000},
{1000, 10000},
{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)
}
func BenchmarkQueryCache100Users10000Tokens(b *testing.B) {
benchmark(b, 100, 10000)
}

func BenchmarkQueryCache1000Users1000Tokens(b *testing.B) {
benchmark(b, 1000, 1000)
}

func BenchmarkQueryCache10000Users100Tokens(b *testing.B) {
benchmark(b, 10000, 100)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/golang-jwt/jwt/v4 v4.4.3
github.com/google/uuid v1.3.0
github.com/kylelemons/godebug v1.1.0
github.com/montanaflynn/stats v0.7.0
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,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=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit 0f2e760

Please sign in to comment.