|
| 1 | +// Copyright 2026 The go-github AUTHORS. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +package github |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "encoding/json" |
| 11 | + "io" |
| 12 | + "net/http" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | +) |
| 16 | + |
| 17 | +// legacyDecodeResponse simulates the behavior before Symmetrical Pooling |
| 18 | +// (io.ReadAll -> json.Unmarshal). |
| 19 | +func legacyDecodeResponse(resp *http.Response, v any) error { |
| 20 | + data, err := io.ReadAll(resp.Body) |
| 21 | + if err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + if len(data) > 0 { |
| 25 | + return json.Unmarshal(data, v) |
| 26 | + } |
| 27 | + return nil |
| 28 | +} |
| 29 | + |
| 30 | +// pooledDecodeResponse simulates the new behavior with Symmetrical Pooling |
| 31 | +// (requestBufferPool -> ReadFrom -> json.Unmarshal). |
| 32 | +func pooledDecodeResponse(resp *http.Response, v any) error { |
| 33 | + respBuf := requestBufferPool.Get().(*bytes.Buffer) |
| 34 | + defer func() { |
| 35 | + respBuf.Reset() |
| 36 | + requestBufferPool.Put(respBuf) |
| 37 | + }() |
| 38 | + |
| 39 | + _, err := respBuf.ReadFrom(resp.Body) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + if respBuf.Len() > 0 { |
| 44 | + b := respBuf.Bytes() |
| 45 | + return json.Unmarshal(b, v) |
| 46 | + } |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +type dummyReadCloser struct { |
| 51 | + io.Reader |
| 52 | +} |
| 53 | + |
| 54 | +func (d *dummyReadCloser) Close() error { return nil } |
| 55 | + |
| 56 | +func BenchmarkDecodeResponse_Legacy(b *testing.B) { |
| 57 | + payload, _ := json.Marshal(map[string]string{"title": "benchmark_test", "body": strings.Repeat("a", 1024*500)}) // 500KB JSON |
| 58 | + |
| 59 | + b.ReportAllocs() |
| 60 | + b.ResetTimer() |
| 61 | + for i := 0; i < b.N; i++ { |
| 62 | + b.StopTimer() |
| 63 | + resp := &http.Response{ |
| 64 | + Body: &dummyReadCloser{Reader: bytes.NewReader(payload)}, |
| 65 | + } |
| 66 | + var v map[string]string |
| 67 | + b.StartTimer() |
| 68 | + |
| 69 | + _ = legacyDecodeResponse(resp, &v) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func BenchmarkDecodeResponse_Pooled(b *testing.B) { |
| 74 | + payload, _ := json.Marshal(map[string]string{"title": "benchmark_test", "body": strings.Repeat("a", 1024*500)}) // 500KB JSON |
| 75 | + |
| 76 | + b.ReportAllocs() |
| 77 | + b.ResetTimer() |
| 78 | + for i := 0; i < b.N; i++ { |
| 79 | + b.StopTimer() |
| 80 | + resp := &http.Response{ |
| 81 | + Body: &dummyReadCloser{Reader: bytes.NewReader(payload)}, |
| 82 | + } |
| 83 | + var v map[string]string |
| 84 | + b.StartTimer() |
| 85 | + |
| 86 | + _ = pooledDecodeResponse(resp, &v) |
| 87 | + } |
| 88 | +} |
0 commit comments