|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | +package com.influxdb.client; |
| 23 | + |
| 24 | +import java.lang.management.ManagementFactory; |
| 25 | +import java.time.Duration; |
| 26 | +import java.time.Instant; |
| 27 | + |
| 28 | +import com.influxdb.client.benchmark.BenchmarkOptions; |
| 29 | +import com.influxdb.client.benchmark.ClientBenchmark; |
| 30 | +import com.influxdb.client.domain.Bucket; |
| 31 | +import org.assertj.core.api.Assertions; |
| 32 | +import org.junit.jupiter.api.AfterEach; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 36 | +import org.junit.platform.runner.JUnitPlatform; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | + |
| 39 | +/** |
| 40 | + * Client benchmark test |
| 41 | + */ |
| 42 | +@RunWith(JUnitPlatform.class) |
| 43 | +class ITBenchmarkTest extends AbstractITClientTest { |
| 44 | + |
| 45 | + private BucketsApi bucketsApi; |
| 46 | + private Bucket bucket; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + void setUp() { |
| 50 | + bucketsApi = influxDBClient.getBucketsApi(); |
| 51 | + bucket = bucketsApi.createBucket(generateName("benchmark"), findMyOrg()); |
| 52 | + } |
| 53 | + |
| 54 | + @AfterEach |
| 55 | + void tearDown() { |
| 56 | + bucketsApi.deleteBucket(bucket); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @EnabledIfSystemProperty(named = "clientBenchmark", matches = "true") |
| 61 | + public void benchmarkBenchmark() throws InterruptedException { |
| 62 | + |
| 63 | + //warmup |
| 64 | + clientBenchmark(50, 20, 100, 1_000, 5_000, 10_000_000); |
| 65 | + clientBenchmark(500, 20, 100, 1_000, 5_000, 10_000_000); |
| 66 | + clientBenchmark(1_000, 20, 100, 1_000, 5_000, 10_000_000); |
| 67 | + clientBenchmark(1_500, 20, 100, 1_000, 5_000, 10_000_000); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public void clientBenchmark(int numberOfThreads, |
| 72 | + int secondsCount, |
| 73 | + int lineProtocolsCount, |
| 74 | + int flushInterval, |
| 75 | + int batchSize, |
| 76 | + int maxBufferSize) throws InterruptedException { |
| 77 | + |
| 78 | + console(("\n--- BENCHMARK START ---")); |
| 79 | + BenchmarkOptions opts = new BenchmarkOptions(getInfluxDb2Url(), "my-token", "my-org", bucket.getName(), |
| 80 | + "sensor_" + System.currentTimeMillis(), |
| 81 | + numberOfThreads, secondsCount, lineProtocolsCount, batchSize, maxBufferSize, flushInterval); |
| 82 | + |
| 83 | + Instant startWrite = Instant.now(); |
| 84 | + com.sun.management.OperatingSystemMXBean osMxBean = |
| 85 | + ManagementFactory.getPlatformMXBean(com.sun.management.OperatingSystemMXBean.class); |
| 86 | + |
| 87 | + long cpuTimeStart = osMxBean.getProcessCpuTime(); |
| 88 | + ClientBenchmark benchmarkWriter = new ClientBenchmark(opts).start(); |
| 89 | + Instant stopWrite = Instant.now(); |
| 90 | + long cpuTimeWrite = osMxBean.getProcessCpuTime() - cpuTimeStart; |
| 91 | + |
| 92 | + double count = benchmarkWriter.verify(); |
| 93 | + Instant stopVerify = Instant.now(); |
| 94 | + long cpuTimeVerify = osMxBean.getProcessCpuTime() - cpuTimeWrite; |
| 95 | + |
| 96 | + Duration writeDuration = Duration.between(startWrite, stopWrite); |
| 97 | + Duration verifyDuration = Duration.between(stopWrite, stopVerify); |
| 98 | + |
| 99 | + double duration = (double) writeDuration.toMillis() / 1000; |
| 100 | + double rate = count / duration; |
| 101 | + console(String.format("--> total rate: %s msg/s", rate)); |
| 102 | + console(String.format("--> write duration: %s", writeDuration)); |
| 103 | + console(String.format("--> write cpu time: %s", Duration.ofNanos(cpuTimeWrite))); |
| 104 | + console(String.format("--> verify duration: %s", verifyDuration)); |
| 105 | + console(String.format("--> verity cpu time: %s", Duration.ofNanos(cpuTimeVerify))); |
| 106 | + console(String.format("--> total duration: %s", Duration.between(startWrite, stopVerify))); |
| 107 | + console(("--- BENCHMARK FINISH ---")); |
| 108 | + |
| 109 | + int idealRate = lineProtocolsCount * numberOfThreads; |
| 110 | + //maximal rate Macbook Pro I9 is cca 80_0000 msg/sec |
| 111 | + if (idealRate < 70_000) { |
| 112 | + Assertions.assertThat(rate).isGreaterThan(idealRate * 0.8); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void console(Object msg) { |
| 117 | + System.out.println(msg); |
| 118 | + } |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | + |
0 commit comments