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.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 /** 29 * Tests for {@link LoadClient}. 30 */ 31 @RunWith(JUnit4.class) 32 public class LoadClientTest { 33 34 @Test testHistogramToStatsConversion()35 public void testHistogramToStatsConversion() throws Exception { 36 double resolution = 1.01; 37 double maxPossible = 10000.0; 38 Control.ClientConfig.Builder config = Control.ClientConfig.newBuilder(); 39 config.getHistogramParamsBuilder().setMaxPossible(maxPossible) 40 .setResolution(resolution - 1.0); 41 config.getPayloadConfigBuilder().getSimpleParamsBuilder() 42 .setReqSize(1) 43 .setRespSize(1); 44 config.setRpcType(Control.RpcType.UNARY); 45 config.setClientType(Control.ClientType.SYNC_CLIENT); 46 config.setClientChannels(1); 47 config.setOutstandingRpcsPerChannel(1); 48 config.getLoadParamsBuilder().getClosedLoopBuilder(); 49 config.addServerTargets("localhost:9999"); 50 51 LoadClient loadClient = new LoadClient(config.build()); 52 loadClient.delay(1); 53 loadClient.delay(10); 54 loadClient.delay(10); 55 loadClient.delay(100); 56 loadClient.delay(100); 57 loadClient.delay(100); 58 loadClient.delay(1000); 59 loadClient.delay(1000); 60 loadClient.delay(1000); 61 loadClient.delay(1000); 62 63 Stats.ClientStats stats = loadClient.getStats(); 64 65 assertEquals(1.0, stats.getLatencies().getMinSeen(), 0.0); 66 assertEquals(1000.0, stats.getLatencies().getMaxSeen(), 0.0); 67 assertEquals(10.0, stats.getLatencies().getCount(), 0.0); 68 69 double base = 0; 70 double logBase = 1; 71 72 for (int i = 0; i < stats.getLatencies().getBucketCount(); i++) { 73 int bucketCount = stats.getLatencies().getBucket(i); 74 if (base > 1.0 && base / resolution < 1.0) { 75 assertEquals(1, bucketCount); 76 } else if (base > 10.0 && base / resolution < 10.0) { 77 assertEquals(2, bucketCount); 78 } else if (base > 100.0 && base / resolution < 100.0) { 79 assertEquals(3, bucketCount); 80 } else if (base > 1000.0 && base / resolution < 1000.0) { 81 assertEquals(4, bucketCount); 82 } else { 83 assertEquals(0, bucketCount); 84 } 85 logBase = logBase * resolution; 86 base = logBase - 1; 87 } 88 assertTrue(base > 10000); 89 assertTrue(base / resolution <= 10000); 90 } 91 } 92