• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.benchmarks.driver;
18 
19 import static junit.framework.TestCase.assertEquals;
20 import static junit.framework.TestCase.assertTrue;
21 
22 import io.grpc.benchmarks.proto.Control;
23 import io.grpc.benchmarks.proto.Stats;
24 import org.junit.After;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 /**
30  * Tests for {@link LoadClient}.
31  */
32 @RunWith(JUnit4.class)
33 public class LoadClientTest {
34   private LoadClient loadClient;
35 
36   @After
tearDown()37   public void tearDown() {
38     if (loadClient != null) {
39       loadClient.shutdownNow();
40     }
41   }
42 
43   @Test
testHistogramToStatsConversion()44   public void testHistogramToStatsConversion() throws Exception {
45     double resolution = 1.01;
46     double maxPossible = 10000.0;
47     Control.ClientConfig.Builder config = Control.ClientConfig.newBuilder();
48     config.getHistogramParamsBuilder().setMaxPossible(maxPossible)
49         .setResolution(resolution - 1.0);
50     config.getPayloadConfigBuilder().getSimpleParamsBuilder()
51         .setReqSize(1)
52         .setRespSize(1);
53     config.setRpcType(Control.RpcType.UNARY);
54     config.setClientType(Control.ClientType.SYNC_CLIENT);
55     config.setClientChannels(1);
56     config.setOutstandingRpcsPerChannel(1);
57     config.getLoadParamsBuilder().getClosedLoopBuilder();
58     config.addServerTargets("localhost:9999");
59 
60     loadClient = new LoadClient(config.build());
61     loadClient.delay(1);
62     loadClient.delay(10);
63     loadClient.delay(10);
64     loadClient.delay(100);
65     loadClient.delay(100);
66     loadClient.delay(100);
67     loadClient.delay(1000);
68     loadClient.delay(1000);
69     loadClient.delay(1000);
70     loadClient.delay(1000);
71 
72     Stats.ClientStats stats = loadClient.getStats();
73 
74     assertEquals(1.0, stats.getLatencies().getMinSeen(), 0.0);
75     assertEquals(1000.0, stats.getLatencies().getMaxSeen(), 0.0);
76     assertEquals(10.0, stats.getLatencies().getCount(), 0.0);
77 
78     double base = 0;
79     double logBase = 1;
80 
81     for (int i = 0; i < stats.getLatencies().getBucketCount(); i++) {
82       int bucketCount = stats.getLatencies().getBucket(i);
83       if (base > 1.0 && base / resolution < 1.0) {
84         assertEquals(1, bucketCount);
85       } else if (base > 10.0 && base / resolution < 10.0) {
86         assertEquals(2, bucketCount);
87       } else if (base > 100.0 && base / resolution < 100.0) {
88         assertEquals(3, bucketCount);
89       } else if (base > 1000.0 && base / resolution < 1000.0) {
90         assertEquals(4, bucketCount);
91       } else {
92         assertEquals(0, bucketCount);
93       }
94       logBase = logBase * resolution;
95       base = logBase - 1;
96     }
97     assertTrue(base > 10000);
98     assertTrue(base / resolution <= 10000);
99   }
100 }
101