1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. 2 // This source code is licensed under both the GPLv2 (found in the 3 // COPYING file in the root directory) and Apache 2.0 License 4 // (found in the LICENSE.Apache file in the root directory). 5 6 package org.rocksdb; 7 8 import org.junit.ClassRule; 9 import org.junit.Rule; 10 import org.junit.Test; 11 import org.junit.rules.TemporaryFolder; 12 13 import java.nio.charset.StandardCharsets; 14 15 import static org.assertj.core.api.Assertions.assertThat; 16 17 public class StatisticsTest { 18 19 @ClassRule 20 public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE = 21 new RocksNativeLibraryResource(); 22 23 @Rule 24 public TemporaryFolder dbFolder = new TemporaryFolder(); 25 26 @Test statsLevel()27 public void statsLevel() throws RocksDBException { 28 final Statistics statistics = new Statistics(); 29 statistics.setStatsLevel(StatsLevel.ALL); 30 assertThat(statistics.statsLevel()).isEqualTo(StatsLevel.ALL); 31 } 32 33 @Test getTickerCount()34 public void getTickerCount() throws RocksDBException { 35 try (final Statistics statistics = new Statistics(); 36 final Options opt = new Options() 37 .setStatistics(statistics) 38 .setCreateIfMissing(true); 39 final RocksDB db = RocksDB.open(opt, 40 dbFolder.getRoot().getAbsolutePath())) { 41 42 final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); 43 final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); 44 45 db.put(key, value); 46 for(int i = 0; i < 10; i++) { 47 db.get(key); 48 } 49 50 assertThat(statistics.getTickerCount(TickerType.BYTES_READ)).isGreaterThan(0); 51 } 52 } 53 54 @Test getAndResetTickerCount()55 public void getAndResetTickerCount() throws RocksDBException { 56 try (final Statistics statistics = new Statistics(); 57 final Options opt = new Options() 58 .setStatistics(statistics) 59 .setCreateIfMissing(true); 60 final RocksDB db = RocksDB.open(opt, 61 dbFolder.getRoot().getAbsolutePath())) { 62 63 final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); 64 final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); 65 66 db.put(key, value); 67 for(int i = 0; i < 10; i++) { 68 db.get(key); 69 } 70 71 final long read = statistics.getAndResetTickerCount(TickerType.BYTES_READ); 72 assertThat(read).isGreaterThan(0); 73 74 final long readAfterReset = statistics.getTickerCount(TickerType.BYTES_READ); 75 assertThat(readAfterReset).isLessThan(read); 76 } 77 } 78 79 @Test getHistogramData()80 public void getHistogramData() throws RocksDBException { 81 try (final Statistics statistics = new Statistics(); 82 final Options opt = new Options() 83 .setStatistics(statistics) 84 .setCreateIfMissing(true); 85 final RocksDB db = RocksDB.open(opt, 86 dbFolder.getRoot().getAbsolutePath())) { 87 88 final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); 89 final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); 90 91 db.put(key, value); 92 for(int i = 0; i < 10; i++) { 93 db.get(key); 94 } 95 96 final HistogramData histogramData = statistics.getHistogramData(HistogramType.BYTES_PER_READ); 97 assertThat(histogramData).isNotNull(); 98 assertThat(histogramData.getAverage()).isGreaterThan(0); 99 assertThat(histogramData.getMedian()).isGreaterThan(0); 100 assertThat(histogramData.getPercentile95()).isGreaterThan(0); 101 assertThat(histogramData.getPercentile99()).isGreaterThan(0); 102 assertThat(histogramData.getStandardDeviation()).isEqualTo(0.00); 103 assertThat(histogramData.getMax()).isGreaterThan(0); 104 assertThat(histogramData.getCount()).isGreaterThan(0); 105 assertThat(histogramData.getSum()).isGreaterThan(0); 106 assertThat(histogramData.getMin()).isGreaterThan(0); 107 } 108 } 109 110 @Test getHistogramString()111 public void getHistogramString() throws RocksDBException { 112 try (final Statistics statistics = new Statistics(); 113 final Options opt = new Options() 114 .setStatistics(statistics) 115 .setCreateIfMissing(true); 116 final RocksDB db = RocksDB.open(opt, 117 dbFolder.getRoot().getAbsolutePath())) { 118 119 final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); 120 final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); 121 122 for(int i = 0; i < 10; i++) { 123 db.put(key, value); 124 } 125 126 assertThat(statistics.getHistogramString(HistogramType.BYTES_PER_WRITE)).isNotNull(); 127 } 128 } 129 130 @Test reset()131 public void reset() throws RocksDBException { 132 try (final Statistics statistics = new Statistics(); 133 final Options opt = new Options() 134 .setStatistics(statistics) 135 .setCreateIfMissing(true); 136 final RocksDB db = RocksDB.open(opt, 137 dbFolder.getRoot().getAbsolutePath())) { 138 139 final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); 140 final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); 141 142 db.put(key, value); 143 for(int i = 0; i < 10; i++) { 144 db.get(key); 145 } 146 147 final long read = statistics.getTickerCount(TickerType.BYTES_READ); 148 assertThat(read).isGreaterThan(0); 149 150 statistics.reset(); 151 152 final long readAfterReset = statistics.getTickerCount(TickerType.BYTES_READ); 153 assertThat(readAfterReset).isLessThan(read); 154 } 155 } 156 157 @Test ToString()158 public void ToString() throws RocksDBException { 159 try (final Statistics statistics = new Statistics(); 160 final Options opt = new Options() 161 .setStatistics(statistics) 162 .setCreateIfMissing(true); 163 final RocksDB db = RocksDB.open(opt, 164 dbFolder.getRoot().getAbsolutePath())) { 165 assertThat(statistics.toString()).isNotNull(); 166 } 167 } 168 } 169