github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchtransport/opensearchtransport_benchmark_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  // Licensed to Elasticsearch B.V. under one or more contributor
    11  // license agreements. See the NOTICE file distributed with
    12  // this work for additional information regarding copyright
    13  // ownership. Elasticsearch B.V. licenses this file to you under
    14  // the Apache License, Version 2.0 (the "License"); you may
    15  // not use this file except in compliance with the License.
    16  // You may obtain a copy of the License at
    17  //
    18  //    http://www.apache.org/licenses/LICENSE-2.0
    19  //
    20  // Unless required by applicable law or agreed to in writing,
    21  // software distributed under the License is distributed on an
    22  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    23  // KIND, either express or implied.  See the License for the
    24  // specific language governing permissions and limitations
    25  // under the License.
    26  
    27  // +build !integration
    28  
    29  package opensearchtransport_test
    30  
    31  import (
    32  	"io/ioutil"
    33  	"net/http"
    34  	"net/url"
    35  	"strings"
    36  	"testing"
    37  
    38  	"github.com/opensearch-project/opensearch-go/v2/opensearchtransport"
    39  )
    40  
    41  var defaultResponse = http.Response{
    42  	Status:        "200 OK",
    43  	StatusCode:    200,
    44  	ContentLength: 13,
    45  	Header:        http.Header(map[string][]string{"Content-Type": {"application/json"}}),
    46  	Body:          ioutil.NopCloser(strings.NewReader(`{"foo":"bar"}`)),
    47  }
    48  
    49  type FakeTransport struct {
    50  	FakeResponse *http.Response
    51  }
    52  
    53  func (t *FakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    54  	return t.FakeResponse, nil
    55  }
    56  
    57  func newFakeTransport(b *testing.B) *FakeTransport {
    58  	return &FakeTransport{FakeResponse: &defaultResponse}
    59  }
    60  
    61  func BenchmarkTransport(b *testing.B) {
    62  	b.ReportAllocs()
    63  
    64  	b.Run("Defaults", func(b *testing.B) {
    65  		for i := 0; i < b.N; i++ {
    66  			tp, _ := opensearchtransport.New(opensearchtransport.Config{
    67  				URLs:      []*url.URL{{Scheme: "http", Host: "foo"}},
    68  				Transport: newFakeTransport(b),
    69  			})
    70  
    71  			req, _ := http.NewRequest("GET", "/abc", nil)
    72  			_, err := tp.Perform(req)
    73  			if err != nil {
    74  				b.Fatalf("Unexpected error: %s", err)
    75  			}
    76  		}
    77  	})
    78  
    79  	b.Run("Headers", func(b *testing.B) {
    80  		hdr := http.Header{}
    81  		hdr.Set("Accept", "application/yaml")
    82  
    83  		for i := 0; i < b.N; i++ {
    84  			tp, _ := opensearchtransport.New(opensearchtransport.Config{
    85  				URLs:      []*url.URL{{Scheme: "http", Host: "foo"}},
    86  				Header:    hdr,
    87  				Transport: newFakeTransport(b),
    88  			})
    89  
    90  			req, _ := http.NewRequest("GET", "/abc", nil)
    91  			_, err := tp.Perform(req)
    92  			if err != nil {
    93  				b.Fatalf("Unexpected error: %s", err)
    94  			}
    95  		}
    96  	})
    97  }