File: performance_test.go

package info (click to toggle)
golang-github-azuread-microsoft-authentication-library-for-go 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: makefile: 4
file content (164 lines) | stat: -rw-r--r-- 4,640 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package performance

import (
	"context"
	"fmt"
	"math/rand"
	"os"
	"testing"
	"time"

	"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/base"
	internalTime "github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/json/types/time"
	"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth"
	"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) {
	// we use a base.Client so we can provide a fake OAuth client
	return base.New("fake_client_id", "https://fake_authority/my_utid", &oauth.Client{
		Authority: &fake.Authority{
			InstanceResp: authority.InstanceDiscoveryResponse{
				Metadata: []authority.InstanceDiscoveryMetadata{
					{
						PreferredNetwork: "fake_authority",
						Aliases:          []string{"fake_authority"},
					},
				},
			},
		},
		Resolver: &fake.ResolveEndpoints{
			Endpoints: authority.Endpoints{
				AuthorizationEndpoint: "auth_endpoint",
				TokenEndpoint:         "token_endpoint",
			},
		},
		WSTrust: &fake.WSTrust{},
	})
}

func populateCache(users int, tokens int, authParams authority.AuthParams, client base.Client) {
	for user := 0; user < users; user++ {
		for token := 0; token < tokens; token++ {
			authParams := client.AuthParams
			authParams.UserAssertion = fmt.Sprintf("fake_access_token%d", user)
			authParams.AuthorizationType = authority.ATOnBehalfOf
			scope := fmt.Sprintf("scope%d", token)

			_, err := client.AuthResultFromToken(context.Background(), authParams, accesstokens.TokenResponse{
				AccessToken:   fmt.Sprintf("fake_access_token%d", user),
				RefreshToken:  "fake_refresh_token",
				ClientInfo:    accesstokens.ClientInfo{UID: "my_uid", UTID: fmt.Sprintf("%dmy_utid", user)},
				ExpiresOn:     internalTime.DurationTime{T: time.Now().Add(1 * time.Hour)},
				GrantedScopes: accesstokens.Scopes{Slice: []string{scope}},
				IDToken: accesstokens.IDToken{
					RawToken: "x.e30",
				},
			}, true)
			if err != nil {
				panic(err)
			}
		}
	}
}
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))
	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 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)
	}
}