Last active
April 18, 2022 09:08
-
-
Save KKcorps/19b6b093bfa15b7d62b27db0b8012e51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.Instant; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
import java.util.concurrent.ThreadLocalRandom; | |
import java.util.concurrent.TimeUnit; | |
import org.joda.time.format.DateTimeFormat; | |
import org.joda.time.format.DateTimeFormatter; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.CompilerControl; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Level; | |
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.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.Warmup; | |
import org.openjdk.jmh.infra.Blackhole; | |
import static java.util.concurrent.TimeUnit.SECONDS; | |
@Warmup(iterations = 2, time = 10, timeUnit = SECONDS) | |
@Measurement(iterations = 5, time = 30, timeUnit = SECONDS) | |
@OutputTimeUnit(TimeUnit.MICROSECONDS) | |
@Fork(value = 1) | |
@State(Scope.Benchmark) | |
public class BenchmarkJoda { | |
int length = 10000; | |
public long[] _longValues; | |
public String[] _dateString; | |
public Instant[] _instants; | |
String _pattern = "yyyy-MM-dd HH:mm:ss.SSS"; | |
DateTimeFormatter _jodaDateTimeFormat = DateTimeFormat.forPattern(_pattern); | |
java.time.format.DateTimeFormatter _javaDateTimeFormat = java.time.format.DateTimeFormatter.ofPattern(_pattern).withZone( ZoneId.systemDefault() );; | |
@Setup(Level.Trial) | |
public void setup(){ | |
_longValues = new long[length]; | |
_instants = new Instant[length]; | |
_dateString = new String[length]; | |
long curr = System.currentTimeMillis(); | |
for(int i = 0 ;i < length ; i++) { | |
_longValues[i] = ThreadLocalRandom.current().nextLong(0, curr); | |
_dateString[i] = _jodaDateTimeFormat.print(_longValues[i]); | |
_instants[i] = Instant.ofEpochMilli(_longValues[i]); | |
} | |
} | |
@Benchmark | |
@BenchmarkMode(Mode.AverageTime) | |
@CompilerControl(CompilerControl.Mode.DONT_INLINE) | |
public void benchmarkJoda(Blackhole blackhole) { | |
for(int i = 0; i < length; i++) { | |
String formattedDate = _jodaDateTimeFormat.print(_longValues[i]); | |
blackhole.consume(formattedDate); | |
} | |
} | |
@Benchmark | |
@BenchmarkMode(Mode.AverageTime) | |
@CompilerControl(CompilerControl.Mode.DONT_INLINE) | |
public void benchmarkJavaTime(Blackhole blackhole) { | |
for(int i = 0; i < length; i++) { | |
String formattedDate = _javaDateTimeFormat.format(Instant.ofEpochMilli(_longValues[i])); | |
blackhole.consume(formattedDate); | |
} | |
} | |
@Benchmark | |
@BenchmarkMode(Mode.AverageTime) | |
@CompilerControl(CompilerControl.Mode.DONT_INLINE) | |
public void benchmarkJavaTimePreCompute(Blackhole blackhole) { | |
for(int i = 0; i < length; i++) { | |
String formattedDate = _javaDateTimeFormat.format(_instants[i]); | |
blackhole.consume(formattedDate); | |
} | |
} | |
@Benchmark | |
@BenchmarkMode(Mode.AverageTime) | |
@CompilerControl(CompilerControl.Mode.DONT_INLINE) | |
public void benchmarkJodaStringToEpoch(Blackhole blackhole) { | |
for(int i = 0; i < length; i++) { | |
long epoch = _jodaDateTimeFormat.parseMillis(_dateString[i]); | |
blackhole.consume(epoch); | |
} | |
} | |
@Benchmark | |
@BenchmarkMode(Mode.AverageTime) | |
@CompilerControl(CompilerControl.Mode.DONT_INLINE) | |
public void benchmarkJavaDateStringToEpoch(Blackhole blackhole) { | |
for(int i = 0; i < length; i++) { | |
long epoch = LocalDateTime.parse(_dateString[i], _javaDateTimeFormat).getNano()/1000; | |
blackhole.consume(epoch); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment