|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.commons.io.jmh; |
| 19 | + |
| 20 | +import static org.apache.commons.io.IOUtils.DEFAULT_BUFFER_SIZE; |
| 21 | +import static org.apache.commons.io.IOUtils.EOF; |
| 22 | +import static org.apache.commons.io.IOUtils.buffer; |
| 23 | + |
| 24 | +import java.io.BufferedInputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.nio.charset.Charset; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +import org.apache.commons.io.IOUtils; |
| 31 | +import org.apache.commons.lang3.StringUtils; |
| 32 | +import org.openjdk.jmh.annotations.Benchmark; |
| 33 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 34 | +import org.openjdk.jmh.annotations.Fork; |
| 35 | +import org.openjdk.jmh.annotations.Measurement; |
| 36 | +import org.openjdk.jmh.annotations.Mode; |
| 37 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 38 | +import org.openjdk.jmh.annotations.Scope; |
| 39 | +import org.openjdk.jmh.annotations.State; |
| 40 | +import org.openjdk.jmh.annotations.Warmup; |
| 41 | +import org.openjdk.jmh.infra.Blackhole; |
| 42 | + |
| 43 | +/** |
| 44 | + * Test different implementations of {@link IOUtils#contentEquals(InputStream, InputStream)}. |
| 45 | + * |
| 46 | + * <pre> |
| 47 | + * Benchmark Mode Cnt Score Error Units |
| 48 | + * IOUtilsContentEqualsInputStreamsBenchmark.testFileCurrent avgt 5 1518342.821 ▒ 201890.705 ns/op |
| 49 | + * IOUtilsContentEqualsInputStreamsBenchmark.testFilePr118 avgt 5 1578606.938 ▒ 66980.718 ns/op |
| 50 | + * IOUtilsContentEqualsInputStreamsBenchmark.testFileRelease_2_8_0 avgt 5 2439163.068 ▒ 265765.294 ns/op |
| 51 | + * IOUtilsContentEqualsInputStreamsBenchmark.testStringCurrent avgt 5 10389834700.000 ▒ 330301175.219 ns/op |
| 52 | + * IOUtilsContentEqualsInputStreamsBenchmark.testStringPr118 avgt 5 10890915400.000 ▒ 3251289634.067 ns/op |
| 53 | + * IOUtilsContentEqualsInputStreamsBenchmark.testStringRelease_2_8_0 avgt 5 12522802960.000 ▒ 111147669.527 ns/op |
| 54 | + * </pre> |
| 55 | + */ |
| 56 | +@BenchmarkMode(Mode.AverageTime) |
| 57 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 58 | +@State(Scope.Thread) |
| 59 | +@Warmup(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS) |
| 60 | +@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS) |
| 61 | +@Fork(value = 1, jvmArgs = {"-server"}) |
| 62 | +public class IOUtilsContentEqualsInputStreamsBenchmark { |
| 63 | + |
| 64 | + private static final String TEST_PATH_A = "/org/apache/commons/io/testfileBOM.xml"; |
| 65 | + private static final String TEST_PATH_16K_A = "/org/apache/commons/io/abitmorethan16k.txt"; |
| 66 | + private static final String TEST_PATH_16K_A_COPY = "/org/apache/commons/io/abitmorethan16kcopy.txt"; |
| 67 | + private static final String TEST_PATH_B = "/org/apache/commons/io/testfileNoBOM.xml"; |
| 68 | + private static final Charset DEFAULT_CHARSET = Charset.defaultCharset(); |
| 69 | + static String[] STRINGS = new String[5]; |
| 70 | + |
| 71 | + static { |
| 72 | + STRINGS[0] = StringUtils.repeat("ab", 1 << 24); |
| 73 | + STRINGS[1] = STRINGS[0] + 'c'; |
| 74 | + STRINGS[2] = STRINGS[0] + 'd'; |
| 75 | + STRINGS[3] = StringUtils.repeat("ab\rab\n", 1 << 24); |
| 76 | + STRINGS[4] = StringUtils.repeat("ab\r\nab\r", 1 << 24); |
| 77 | + } |
| 78 | + |
| 79 | + static String SPECIAL_CASE_STRING_0 = StringUtils.repeat(StringUtils.repeat("ab", 1 << 24) + '\n', 2); |
| 80 | + static String SPECIAL_CASE_STRING_1 = StringUtils.repeat(StringUtils.repeat("cd", 1 << 24) + '\n', 2); |
| 81 | + |
| 82 | + @SuppressWarnings("resource") |
| 83 | + public static boolean contentEquals_release_2_8_0(final InputStream input1, final InputStream input2) |
| 84 | + throws IOException { |
| 85 | + if (input1 == input2) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + if (input1 == null ^ input2 == null) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + final BufferedInputStream bufferedInput1 = buffer(input1); |
| 92 | + final BufferedInputStream bufferedInput2 = buffer(input2); |
| 93 | + int ch = bufferedInput1.read(); |
| 94 | + while (EOF != ch) { |
| 95 | + final int ch2 = bufferedInput2.read(); |
| 96 | + if (ch != ch2) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + ch = bufferedInput1.read(); |
| 100 | + } |
| 101 | + return bufferedInput2.read() == EOF; |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + public static boolean contentEqualsPr118(final InputStream input1, final InputStream input2) throws IOException { |
| 106 | + if (input1 == input2) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + if (input1 == null || input2 == null) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + final byte[] array1 = new byte[DEFAULT_BUFFER_SIZE]; |
| 114 | + final byte[] array2 = new byte[DEFAULT_BUFFER_SIZE]; |
| 115 | + int pos1; |
| 116 | + int pos2; |
| 117 | + int count1; |
| 118 | + int count2; |
| 119 | + while (true) { |
| 120 | + pos1 = 0; |
| 121 | + pos2 = 0; |
| 122 | + for (int index = 0; index < DEFAULT_BUFFER_SIZE; index++) { |
| 123 | + if (pos1 == index) { |
| 124 | + do { |
| 125 | + count1 = input1.read(array1, pos1, DEFAULT_BUFFER_SIZE - pos1); |
| 126 | + } while (count1 == 0); |
| 127 | + if (count1 == EOF) { |
| 128 | + return pos2 == index && input2.read() == EOF; |
| 129 | + } |
| 130 | + pos1 += count1; |
| 131 | + } |
| 132 | + if (pos2 == index) { |
| 133 | + do { |
| 134 | + count2 = input2.read(array2, pos2, DEFAULT_BUFFER_SIZE - pos2); |
| 135 | + } while (count2 == 0); |
| 136 | + if (count2 == EOF) { |
| 137 | + return pos1 == index && input1.read() == EOF; |
| 138 | + } |
| 139 | + pos2 += count2; |
| 140 | + } |
| 141 | + if (array1[index] != array2[index]) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Benchmark |
| 149 | + public boolean[] testFileCurrent() throws IOException { |
| 150 | + final boolean[] res = new boolean[3]; |
| 151 | + try (InputStream inputStream1 = this.getClass().getResourceAsStream(TEST_PATH_A); |
| 152 | + InputStream inputStream2 = this.getClass().getResourceAsStream(TEST_PATH_B)) { |
| 153 | + res[0] = IOUtils.contentEquals(inputStream1, inputStream1); |
| 154 | + } |
| 155 | + try (InputStream inputStream1 = this.getClass().getResourceAsStream(TEST_PATH_A); |
| 156 | + InputStream inputStream2 = this.getClass().getResourceAsStream(TEST_PATH_A);) { |
| 157 | + res[1] = IOUtils.contentEquals(inputStream1, inputStream2); |
| 158 | + } |
| 159 | + try (InputStream inputStream1 = this.getClass().getResourceAsStream(TEST_PATH_16K_A); |
| 160 | + InputStream inputStream2 = this.getClass().getResourceAsStream(TEST_PATH_16K_A_COPY);) { |
| 161 | + res[2] = IOUtils.contentEquals(inputStream1, inputStream2); |
| 162 | + } |
| 163 | + return res; |
| 164 | + } |
| 165 | + |
| 166 | + @Benchmark |
| 167 | + public boolean[] testFilePr118() throws IOException { |
| 168 | + final boolean[] res = new boolean[3]; |
| 169 | + try (InputStream inputStream1 = this.getClass().getResourceAsStream(TEST_PATH_A); |
| 170 | + InputStream inputStream2 = this.getClass().getResourceAsStream(TEST_PATH_B)) { |
| 171 | + res[0] = contentEqualsPr118(inputStream1, inputStream1); |
| 172 | + } |
| 173 | + try (InputStream inputStream1 = this.getClass().getResourceAsStream(TEST_PATH_A); |
| 174 | + InputStream inputStream2 = this.getClass().getResourceAsStream(TEST_PATH_A);) { |
| 175 | + res[1] = contentEqualsPr118(inputStream1, inputStream2); |
| 176 | + } |
| 177 | + try (InputStream inputStream1 = this.getClass().getResourceAsStream(TEST_PATH_16K_A); |
| 178 | + InputStream inputStream2 = this.getClass().getResourceAsStream(TEST_PATH_16K_A_COPY);) { |
| 179 | + res[2] = contentEqualsPr118(inputStream1, inputStream2); |
| 180 | + } |
| 181 | + return res; |
| 182 | + } |
| 183 | + |
| 184 | + @Benchmark |
| 185 | + public boolean[] testFileRelease_2_8_0() throws IOException { |
| 186 | + final boolean[] res = new boolean[3]; |
| 187 | + try (InputStream inputStream1 = this.getClass().getResourceAsStream(TEST_PATH_A); |
| 188 | + InputStream inputStream2 = this.getClass().getResourceAsStream(TEST_PATH_B)) { |
| 189 | + res[0] = contentEquals_release_2_8_0(inputStream1, inputStream1); |
| 190 | + } |
| 191 | + try (InputStream inputStream1 = this.getClass().getResourceAsStream(TEST_PATH_A); |
| 192 | + InputStream inputStream2 = this.getClass().getResourceAsStream(TEST_PATH_A);) { |
| 193 | + res[1] = contentEquals_release_2_8_0(inputStream1, inputStream2); |
| 194 | + } |
| 195 | + try (InputStream inputStream1 = this.getClass().getResourceAsStream(TEST_PATH_16K_A); |
| 196 | + InputStream inputStream2 = this.getClass().getResourceAsStream(TEST_PATH_16K_A_COPY);) { |
| 197 | + res[2] = contentEquals_release_2_8_0(inputStream1, inputStream2); |
| 198 | + } |
| 199 | + return res; |
| 200 | + } |
| 201 | + |
| 202 | + @Benchmark |
| 203 | + public void testStringCurrent(final Blackhole blackhole) throws IOException { |
| 204 | + for (int i = 0; i < 5; i++) { |
| 205 | + for (int j = 0; j < 5; j++) { |
| 206 | + try (InputStream inputReader1 = IOUtils.toInputStream(STRINGS[i], DEFAULT_CHARSET); |
| 207 | + InputStream inputReader2 = IOUtils.toInputStream(STRINGS[j], DEFAULT_CHARSET)) { |
| 208 | + blackhole.consume(IOUtils.contentEquals(inputReader1, inputReader2)); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + @Benchmark |
| 215 | + public void testStringPr118(final Blackhole blackhole) throws IOException { |
| 216 | + for (int i = 0; i < 5; i++) { |
| 217 | + for (int j = 0; j < 5; j++) { |
| 218 | + try (InputStream inputReader1 = IOUtils.toInputStream(STRINGS[i], DEFAULT_CHARSET); |
| 219 | + InputStream inputReader2 = IOUtils.toInputStream(STRINGS[j], DEFAULT_CHARSET)) { |
| 220 | + blackhole.consume(contentEqualsPr118(inputReader1, inputReader2)); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + @Benchmark |
| 227 | + public void testStringRelease_2_8_0(final Blackhole blackhole) throws IOException { |
| 228 | + for (int i = 0; i < 5; i++) { |
| 229 | + for (int j = 0; j < 5; j++) { |
| 230 | + try (InputStream inputReader1 = IOUtils.toInputStream(STRINGS[i], DEFAULT_CHARSET); |
| 231 | + InputStream inputReader2 = IOUtils.toInputStream(STRINGS[j], DEFAULT_CHARSET)) { |
| 232 | + blackhole.consume(contentEquals_release_2_8_0(inputReader1, inputReader2)); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | +} |
0 commit comments