benchmark_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package bson
  7. import (
  8. "testing"
  9. )
  10. type encodetest struct {
  11. Field1String string
  12. Field1Int64 int64
  13. Field1Float64 float64
  14. Field2String string
  15. Field2Int64 int64
  16. Field2Float64 float64
  17. Field3String string
  18. Field3Int64 int64
  19. Field3Float64 float64
  20. Field4String string
  21. Field4Int64 int64
  22. Field4Float64 float64
  23. }
  24. type nestedtest1 struct {
  25. Nested nestedtest2
  26. }
  27. type nestedtest2 struct {
  28. Nested nestedtest3
  29. }
  30. type nestedtest3 struct {
  31. Nested nestedtest4
  32. }
  33. type nestedtest4 struct {
  34. Nested nestedtest5
  35. }
  36. type nestedtest5 struct {
  37. Nested nestedtest6
  38. }
  39. type nestedtest6 struct {
  40. Nested nestedtest7
  41. }
  42. type nestedtest7 struct {
  43. Nested nestedtest8
  44. }
  45. type nestedtest8 struct {
  46. Nested nestedtest9
  47. }
  48. type nestedtest9 struct {
  49. Nested nestedtest10
  50. }
  51. type nestedtest10 struct {
  52. Nested nestedtest11
  53. }
  54. type nestedtest11 struct {
  55. Nested encodetest
  56. }
  57. var encodetestInstance = encodetest{
  58. Field1String: "foo",
  59. Field1Int64: 1,
  60. Field1Float64: 3.0,
  61. Field2String: "bar",
  62. Field2Int64: 2,
  63. Field2Float64: 3.1,
  64. Field3String: "baz",
  65. Field3Int64: 3,
  66. Field3Float64: 3.14,
  67. Field4String: "qux",
  68. Field4Int64: 4,
  69. Field4Float64: 3.141,
  70. }
  71. var nestedInstance = nestedtest1{
  72. nestedtest2{
  73. nestedtest3{
  74. nestedtest4{
  75. nestedtest5{
  76. nestedtest6{
  77. nestedtest7{
  78. nestedtest8{
  79. nestedtest9{
  80. nestedtest10{
  81. nestedtest11{
  82. encodetest{
  83. Field1String: "foo",
  84. Field1Int64: 1,
  85. Field1Float64: 3.0,
  86. Field2String: "bar",
  87. Field2Int64: 2,
  88. Field2Float64: 3.1,
  89. Field3String: "baz",
  90. Field3Int64: 3,
  91. Field3Float64: 3.14,
  92. Field4String: "qux",
  93. Field4Int64: 4,
  94. Field4Float64: 3.141,
  95. },
  96. },
  97. },
  98. },
  99. },
  100. },
  101. },
  102. },
  103. },
  104. },
  105. },
  106. }
  107. func BenchmarkEncoding(b *testing.B) {
  108. for i := 0; i < b.N; i++ {
  109. _, _ = Marshal(encodetestInstance)
  110. }
  111. }
  112. func BenchmarkEncodingNested(b *testing.B) {
  113. for i := 0; i < b.N; i++ {
  114. _, _ = Marshal(nestedInstance)
  115. }
  116. }