|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//go:build !integration |
| 19 | +// +build !integration |
| 20 | + |
| 21 | +package elastictransport |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "io" |
| 26 | + "net/http" |
| 27 | + "net/url" |
| 28 | + "strings" |
| 29 | + "sync" |
| 30 | + "testing" |
| 31 | +) |
| 32 | + |
| 33 | +// benchTransport returns a minimal response without hitting the network. |
| 34 | +// Each call returns a fresh *http.Response so parallel goroutines don't share |
| 35 | +// mutable state. |
| 36 | +type benchTransport struct{} |
| 37 | + |
| 38 | +func (t *benchTransport) RoundTrip(*http.Request) (*http.Response, error) { |
| 39 | + return &http.Response{ |
| 40 | + StatusCode: 200, |
| 41 | + Body: io.NopCloser(strings.NewReader("")), |
| 42 | + ContentLength: 0, |
| 43 | + Header: http.Header{}, |
| 44 | + }, nil |
| 45 | +} |
| 46 | + |
| 47 | +// benchmarkPerform exercises Client.Perform under varying parallelism factors. |
| 48 | +// The sub-benchmarks vary the value passed to b.SetParallelism, which scales |
| 49 | +// parallel work relative to GOMAXPROCS rather than creating an exact number |
| 50 | +// of goroutines. |
| 51 | +func benchmarkPerform(b *testing.B, name string, client *Client) { |
| 52 | + b.Helper() |
| 53 | + b.Run(name, func(b *testing.B) { |
| 54 | + for _, p := range []int{1, 10, 100, 1000} { |
| 55 | + b.Run(fmt.Sprintf("parallelism-%d", p), func(b *testing.B) { |
| 56 | + b.SetParallelism(p) |
| 57 | + b.RunParallel(func(pb *testing.PB) { |
| 58 | + for pb.Next() { |
| 59 | + req, _ := http.NewRequest("GET", "/", nil) |
| 60 | + res, err := client.Perform(req) |
| 61 | + if err != nil { |
| 62 | + b.Fatalf("Perform: %s", err) |
| 63 | + } |
| 64 | + if err := res.Body.Close(); err != nil { |
| 65 | + b.Fatalf("Close response body: %s", err) |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
| 69 | + }) |
| 70 | + } |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func BenchmarkClientPoolAccess(b *testing.B) { |
| 75 | + b.ReportAllocs() |
| 76 | + |
| 77 | + b.Run("SingleConnectionPool", func(b *testing.B) { |
| 78 | + u, _ := url.Parse("http://localhost:9200") |
| 79 | + tp, err := New(Config{ |
| 80 | + URLs: []*url.URL{u}, |
| 81 | + Transport: &benchTransport{}, |
| 82 | + DisableRetry: true, |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + b.Fatal(err) |
| 86 | + } |
| 87 | + benchmarkPerform(b, "Perform", tp) |
| 88 | + }) |
| 89 | + |
| 90 | + b.Run("StatusConnectionPool", func(b *testing.B) { |
| 91 | + var urls []*url.URL |
| 92 | + for i := 0; i < 10; i++ { |
| 93 | + u, _ := url.Parse(fmt.Sprintf("http://node%d:9200", i)) |
| 94 | + urls = append(urls, u) |
| 95 | + } |
| 96 | + tp, err := New(Config{ |
| 97 | + URLs: urls, |
| 98 | + Transport: &benchTransport{}, |
| 99 | + DisableRetry: true, |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + b.Fatal(err) |
| 103 | + } |
| 104 | + benchmarkPerform(b, "Perform", tp) |
| 105 | + }) |
| 106 | + |
| 107 | + b.Run("CustomPool/SynchronizedWrapper", func(b *testing.B) { |
| 108 | + var urls []*url.URL |
| 109 | + for i := 0; i < 10; i++ { |
| 110 | + u, _ := url.Parse(fmt.Sprintf("http://node%d:9200", i)) |
| 111 | + urls = append(urls, u) |
| 112 | + } |
| 113 | + tp, err := New(Config{ |
| 114 | + URLs: urls, |
| 115 | + ConnectionPoolFunc: func(conns []*Connection, sel Selector) ConnectionPool { |
| 116 | + return &benchCustomPool{conns: conns} |
| 117 | + }, |
| 118 | + Transport: &benchTransport{}, |
| 119 | + DisableRetry: true, |
| 120 | + }) |
| 121 | + if err != nil { |
| 122 | + b.Fatal(err) |
| 123 | + } |
| 124 | + benchmarkPerform(b, "Perform", tp) |
| 125 | + }) |
| 126 | + |
| 127 | + b.Run("CustomPool/ConcurrentSafeOptIn", func(b *testing.B) { |
| 128 | + var urls []*url.URL |
| 129 | + for i := 0; i < 10; i++ { |
| 130 | + u, _ := url.Parse(fmt.Sprintf("http://node%d:9200", i)) |
| 131 | + urls = append(urls, u) |
| 132 | + } |
| 133 | + tp, err := New(Config{ |
| 134 | + URLs: urls, |
| 135 | + ConnectionPoolFunc: func(conns []*Connection, sel Selector) ConnectionPool { |
| 136 | + return &benchConcurrentSafeCustomPool{ |
| 137 | + benchCustomPool: benchCustomPool{conns: conns}, |
| 138 | + } |
| 139 | + }, |
| 140 | + Transport: &benchTransport{}, |
| 141 | + DisableRetry: true, |
| 142 | + }) |
| 143 | + if err != nil { |
| 144 | + b.Fatal(err) |
| 145 | + } |
| 146 | + benchmarkPerform(b, "Perform", tp) |
| 147 | + }) |
| 148 | +} |
| 149 | + |
| 150 | +type benchCustomPool struct { |
| 151 | + mu sync.Mutex |
| 152 | + conns []*Connection |
| 153 | + curr int |
| 154 | +} |
| 155 | + |
| 156 | +func (p *benchCustomPool) Next() (*Connection, error) { |
| 157 | + p.mu.Lock() |
| 158 | + defer p.mu.Unlock() |
| 159 | + c := p.conns[p.curr%len(p.conns)] |
| 160 | + p.curr++ |
| 161 | + return c, nil |
| 162 | +} |
| 163 | + |
| 164 | +func (p *benchCustomPool) OnSuccess(*Connection) error { return nil } |
| 165 | +func (p *benchCustomPool) OnFailure(*Connection) error { return nil } |
| 166 | +func (p *benchCustomPool) URLs() []*url.URL { |
| 167 | + p.mu.Lock() |
| 168 | + defer p.mu.Unlock() |
| 169 | + out := make([]*url.URL, len(p.conns)) |
| 170 | + for i, c := range p.conns { |
| 171 | + out[i] = c.URL |
| 172 | + } |
| 173 | + return out |
| 174 | +} |
| 175 | + |
| 176 | +type benchConcurrentSafeCustomPool struct { |
| 177 | + benchCustomPool |
| 178 | +} |
| 179 | + |
| 180 | +func (p *benchConcurrentSafeCustomPool) ConcurrentSafe() {} |
0 commit comments