|
| 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 interface{}) 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 interface{}) 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 | +} |
| 89 | + |
| 90 | +// legacyEncodeRequest simulates the behavior before Symmetrical Pooling |
| 91 | +// (json.Encoder straight to new bytes.Buffer) |
| 92 | +func legacyEncodeRequest(v interface{}) (*bytes.Buffer, error) { |
| 93 | + buf := &bytes.Buffer{} |
| 94 | + err := json.NewEncoder(buf).Encode(v) |
| 95 | + return buf, err |
| 96 | +} |
| 97 | + |
| 98 | +// pooledEncodeRequest simulates the new behavior |
| 99 | +// (requestBufferPool -> json.Encoder -> make slice) |
| 100 | +func pooledEncodeRequest(v interface{}) ([]byte, error) { |
| 101 | + buf := requestBufferPool.Get().(*bytes.Buffer) |
| 102 | + defer func() { |
| 103 | + buf.Reset() |
| 104 | + requestBufferPool.Put(buf) |
| 105 | + }() |
| 106 | + err := json.NewEncoder(buf).Encode(v) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + b := make([]byte, buf.Len()) |
| 111 | + copy(b, buf.Bytes()) |
| 112 | + return b, nil |
| 113 | +} |
| 114 | + |
| 115 | +func BenchmarkEncodeRequest_Legacy(b *testing.B) { |
| 116 | + payload := map[string]string{"title": "benchmark_test", "body": strings.Repeat("a", 1024*500)} // 500KB string |
| 117 | + |
| 118 | + b.ReportAllocs() |
| 119 | + b.ResetTimer() |
| 120 | + for i := 0; i < b.N; i++ { |
| 121 | + buf, _ := legacyEncodeRequest(payload) |
| 122 | + _ = buf.Bytes() |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func BenchmarkEncodeRequest_Pooled(b *testing.B) { |
| 127 | + payload := map[string]string{"title": "benchmark_test", "body": strings.Repeat("a", 1024*500)} // 500KB string |
| 128 | + |
| 129 | + b.ReportAllocs() |
| 130 | + b.ResetTimer() |
| 131 | + for i := 0; i < b.N; i++ { |
| 132 | + bSlice, _ := pooledEncodeRequest(payload) |
| 133 | + _ = bSlice |
| 134 | + } |
| 135 | +} |
0 commit comments