keys_bench_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. package client
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "reflect"
  19. "strings"
  20. "testing"
  21. )
  22. func createTestNode(size int) *Node {
  23. return &Node{
  24. Key: strings.Repeat("a", 30),
  25. Value: strings.Repeat("a", size),
  26. CreatedIndex: 123456,
  27. ModifiedIndex: 123456,
  28. TTL: 123456789,
  29. }
  30. }
  31. func createTestNodeWithChildren(children, size int) *Node {
  32. node := createTestNode(size)
  33. for i := 0; i < children; i++ {
  34. node.Nodes = append(node.Nodes, createTestNode(size))
  35. }
  36. return node
  37. }
  38. func createTestResponse(children, size int) *Response {
  39. return &Response{
  40. Action: "aaaaa",
  41. Node: createTestNodeWithChildren(children, size),
  42. PrevNode: nil,
  43. }
  44. }
  45. func benchmarkResponseUnmarshalling(b *testing.B, children, size int) {
  46. header := http.Header{}
  47. header.Add("X-Etcd-Index", "123456")
  48. response := createTestResponse(children, size)
  49. body, err := json.Marshal(response)
  50. if err != nil {
  51. b.Fatal(err)
  52. }
  53. b.ResetTimer()
  54. newResponse := new(Response)
  55. for i := 0; i < b.N; i++ {
  56. if newResponse, err = unmarshalSuccessfulKeysResponse(header, body); err != nil {
  57. b.Errorf("error unmarshalling response (%v)", err)
  58. }
  59. }
  60. if !reflect.DeepEqual(response.Node, newResponse.Node) {
  61. b.Errorf("Unexpected difference in a parsed response: \n%+v\n%+v", response, newResponse)
  62. }
  63. }
  64. func BenchmarkSmallResponseUnmarshal(b *testing.B) {
  65. benchmarkResponseUnmarshalling(b, 30, 20)
  66. }
  67. func BenchmarkManySmallResponseUnmarshal(b *testing.B) {
  68. benchmarkResponseUnmarshalling(b, 3000, 20)
  69. }
  70. func BenchmarkMediumResponseUnmarshal(b *testing.B) {
  71. benchmarkResponseUnmarshalling(b, 300, 200)
  72. }
  73. func BenchmarkLargeResponseUnmarshal(b *testing.B) {
  74. benchmarkResponseUnmarshalling(b, 3000, 2000)
  75. }