Skip to content

Instantly share code, notes, and snippets.

@unnivm
Last active September 27, 2023 05:40
Show Gist options
  • Save unnivm/f744919e70cf67bbf986d6651c2e39cd to your computer and use it in GitHub Desktop.
Save unnivm/f744919e70cf67bbf986d6651c2e39cd to your computer and use it in GitHub Desktop.
Code for GraalVM GC Comparison
public static void main(String[] args) {
for(int i =0;i<TopPublicTimeServers.publicTimeServers.length; i++) {
new LocalPublicTimeServerThread(TopPublicTimeServers.publicTimeServers[i]).start();
}
}
private class TopPublicTimeServers {
protected static String [] publicTimeServers = {
"time.google.com",
"time.cloudflare.com",
"time.facebook.com",
"time.windows.com",
"time.apple.com",
"time.nist.gov",
}
}
private static class LocalPublicTimeServerThread extends Thread {
public LocalPublicTimeServerThread(String name) {
this.name = name;
}
public void run() {
while(true) {
long currNanoTime = System.nanoTime();
try {
// call individual time server and get the time
startMonitor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void startMonitor() throws Exception {
NTPUDPClient client = new NTPUDPClient();
// We want to timeout if a response takes longer than 10 seconds
client.setDefaultTimeout(10_000);
InetAddress inetAddress = InetAddress.getByName(name);
long start = getCurrentSystemTime();
TimeInfo timeInfo = client.getTime(inetAddress);
long end = getCurrentSystemTime();
long avgTime = ( start + end ) / 2;
timeInfo.computeDetails();
if (timeInfo.getOffset() != null) {
this.offset = timeInfo.getOffset();
}
TimeStamp systemNtpTime = TimeStamp.getCurrentTime();
TimeStamp.getCurrentTime().getFraction();
long nstTime = systemNtpTime.getTime();
logger.info("NST time: " + nstTime);
logger.info("System time: " + avgTime);
if(nstTime > avgTime) {
logger.info("NST time is ahead by:" + Math.abs(avgTime - nstTime) + " milli seconds");
}
else if(nstTime < avgTime) {
logger.info("Local time is ahead by:" + Math.abs(avgTime - nstTime) + " milli seconds");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment