Debian Codegraph @

internal/utils/bench_test.go

raw
1 package utils_test
2
3 import (
4 "net/http"
5 "testing"
6
7 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/utils"
8 )
9
10 type benchmarkRoundTripper struct{}
11
12 func (benchmarkRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
13 return &http.Response{
14 StatusCode: http.StatusOK,
15 Body: http.NoBody,
16 Header: make(http.Header),
17 Request: req,
18 }, nil
19 }
20
21 func BenchmarkUserAgentTransportRoundTrip(b *testing.B) {
22 transport := utils.NewUserAgentTransport(benchmarkRoundTripper{})
23
24 req, err := http.NewRequestWithContext(b.Context(), http.MethodGet, "http://localhost", nil)
25 if err != nil {
26 b.Fatal(err)
27 }
28
29 b.ReportAllocs()
30 b.ResetTimer()
31
32 for b.Loop() {
33 req.Header = make(http.Header)
34
35 resp, err := transport.RoundTrip(req)
36 if err != nil {
37 b.Fatal(err)
38 }
39
40 if err = resp.Body.Close(); err != nil {
41 b.Fatal(err)
42 }
43 }
44 }
45
46 func BenchmarkLookupGroupNumeric(b *testing.B) {
47 var gid int
48
49 b.ReportAllocs()
50 b.ResetTimer()
51
52 for b.Loop() {
53 parsedGID, err := utils.LookupGroup("1001")
54 if err != nil {
55 b.Fatal(err)
56 }
57
58 gid = parsedGID
59 }
60
61 _ = gid
62 }