Skip to content

Instantly share code, notes, and snippets.

@yv84
Last active October 8, 2019 04:16
Show Gist options
  • Save yv84/0fe155f9daa96616ff7fe011fe3fd21c to your computer and use it in GitHub Desktop.
Save yv84/0fe155f9daa96616ff7fe011fe3fd21c to your computer and use it in GitHub Desktop.
Fileslines.reduce.java
package ru.fss.print.client.kp.decisionrenewal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
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.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;
import org.springframework.test.context.ActiveProfiles;
//@Disabled
@ActiveProfiles("test")
@Slf4j
public class PerformanceTest {
@Test public void
launchBenchmark() throws Exception {
Options opt = new OptionsBuilder()
// Specify which benchmarks to run.
// You can be more specific if you'd like to run only one benchmark per test.
.include(this.getClass().getName() + ".*")
// Set the following options as needed
.mode (Mode.AverageTime)
.timeUnit(TimeUnit.MICROSECONDS)
.warmupTime(TimeValue.seconds(1))
.warmupIterations(2)
.measurementTime(TimeValue.seconds(1))
.measurementIterations(2)
.threads(2)
.forks(1)
.shouldFailOnError(true)
.shouldDoGC(true)
//.jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining")
//.addProfiler(WinPerfAsmProfiler.class)
.build();
new Runner(opt).run();
}
// The JMH samples are the best documentation for how to use it
// http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/
@State (Scope.Thread)
public static class BenchmarkState {
@Disabled
@Test
@Benchmark
@Fork(value = 1, warmups = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void streamLinesReduceTest() throws Exception {
String metadataPath = "C:/Users/yv84/dev/ibs/fcc/arm-kp";
Path metadataPathP = Paths.get(metadataPath, "metadata.json");
String collect = null;
try (Stream<String> stream = Files.lines(metadataPathP)) {
collect = stream.reduce("", (a, b) -> a + b);
}
Assertions.assertNotNull(collect);
}
@Disabled
@Test
@Benchmark
@Fork(value = 1, warmups = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void streamLinesJoiningTest() throws Exception {
String metadataPath = "C:/Users/yv84/dev/ibs/fcc/arm-kp";
Path metadataPathP = Paths.get(metadataPath, "metadata.json");
String collect = null;
try (Stream<String> stream = Files.lines(metadataPathP)) {
collect = stream.collect(Collectors.joining());
}
Assertions.assertNotNull(collect);
}
@Disabled
@Test
@Benchmark
@Fork(value = 1, warmups = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void streamLinesStringBuilderTest() throws Exception {
String metadataPath = "C:/Users/yv84/dev/ibs/fcc/arm-kp";
Path metadataPathP = Paths.get(metadataPath, "metadata.json");
String collect = null;
try (Stream<String> stream = Files.lines(metadataPathP)) {
StringBuilder sb = new StringBuilder();
stream.forEach(sb::append);
collect = sb.toString();
}
Assertions.assertNotNull(collect);
}
@Test
@Benchmark
@Fork(value = 1000, warmups = 10)
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void test2Test() throws Exception {
log.trace("{}", 1 + 2 + 3 + 4);
}
//@Disabled
@Test
@Benchmark
@Fork(value = 1000, warmups = 10)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void test1Test() throws Exception {
log.trace("{} - {} - {} - {}", 1, 2, 3, 4);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment