OSDN Git Service

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