Created
December 20, 2022 04:09
-
-
Save ocadaruma/e68beb7e9fdad8ff8be82738fdbbfdae 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
$ java WallclockTest /path/to/test2.bin | |
PID=[114109]: Waiting for a while... | |
Start processing | |
Profiling started | |
write time: 22509508631, sleep time: 10004730457 |
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.io.IOException; | |
import java.io.UncheckedIOException; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.concurrent.atomic.AtomicReference; | |
public class WallclockTest { | |
static class HandMeasuredProfile { | |
long writeTimeNanos = 0; | |
long sleepTimeNanos = 0; | |
} | |
public static void main(String[] args) throws Exception { | |
Path filePath = Paths.get(args[0]); | |
final AtomicReference<HandMeasuredProfile> result = new AtomicReference<>(); | |
Thread th = new Thread(() -> { | |
HandMeasuredProfile profile = new HandMeasuredProfile(); | |
try (FileChannel channel = FileChannel.open( | |
filePath, | |
StandardOpenOption.CREATE, | |
StandardOpenOption.APPEND, | |
StandardOpenOption.SYNC)) { | |
System.out.printf("PID=[%d]: Waiting for a while...\n", ProcessHandle.current().pid()); | |
Thread.sleep(3000L); | |
System.out.println("Start processing"); | |
ByteBuffer buf = ByteBuffer.allocate(4); | |
buf.putInt(0); | |
for (int i = 0; i < 20; i++) { | |
buf.flip(); | |
long t0 = System.nanoTime(); | |
channel.write(buf); | |
long t = System.nanoTime(); | |
profile.writeTimeNanos += t - t0; | |
t0 = t; | |
Thread.sleep(500L); | |
t = System.nanoTime(); | |
profile.sleepTimeNanos += t - t0; | |
} | |
} catch (IOException e) { | |
throw new UncheckedIOException(e); | |
} catch (InterruptedException e) { | |
Thread.currentThread().interrupt(); | |
} finally { | |
result.set(profile); | |
} | |
}); | |
th.setName("test-thread"); | |
th.start(); | |
th.join(); | |
System.out.printf("write time: %d, sleep time: %d\n", | |
result.get().writeTimeNanos, | |
result.get().sleepTimeNanos); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment