Files
go-github/github/github_benchmark_test.go
Steve Hipwell 69bd00175e feat!: Add token source for generic auth logic (#4637)
BREAKING CHANGE: `BasicAuthTransport`, `UnauthenticatedRateLimitedTransport` & `WithHTTPClient` have been removed as they do not fit with the required behaviour to only authenticate requests for specific endpoints. `CheckRedirect` and `Jar` are no longer supported.
2026-10-22 12:02:08 -04:00

59 lines
1.5 KiB
Go

// Copyright 2026 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
)
// fixedResponseTransport serves a canned JSON payload without touching the
// network so that BenchmarkDo measures the real request/decode path alone.
type fixedResponseTransport struct {
payload []byte
}
func (t *fixedResponseTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
Body: io.NopCloser(bytes.NewReader(t.payload)),
Request: req,
}, nil
}
func BenchmarkDo(b *testing.B) {
for _, sizeKB := range []int{1, 500} {
b.Run(fmt.Sprintf("%vKB", sizeKB), func(b *testing.B) {
payload, err := json.Marshal(map[string]string{"body": strings.Repeat("a", sizeKB*1024)})
if err != nil {
b.Fatalf("json.Marshal returned error: %v", err)
}
client, err := NewClient(WithTransport(&fixedResponseTransport{payload: payload}))
if err != nil {
b.Fatalf("NewClient returned error: %v", err)
}
req, err := client.NewRequest(b.Context(), "GET", ".", nil)
if err != nil {
b.Fatalf("NewRequest returned error: %v", err)
}
b.ReportAllocs()
for b.Loop() {
var v map[string]string
if _, err := client.Do(req, &v); err != nil {
b.Fatalf("Do returned error: %v", err)
}
}
})
}
}