github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchutil/bulk_indexer_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 opensearchutil_test 30 31 import ( 32 "bytes" 33 "context" 34 "io/ioutil" 35 "net/http" 36 "strconv" 37 "strings" 38 "testing" 39 40 "github.com/opensearch-project/opensearch-go/v2" 41 "github.com/opensearch-project/opensearch-go/v2/opensearchutil" 42 ) 43 44 var mockResponseBody = `{ 45 "took": 30, 46 "errors": false, 47 "items": [ 48 { 49 "index": { 50 "_index": "test", 51 "_id": "1", 52 "_version": 1, 53 "result": "created", 54 "_shards": { "total": 2, "successful": 1, "failed": 0 }, 55 "status": 201, 56 "_seq_no": 0, 57 "_primary_term": 1 58 } 59 } 60 ] 61 }` 62 63 type mockTransp struct{} 64 65 func (t *mockTransp) RoundTrip(req *http.Request) (*http.Response, error) { 66 return &http.Response{Body: ioutil.NopCloser(strings.NewReader(mockResponseBody))}, nil // 1x alloc 67 } 68 69 func BenchmarkBulkIndexer(b *testing.B) { 70 b.ReportAllocs() 71 72 b.Run("Basic", func(b *testing.B) { 73 b.ResetTimer() 74 75 client, _ := opensearch.NewClient(opensearch.Config{Transport: &mockTransp{}}) 76 bi, _ := opensearchutil.NewBulkIndexer(opensearchutil.BulkIndexerConfig{ 77 Client: client, 78 FlushBytes: 1024, 79 }) 80 defer bi.Close(context.Background()) 81 82 docID := make([]byte, 0, 16) 83 var docIDBuf bytes.Buffer 84 docIDBuf.Grow(cap(docID)) 85 86 for i := 0; i < b.N; i++ { 87 docID = strconv.AppendInt(docID, int64(i), 10) 88 docIDBuf.Write(docID) 89 bi.Add(context.Background(), opensearchutil.BulkIndexerItem{ 90 Action: "index", 91 DocumentID: docIDBuf.String(), // 1x alloc 92 Body: strings.NewReader(`{"foo":"bar"}`), // 1x alloc 93 }) 94 docID = docID[:0] 95 docIDBuf.Reset() 96 } 97 }) 98 }