github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchtransport/logger_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 "bytes" 33 "io/ioutil" 34 "net/http" 35 "net/url" 36 "testing" 37 38 "github.com/opensearch-project/opensearch-go/v2/opensearchtransport" 39 ) 40 41 func BenchmarkTransportLogger(b *testing.B) { 42 b.ReportAllocs() 43 44 b.Run("Text", func(b *testing.B) { 45 for i := 0; i < b.N; i++ { 46 tp, _ := opensearchtransport.New(opensearchtransport.Config{ 47 URLs: []*url.URL{{Scheme: "http", Host: "foo"}}, 48 Transport: newFakeTransport(b), 49 Logger: &opensearchtransport.TextLogger{Output: ioutil.Discard}, 50 }) 51 52 req, _ := http.NewRequest("GET", "/abc", nil) 53 _, err := tp.Perform(req) 54 if err != nil { 55 b.Fatalf("Unexpected error: %s", err) 56 } 57 } 58 }) 59 60 b.Run("Text-Body", func(b *testing.B) { 61 for i := 0; i < b.N; i++ { 62 tp, _ := opensearchtransport.New(opensearchtransport.Config{ 63 URLs: []*url.URL{{Scheme: "http", Host: "foo"}}, 64 Transport: newFakeTransport(b), 65 Logger: &opensearchtransport.TextLogger{Output: ioutil.Discard, EnableRequestBody: true, EnableResponseBody: true}, 66 }) 67 68 req, _ := http.NewRequest("GET", "/abc", nil) 69 res, err := tp.Perform(req) 70 if err != nil { 71 b.Fatalf("Unexpected error: %s", err) 72 } 73 74 body, err := ioutil.ReadAll(res.Body) 75 if err != nil { 76 b.Fatalf("Error reading response body: %s", err) 77 } 78 res.Body = ioutil.NopCloser(bytes.NewBuffer(body)) 79 if len(body) < 13 { 80 b.Errorf("Error reading response body bytes, want=13, got=%d", len(body)) 81 } 82 } 83 }) 84 85 b.Run("JSON", func(b *testing.B) { 86 for i := 0; i < b.N; i++ { 87 tp, _ := opensearchtransport.New(opensearchtransport.Config{ 88 URLs: []*url.URL{{Scheme: "http", Host: "foo"}}, 89 Transport: newFakeTransport(b), 90 Logger: &opensearchtransport.JSONLogger{Output: ioutil.Discard}, 91 }) 92 93 req, _ := http.NewRequest("GET", "/abc", nil) 94 _, err := tp.Perform(req) 95 if err != nil { 96 b.Fatalf("Unexpected error: %s", err) 97 } 98 } 99 }) 100 101 b.Run("JSON-Body", func(b *testing.B) { 102 for i := 0; i < b.N; i++ { 103 tp, _ := opensearchtransport.New(opensearchtransport.Config{ 104 URLs: []*url.URL{{Scheme: "http", Host: "foo"}}, 105 Transport: newFakeTransport(b), 106 Logger: &opensearchtransport.JSONLogger{Output: ioutil.Discard, EnableRequestBody: true, EnableResponseBody: true}, 107 }) 108 109 req, _ := http.NewRequest("GET", "/abc", nil) 110 _, err := tp.Perform(req) 111 if err != nil { 112 b.Fatalf("Unexpected error: %s", err) 113 } 114 } 115 }) 116 }