apache / apache/pinot

Benchmark of ThreadTimer, System.currentTimeMillis() and System.nanoTime()

Open
#7,866 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
6.1k
Forks
1.5k
Avg merge
1d 21h
Merged PRs (30d)
189

Description

Benchmark code
```
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Measurement(iterations = 5, time = 30)
@Fork(1)
@State(Scope.Benchmark)
public class BenchmarkTimer {

public static void main(String[] args)
throws Exception {
ChainedOptionsBuilder opt = new OptionsBuilder().include(BenchmarkTimer.class.getSimpleName());
new Runner(opt.build()).run();
}

@Benchmark
public void benchmarkThreadCpuTimer() {
ThreadTimer threadTimer = new ThreadTimer();
long totalThreadCpuTimeNs = threadTimer.getThreadTimeNs();
}

@Benchmark
public void benchmarkSystemCurrentTimeMillis() {
long startWallClockTimeMs = System.currentTimeMillis();
long totalWallClockTimeMs = System.currentTimeMillis() - startWallClockTimeMs;
long totalWallClockTimeNs = TimeUnit.MILLISECONDS.toNanos(totalWallClockTimeMs);
}

@Benchmark
public void benchmarkSystemNanoTime() {
long startWallClockTimeNs = System.nanoTime();
long totalWallClockTimeNs = System.nanoTime() - startWallClockTimeNs;
}
}

```

Benchmark result of adoptopenjdk-11.jdk on macOS Monterey v12.0.1
```
Benchmark Mode Cnt Score Error Units
BenchmarkTimer.benchmarkSystemCurrentTimeMillis avgt 5 54.111 ± 12.025 ns/op
BenchmarkTimer.benchmarkSystemNanoTime avgt 5 61.704 ± 0.645 ns/op
BenchmarkTimer.benchmarkThreadCpuTimer avgt 5 0.526 ± 0.023 ns/op
```

Benchmark result of jdk11.0.10-msft.jdk on macOs Big sur v11.6.1
```
Benchmark Mode Cnt Score Error Units
BenchmarkTimer.benchmarkSystemCurrentTimeMillis avgt 5 48.704 ± 2.548 ns/op
BenchmarkTimer.benchmarkSystemNanoTime avgt 5 56.387 ± 2.091 ns/op
BenchmarkTimer.benchmarkThreadCpuTimer avgt 5 0.476 ± 0.009 ns/op
```

Looks like thread cpu time measuring (retrieve from java.lang.management.ThreadMXBean) has little overhead, while wall clock time measuring (need a system call. Depends on the JVM and OS, the system call can be accelerate by vDSO to avoid user-space/kernel-space switch) is much more expensive.

cc @siddharthteotia @richardstartin

Contributor guide

Open the contributing guide

Research direction

The issue provides standalone BenchmarkTimer methods comparing ThreadTimer, System.currentTimeMillis(), and System.nanoTime(), but names no repository file or test. First clarify whether a benchmark should be added and where it belongs; completion would require an agreed benchmark location, reproducible measurements, and documented comparison results.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.