garydgregory commented on a change in pull request #184: URL: https://github.com/apache/commons-io/pull/184#discussion_r552943274
########## File path: src/test/java/org/apache/commons/io/performance/IOUtilsContentEqualsPerformanceTest.java ########## @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.performance; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.util.concurrent.TimeUnit; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.buffer.UnsynchronizedBufferedInputStream; +import org.apache.commons.io.input.buffer.UnsynchronizedBufferedReader; +import org.apache.commons.lang3.StringUtils; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +/** + * Test to show whether using BitSet for removeAll() methods is faster than using HashSet. + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@State(Scope.Thread) +@Warmup(iterations = 1, time = 10, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 1, time = 10, timeUnit = TimeUnit.SECONDS) +@Fork(value = 1, jvmArgs = {"-server"}) +public class IOUtilsContentEqualsPerformanceTest { + static String[] STRINGS = new String[1]; + + static { + STRINGS[0] = StringUtils.repeat("ab", 1 << 24); + } + + @Benchmark + public void testBufferedInputStream(Blackhole blackhole) throws IOException { + try ( + InputStream inputStream = IOUtils.toInputStream(STRINGS[0]); + BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream) + ) { + while (true) { + int nowI = bufferedInputStream.read(); + // do nothing + blackhole.consume(nowI); + if (nowI != -1) { + break; + } + } + } + } + + @Benchmark + public void testUnsynchronizedBufferedInputStream(Blackhole blackhole) throws IOException { + try ( + InputStream inputStream = IOUtils.toInputStream(STRINGS[0]); + UnsynchronizedBufferedInputStream bufferedInputStream = new UnsynchronizedBufferedInputStream(inputStream) + ) { + while (true) { + int nowI = bufferedInputStream.read(); + // do nothing + blackhole.consume(nowI); + if (nowI != -1) { + break; + } + } + } + } + + @Benchmark + public void testBufferedReader(Blackhole blackhole) throws IOException { + try ( + StringReader inputReader = new StringReader(STRINGS[0]); + BufferedReader bufferedReader = new BufferedReader(inputReader) + ) { + while (true) { + int nowI = bufferedReader.read(); + // do nothing + blackhole.consume(nowI); + if (nowI != -1) { + break; + } + } + } + } + + @Benchmark + public void testUnsynchronizedBufferedReader(Blackhole blackhole) throws IOException { + try ( + StringReader inputReader = new StringReader(STRINGS[0]); + UnsynchronizedBufferedReader bufferedReader = new UnsynchronizedBufferedReader(inputReader) + ) { + while (true) { + int nowI = bufferedReader.read(); + // do nothing + blackhole.consume(nowI); + if (nowI != -1) { + break; + } + } + } + } + + + + + + + + + + + + + + + + + @Benchmark + public void testBufferedInputStream2(Blackhole blackhole) throws IOException { + try ( + InputStream inputStream = IOUtils.toInputStream(STRINGS[0]); + BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream) + ) { + byte[] bytes = new byte[1]; + while (true) { + int nowI = bufferedInputStream.read(bytes , 0, 1); + // do nothing + blackhole.consume(nowI); + if (nowI != -1) { + break; + } + } + } + } + + @Benchmark + public void testUnsynchronizedBufferedInputStream2(Blackhole blackhole) throws IOException { + try ( + InputStream inputStream = IOUtils.toInputStream(STRINGS[0]); + UnsynchronizedBufferedInputStream bufferedInputStream = new UnsynchronizedBufferedInputStream(inputStream) + ) { + byte[] bytes = new byte[1]; + while (true) { + int nowI = bufferedInputStream.read(bytes , 0, 1); + // do nothing + blackhole.consume(nowI); + if (nowI != -1) { + break; + } + } + } + } + + @Benchmark + public void testBufferedReader2(Blackhole blackhole) throws IOException { + try ( + StringReader inputReader = new StringReader(STRINGS[0]); + BufferedReader bufferedReader = new BufferedReader(inputReader) + ) { + char[] bytes = new char[1]; + while (true) { + int nowI = bufferedReader.read(bytes , 0, 1); + // do nothing + blackhole.consume(nowI); + if (nowI != -1) { + break; + } + } + } + } + + @Benchmark + public void testUnsynchronizedBufferedReader2(Blackhole blackhole) throws IOException { + try ( + StringReader inputReader = new StringReader(STRINGS[0]); + UnsynchronizedBufferedReader bufferedReader = new UnsynchronizedBufferedReader(inputReader) + ) { + char[] bytes = new char[1]; + while (true) { + int nowI = bufferedReader.read(bytes , 0, 1); + // do nothing + blackhole.consume(nowI); + if (nowI != -1) { + break; + } + } + } + } + +} Review comment: It would simpler to validate this test if it was factored in such a way that a worker method was given a stream such that its called can call it with a constructed object, once for the JRE stream, another with the custom stream. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
