File: perf_test.go

package info (click to toggle)
golang-github-azuread-microsoft-authentication-extensions-for-go 0.0~git20231002.7e3b8e2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 260 kB
  • sloc: makefile: 4
file content (98 lines) | stat: -rw-r--r-- 2,585 bytes parent folder | download
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

package cache

import (
	"fmt"
	"path/filepath"
	"sync"
	"testing"

	"github.com/AzureAD/microsoft-authentication-extensions-for-go/cache/accessor/file"
	"github.com/AzureAD/microsoft-authentication-library-for-go/apps/cache"
	"github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
	"github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
	"github.com/stretchr/testify/require"
)

// this file benchmarks MSAL clients using Cache and file.Accessor

func newCache(b *testing.B) *Cache {
	p := filepath.Join(b.TempDir(), b.Name())
	a, err := file.New(p)
	require.NoError(b, err)
	c, err := New(a, p+".timestamp")
	require.NoError(b, err)
	return c
}

func BenchmarkConfidentialClient(b *testing.B) {
	for _, baseline := range []bool{false, true} {
		name := "file accessor"
		if baseline {
			name = "no persistence"
		}
		b.Run(name, func(b *testing.B) {
			var c cache.ExportReplace
			if !baseline {
				c = newCache(b)
			}
			cred, err := confidential.NewCredFromSecret("*")
			require.NoError(b, err)
			client, err := confidential.New(
				"https://login.microsoftonline.com/tenant", "ID", cred, confidential.WithCache(c), confidential.WithHTTPClient(&mockSTS{}),
			)
			require.NoError(b, err)

			gr := 10
			wg := sync.WaitGroup{}
			b.ResetTimer()
			for i := 0; i < b.N; i++ {
				for i := 0; i < gr; i++ {
					wg.Add(1)
					go func(n int) {
						defer wg.Done()
						s := fmt.Sprint(n)
						_, _ = client.AcquireTokenByCredential(ctx, []string{s})
						_, _ = client.AcquireTokenSilent(ctx, []string{s})
					}(i)
				}
				wg.Wait()
			}
		})
	}
}

func BenchmarkPublicClient(b *testing.B) {
	for _, baseline := range []bool{false, true} {
		name := "file accessor"
		if baseline {
			name = "no persistence"
		}
		b.Run(name, func(b *testing.B) {
			var c cache.ExportReplace
			if !baseline {
				c = newCache(b)
			}
			client, err := public.New("clientID", public.WithCache(c), public.WithHTTPClient(&mockSTS{}))
			require.NoError(b, err)

			gr := 10
			wg := sync.WaitGroup{}
			b.ResetTimer()
			for i := 0; i < b.N; i++ {
				for i := 0; i < gr; i++ {
					wg.Add(1)
					go func(n int) {
						defer wg.Done()
						s := fmt.Sprint(n)
						ar, _ := client.AcquireTokenByUsernamePassword(ctx, []string{s}, s, "password")
						_, _ = client.AcquireTokenSilent(ctx, []string{s}, public.WithSilentAccount(ar.Account))
					}(i)
				}
				wg.Wait()
			}
		})
	}
}