|
1 | 1 | package stroom.proxy.app.handler;
|
2 | 2 |
|
3 | 3 | import stroom.proxy.app.handler.NumberedDirProvider.DirId;
|
| 4 | +import stroom.test.common.TestUtil; |
| 5 | +import stroom.test.common.TestUtil.TimedCase; |
4 | 6 | import stroom.test.common.util.test.StroomUnitTest;
|
5 | 7 | import stroom.util.io.FileUtil;
|
| 8 | +import stroom.util.logging.LambdaLogger; |
| 9 | +import stroom.util.logging.LambdaLoggerFactory; |
6 | 10 |
|
| 11 | +import com.google.common.base.Strings; |
7 | 12 | import org.assertj.core.api.Assertions;
|
| 13 | +import org.junit.jupiter.api.Disabled; |
8 | 14 | import org.junit.jupiter.api.Test;
|
9 | 15 | import org.junit.jupiter.api.io.TempDir;
|
10 | 16 |
|
|
17 | 23 |
|
18 | 24 | public class TestNumberedDirProvider extends StroomUnitTest {
|
19 | 25 |
|
| 26 | + private static final LambdaLogger LOGGER = LambdaLoggerFactory.getLogger(TestNumberedDirProvider.class); |
| 27 | + |
20 | 28 | @Test
|
21 | 29 | void test() throws Exception {
|
22 | 30 | final Path dir = Files.createTempDirectory("test");
|
@@ -99,4 +107,134 @@ void testDirIdComparator() {
|
99 | 107 | .toList())
|
100 | 108 | .containsExactly(2L, 5L, 7L, 10L);
|
101 | 109 | }
|
| 110 | + |
| 111 | + @Test |
| 112 | + void testCreate() { |
| 113 | + for (int i = 0; i < 10; i++) { |
| 114 | + final long num = (long) Math.pow(10, i); |
| 115 | + final String str = Long.toString(num); |
| 116 | + LOGGER.info("str: {}, len: {}", str, str.length()); |
| 117 | + final String expected = Strings.padStart(str, 10, '0'); |
| 118 | + final String actual = NumberedDirProvider.create(num); |
| 119 | + Assertions.assertThat(actual) |
| 120 | + .isEqualTo(expected); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Disabled // Manual perf test only |
| 125 | + @Test |
| 126 | + void testCreatePerf() { |
| 127 | + |
| 128 | + final TimedCase timedCase1 = TimedCase.of("create1", (round, iterations) -> { |
| 129 | + for (long i = 0; i < iterations; i++) { |
| 130 | + final String str = create1(i); |
| 131 | + //noinspection ConstantValue |
| 132 | + if (str == null) { |
| 133 | + throw new RuntimeException("null"); |
| 134 | + } |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + final TimedCase timedCase2 = TimedCase.of("create2", (round, iterations) -> { |
| 139 | + for (long i = 0; i < iterations; i++) { |
| 140 | + final String str = create2(i); |
| 141 | + if (str == null) { |
| 142 | + throw new RuntimeException("null"); |
| 143 | + } |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + final TimedCase timedCase3 = TimedCase.of("create3", (round, iterations) -> { |
| 148 | + for (long i = 0; i < iterations; i++) { |
| 149 | + final String str = create3(i); |
| 150 | + if (str == null) { |
| 151 | + throw new RuntimeException("null"); |
| 152 | + } |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + final TimedCase timedCase4 = TimedCase.of("create4", (round, iterations) -> { |
| 157 | + for (long i = 0; i < iterations; i++) { |
| 158 | + final String str = create4(i); |
| 159 | + if (str == null) { |
| 160 | + throw new RuntimeException("null"); |
| 161 | + } |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + TestUtil.comparePerformance( |
| 166 | + 5, |
| 167 | + 10_000_000L, |
| 168 | + LOGGER::info, |
| 169 | + timedCase1, |
| 170 | + timedCase2, |
| 171 | + timedCase3, |
| 172 | + timedCase4); |
| 173 | + } |
| 174 | + |
| 175 | + private static String create1(final long num) { |
| 176 | + return Strings.padStart(Long.toString(num), 10, '0'); |
| 177 | + } |
| 178 | + |
| 179 | + private static String create2(final long num) { |
| 180 | + if (num == 0) { |
| 181 | + return "0000000000"; |
| 182 | + } else { |
| 183 | + int length = (int) (Math.log10(num) + 1); |
| 184 | + return switch (length) { |
| 185 | + case 0 -> "0000000000"; |
| 186 | + case 1 -> "000000000" + num; |
| 187 | + case 2 -> "00000000" + num; |
| 188 | + case 3 -> "0000000" + num; |
| 189 | + case 4 -> "000000" + num; |
| 190 | + case 5 -> "00000" + num; |
| 191 | + case 6 -> "0000" + num; |
| 192 | + case 7 -> "000" + num; |
| 193 | + case 8 -> "00" + num; |
| 194 | + case 9 -> "0" + num; |
| 195 | + case 10 -> "" + num; |
| 196 | + default -> throw new IllegalArgumentException("num is too big"); |
| 197 | + }; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private static String create3(final long num) { |
| 202 | + if (num == 0) { |
| 203 | + return "0000000000"; |
| 204 | + } else { |
| 205 | + final String str = String.valueOf(num); |
| 206 | + int len = str.length(); |
| 207 | + return switch (len) { |
| 208 | + case 0 -> "0000000000"; |
| 209 | + case 1 -> "000000000" + str; |
| 210 | + case 2 -> "00000000" + str; |
| 211 | + case 3 -> "0000000" + str; |
| 212 | + case 4 -> "000000" + str; |
| 213 | + case 5 -> "00000" + str; |
| 214 | + case 6 -> "0000" + str; |
| 215 | + case 7 -> "000" + str; |
| 216 | + case 8 -> "00" + str; |
| 217 | + case 9 -> "0" + str; |
| 218 | + default -> str; |
| 219 | + }; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + private static String create4(final long num) { |
| 224 | + final String str = String.valueOf(num); |
| 225 | + int len = str.length(); |
| 226 | + return switch (len) { |
| 227 | + case 0 -> "0000000000"; |
| 228 | + case 1 -> "000000000" + str; |
| 229 | + case 2 -> "00000000" + str; |
| 230 | + case 3 -> "0000000" + str; |
| 231 | + case 4 -> "000000" + str; |
| 232 | + case 5 -> "00000" + str; |
| 233 | + case 6 -> "0000" + str; |
| 234 | + case 7 -> "000" + str; |
| 235 | + case 8 -> "00" + str; |
| 236 | + case 9 -> "0" + str; |
| 237 | + default -> str; |
| 238 | + }; |
| 239 | + } |
102 | 240 | }
|
0 commit comments