benchmark_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package benchmark
  19. import (
  20. "fmt"
  21. "os"
  22. "reflect"
  23. "testing"
  24. "time"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/benchmark/stats"
  27. )
  28. func BenchmarkClient(b *testing.B) {
  29. enableTrace := []bool{true, false} // run both enable and disable by default
  30. // When set the latency to 0 (no delay), the result is slower than the real result with no delay
  31. // because latency simulation section has extra operations
  32. latency := []time.Duration{0, 40 * time.Millisecond} // if non-positive, no delay.
  33. kbps := []int{0, 10240} // if non-positive, infinite
  34. mtu := []int{0} // if non-positive, infinite
  35. maxConcurrentCalls := []int{1, 8, 64, 512}
  36. reqSizeBytes := []int{1, 1024 * 1024}
  37. respSizeBytes := []int{1, 1024 * 1024}
  38. featuresCurPos := make([]int, 7)
  39. // 0:enableTracing 1:md 2:ltc 3:kbps 4:mtu 5:maxC 6:connCount 7:reqSize 8:respSize
  40. featuresMaxPosition := []int{len(enableTrace), len(latency), len(kbps), len(mtu), len(maxConcurrentCalls), len(reqSizeBytes), len(respSizeBytes)}
  41. initalPos := make([]int, len(featuresCurPos))
  42. // run benchmarks
  43. start := true
  44. for !reflect.DeepEqual(featuresCurPos, initalPos) || start {
  45. start = false
  46. tracing := "Trace"
  47. if !enableTrace[featuresCurPos[0]] {
  48. tracing = "noTrace"
  49. }
  50. benchFeature := stats.Features{
  51. EnableTrace: enableTrace[featuresCurPos[0]],
  52. Latency: latency[featuresCurPos[1]],
  53. Kbps: kbps[featuresCurPos[2]],
  54. Mtu: mtu[featuresCurPos[3]],
  55. MaxConcurrentCalls: maxConcurrentCalls[featuresCurPos[4]],
  56. ReqSizeBytes: reqSizeBytes[featuresCurPos[5]],
  57. RespSizeBytes: respSizeBytes[featuresCurPos[6]],
  58. }
  59. grpc.EnableTracing = enableTrace[featuresCurPos[0]]
  60. b.Run(fmt.Sprintf("Unary-%s-%s",
  61. tracing, benchFeature.String()), func(b *testing.B) {
  62. runUnary(b, benchFeature)
  63. })
  64. b.Run(fmt.Sprintf("Stream-%s-%s",
  65. tracing, benchFeature.String()), func(b *testing.B) {
  66. runStream(b, benchFeature)
  67. })
  68. AddOne(featuresCurPos, featuresMaxPosition)
  69. }
  70. }
  71. func TestMain(m *testing.M) {
  72. os.Exit(stats.RunTestMain(m))
  73. }