Last active
November 23, 2022 15:24
-
-
Save vladimirdolzhenko/a46431907d31bbfda73305d876711f28 to your computer and use it in GitHub Desktop.
PerfTiming
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
package com; | |
/* | |
MacOSX: | |
java 8 | |
Benchmark Mode Cnt Score Error Units | |
ClockGranularity.granularityClockNanos avgt 2 999929.426 ns/op | |
ClockGranularity.granularityNanos avgt 2 41.420 ns/op | |
java 9 | |
Benchmark Mode Cnt Score Error Units | |
ClockGranularity.granularityClockNanos avgt 2 1011,522 ns/op | |
ClockGranularity.granularityNanos avgt 2 41,657 ns/op | |
Windows: | |
java 9 | |
Benchmark Mode Cnt Score Error Units | |
ClockGranularity.granularityClockNanos avgt 2 999916.218 ns/op | |
ClockGranularity.granularityNanos avgt 2 301.552 ns/op | |
Linux: | |
java 8 | |
Benchmark Mode Cnt Score Error Units | |
ClockGranularity.granularityClockNanos avgt 2 999881.640 ns/op | |
ClockGranularity.granularityNanos avgt 2 42.009 ns/op | |
java 9 | |
Benchmark Mode Cnt Score Error Units | |
ClockGranularity.granularityClockNanos avgt 2 1002.419 ns/op | |
ClockGranularity.granularityNanos avgt 2 43.195 ns/op | |
*/ | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import java.time.Clock; | |
import java.time.Instant; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* @author vladimir.dolzhenko@gmail.com | |
* @since 2017-05-13 | |
*/ | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@Fork(1) | |
@Warmup(iterations = 1, time = 15000, timeUnit = TimeUnit.MILLISECONDS) | |
@Measurement(iterations = 2, time = 15000, timeUnit = TimeUnit.MILLISECONDS) | |
@Threads(1) | |
@State(Scope.Benchmark) | |
public class ClockGranularity { | |
private long lastValue; | |
public static final Clock clock = Clock.systemUTC(); | |
public static long clockNanos(){ | |
final Instant instant = clock.instant(); | |
return instant.getEpochSecond() * 1_000_000_000L + instant.getNano(); | |
} | |
@Benchmark | |
public long granularityClockNanos() { | |
long cur; | |
do { | |
cur = clockNanos(); | |
} while (cur == lastValue); | |
lastValue = cur; | |
return cur; | |
} | |
@Benchmark | |
public long granularityNanos() { | |
long cur; | |
do { | |
cur = System.nanoTime(); | |
} while (cur == lastValue); | |
lastValue = cur; | |
return cur; | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(ClockGranularity.class.getSimpleName()) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
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
/* | |
to compile on MacOsX: | |
$ gcc -dynamiclib -o libprecisetimestamp.dylib com_PreciseTimestamp.c -I$JAVA_HOME/include -I$JAVA_HOME/include/darwin -framework JavaVM | |
*/ | |
#include <stdlib.h> | |
#include <sys/time.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include "com_PreciseTimestamp.h" | |
JNIEXPORT jlong JNICALL Java_com_PreciseTimestamp_getMicros | |
(JNIEnv * env, jclass jc) | |
{ | |
#if defined(_POSIX_TIMERS) | |
{ | |
struct timespec ts; | |
if ( 0 == clock_gettime(CLOCK_REALTIME, &ts) ) | |
{ | |
return ((((jlong) ts.tv_sec) * 1000000) + | |
(((jlong) ts.tv_nsec)) / 1000); | |
} | |
} | |
#else | |
// otherwise use gettimeofday | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
return (((jlong) tv.tv_sec) * 1000000) + ((jlong) tv.tv_usec); | |
#endif // _POSIX_TIMERS | |
} | |
JNIEXPORT jlong JNICALL Java_com_PreciseTimestamp_getCMicros | |
(JNIEnv * env, jclass jc) | |
{ | |
#if defined(_POSIX_TIMERS) | |
{ | |
struct timespec ts; | |
if ( 0 == clock_gettime(CLOCK_REALTIME, &ts) ) | |
{ | |
return ((((jlong) ts.tv_sec) * 1000000) + | |
(((jlong) ts.tv_nsec)) / 1000); | |
} | |
} | |
#else | |
// otherwise use gettimeofday | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
return (((jlong) tv.tv_sec) * 1000000) + ((jlong) tv.tv_usec); | |
#endif | |
} | |
JNIEXPORT jlong JNICALL JavaCritical_com_PreciseTimestamp_getCMicros | |
() | |
{ | |
#if defined(_POSIX_TIMERS) | |
{ | |
struct timespec ts; | |
if ( 0 == clock_gettime(CLOCK_REALTIME, &ts) ) | |
{ | |
return ((((jlong) ts.tv_sec) * 1000000) + | |
(((jlong) ts.tv_nsec)) / 1000); | |
} | |
} | |
#else | |
// otherwise use gettimeofday | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
return (((jlong) tv.tv_sec) * 1000000) + ((jlong) tv.tv_usec); | |
#endif | |
} |
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
/* DO NOT EDIT THIS FILE - it is machine generated */ | |
#include <jni.h> | |
/* Header for class com_PreciseTimestamp */ | |
#ifndef _Included_com_PreciseTimestamp | |
#define _Included_com_PreciseTimestamp | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
/* | |
* Class: com_PreciseTimestamp | |
* Method: getMicros | |
* Signature: ()J | |
*/ | |
JNIEXPORT jlong JNICALL Java_com_PreciseTimestamp_getMicros | |
(JNIEnv *, jclass); | |
/* | |
* Class: com_PreciseTimestamp | |
* Method: getCMicros | |
* Signature: ()J | |
*/ | |
JNIEXPORT jlong JNICALL Java_com_PreciseTimestamp_getCMicros | |
(JNIEnv *, jclass); | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif |
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
package com; | |
/* | |
Benchmark Mode Cnt Score Error Units | |
PerfTiming.clockMicros avgt 5 60,452 ± 2,097 ns/op | |
PerfTiming.criticalJniMicros avgt 5 53,218 ± 1,220 ns/op | |
PerfTiming.instant avgt 5 62,082 ± 1,679 ns/op | |
PerfTiming.jniMicros avgt 5 54,001 ± 1,089 ns/op | |
PerfTiming.systemCurrentTimeMillis avgt 5 44,332 ± 0,625 ns/op | |
PerfTiming.systemNanoTime avgt 5 40,342 ± 1,322 ns/op | |
Benchmark Mode Cnt Score Error Units | |
PerfTiming.clockMicros avgt 5 60,832 ± 1,356 ns/op | |
PerfTiming.clockMicros:·gc.alloc.rate avgt 5 ≈ 10⁻⁵ MB/sec | |
PerfTiming.clockMicros:·gc.alloc.rate.norm avgt 5 ≈ 10⁻⁶ B/op | |
PerfTiming.clockMicros:·gc.count avgt 5 ≈ 0 counts | |
PerfTiming.instant avgt 5 67,714 ± 2,713 ns/op | |
PerfTiming.instant:·gc.alloc.rate avgt 5 327,083 ± 13,098 MB/sec | |
PerfTiming.instant:·gc.alloc.rate.norm avgt 5 24,000 ± 0,001 B/op | |
PerfTiming.instant:·gc.churn.CodeHeap_'non-profiled_nmethods' avgt 5 0,001 ± 0,001 MB/sec | |
PerfTiming.instant:·gc.churn.CodeHeap_'non-profiled_nmethods'.norm avgt 5 ≈ 10⁻⁴ B/op | |
PerfTiming.instant:·gc.churn.G1_Old_Gen avgt 5 327,390 ± 20,651 MB/sec | |
PerfTiming.instant:·gc.churn.G1_Old_Gen.norm avgt 5 24,021 ± 0,644 B/op | |
PerfTiming.instant:·gc.count avgt 5 167,000 counts | |
PerfTiming.instant:·gc.time avgt 5 102,000 ms | |
*/ | |
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.Threads; | |
import org.openjdk.jmh.annotations.Warmup; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import java.time.Instant; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* @author vladimir.dolzhenko@gmail.com | |
* @since 2017-05-09 | |
*/ | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@Fork(1) | |
@Warmup(iterations = 3, time = 15000, timeUnit = TimeUnit.MILLISECONDS) | |
@Measurement(iterations = 5, time = 15000, timeUnit = TimeUnit.MILLISECONDS) | |
@Threads(1) | |
public class PerfTiming { | |
@Benchmark | |
public long systemNanoTime() { | |
return System.nanoTime(); | |
} | |
@Benchmark | |
public long systemCurrentTimeMillis() { | |
return System.currentTimeMillis(); | |
} | |
@Benchmark | |
public long jniMicros() { | |
return PreciseTimestamp.getMicros(); | |
} | |
@Benchmark | |
public long criticalJniMicros() { | |
return PreciseTimestamp.getCMicros(); | |
} | |
@Benchmark | |
public long clockMicros(){ | |
return PreciseTimestamp.clockMicros(); | |
} | |
@Benchmark | |
public Instant instant(){ | |
return PreciseTimestamp.clock.instant(); | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.addProfiler("gc") | |
.include(PerfTiming.class.getSimpleName()) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
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
/* | |
java 8: | |
JNI micros: 1494398000506568 | |
Critical JNI micros: 1494398000506986 | |
ClockUTC micros: 1494398000507000 | |
currentTimeMillis: 1494398000507 | |
java 9: | |
JNI micros: 1494398039238236 | |
Critical JNI micros: 1494398039238439 | |
ClockUTC micros: 1494398039238498 | |
currentTimeMillis: 1494398039239 | |
*/ | |
package com; | |
import java.time.Clock; | |
import java.time.Instant; | |
public final class PreciseTimestamp { | |
static { | |
try { | |
System.loadLibrary("precisetimestamp"); | |
} catch (Throwable e){ | |
throw new RuntimeException(e.getMessage(), e); | |
} | |
} | |
// JNI microsecond timestamp provider | |
public static native long getMicros(); | |
// Critical JNI microsecond timestamp provider | |
public static native long getCMicros(); | |
public static final Clock clock = Clock.systemUTC(); | |
public static long clockMicros(){ | |
final Instant instant = clock.instant(); | |
return instant.getEpochSecond() * 1_000_000L + (instant.getNano() / 1_000); | |
} | |
public static void main(String[] args) { | |
System.out.println("JNI micros:\t\t\t\t" + PreciseTimestamp.getMicros()); | |
System.out.println("Critical JNI micros:\t" + PreciseTimestamp.getCMicros()); | |
System.out.println("ClockUTC micros:\t\t" + PreciseTimestamp.clockMicros()); | |
System.out.println("currentTimeMillis:\t\t" + System.currentTimeMillis()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment