go.etcd.io/etcd@v3.3.27+incompatible/client/keys_bench_test.go (about)

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package client
    16  
    17  import (
    18  	"encoding/json"
    19  	"net/http"
    20  	"reflect"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  func createTestNode(size int) *Node {
    26  	return &Node{
    27  		Key:           strings.Repeat("a", 30),
    28  		Value:         strings.Repeat("a", size),
    29  		CreatedIndex:  123456,
    30  		ModifiedIndex: 123456,
    31  		TTL:           123456789,
    32  	}
    33  }
    34  
    35  func createTestNodeWithChildren(children, size int) *Node {
    36  	node := createTestNode(size)
    37  	for i := 0; i < children; i++ {
    38  		node.Nodes = append(node.Nodes, createTestNode(size))
    39  	}
    40  	return node
    41  }
    42  
    43  func createTestResponse(children, size int) *Response {
    44  	return &Response{
    45  		Action:   "aaaaa",
    46  		Node:     createTestNodeWithChildren(children, size),
    47  		PrevNode: nil,
    48  	}
    49  }
    50  
    51  func benchmarkResponseUnmarshalling(b *testing.B, children, size int) {
    52  	header := http.Header{}
    53  	header.Add("X-Etcd-Index", "123456")
    54  	response := createTestResponse(children, size)
    55  	body, err := json.Marshal(response)
    56  	if err != nil {
    57  		b.Fatal(err)
    58  	}
    59  
    60  	b.ResetTimer()
    61  	newResponse := new(Response)
    62  	for i := 0; i < b.N; i++ {
    63  		if newResponse, err = unmarshalSuccessfulKeysResponse(header, body); err != nil {
    64  			b.Errorf("error unmarshaling response (%v)", err)
    65  		}
    66  
    67  	}
    68  	if !reflect.DeepEqual(response.Node, newResponse.Node) {
    69  		b.Errorf("Unexpected difference in a parsed response: \n%+v\n%+v", response, newResponse)
    70  	}
    71  }
    72  
    73  func BenchmarkSmallResponseUnmarshal(b *testing.B) {
    74  	benchmarkResponseUnmarshalling(b, 30, 20)
    75  }
    76  
    77  func BenchmarkManySmallResponseUnmarshal(b *testing.B) {
    78  	benchmarkResponseUnmarshalling(b, 3000, 20)
    79  }
    80  
    81  func BenchmarkMediumResponseUnmarshal(b *testing.B) {
    82  	benchmarkResponseUnmarshalling(b, 300, 200)
    83  }
    84  
    85  func BenchmarkLargeResponseUnmarshal(b *testing.B) {
    86  	benchmarkResponseUnmarshalling(b, 3000, 2000)
    87  }