GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestClock01.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022-2023 Gothel Software e.K.
4 * Copyright (c) 2022-2023 JogAmp Community.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26package com.jogamp.common.os;
27
28import java.time.Instant;
29import java.time.ZoneOffset;
30import java.time.temporal.ChronoUnit;
31
32import org.junit.Assert;
33import org.junit.FixMethodOrder;
34import org.junit.Test;
35import org.junit.runners.MethodSorters;
36
37import com.jogamp.common.util.TSPrinter;
38import com.jogamp.junit.util.JunitTracer;
39
40import jogamp.common.os.PlatformPropsImpl;
41
42@FixMethodOrder(MethodSorters.NAME_ASCENDING)
43public class TestClock01 extends JunitTracer {
44 TSPrinter logout = TSPrinter.stderr();
45
46 @Test
47 public void test00() {
49 TSPrinter.print(System.err, "test00\n");
50
51 final Instant m_t0 = Instant.ofEpochMilli(Clock.currentTimeMillis());
52 final Instant m_t1 = Clock.getMonotonicTime();
53 final Instant w_t2 = Instant.ofEpochSecond(Clock.wallClockSeconds());
54 final Instant w_t3 = Clock.getWallClockTime();
55
56 try {
57 Thread.sleep(100);
58 } catch (final InterruptedException e) { }
59
60 final Instant m_t0_b = Instant.ofEpochMilli(Clock.currentTimeMillis());
61 final Instant m_t1_b = Clock.getMonotonicTime();
62 final Instant w_t2_b = Instant.ofEpochSecond(Clock.wallClockSeconds());
63 final Instant w_t3_b = Clock.getWallClockTime();
64
65 final long m_t0_d = m_t0.until(m_t0_b, ChronoUnit.MILLIS);
66 final long m_t1_d = m_t1.until(m_t1_b, ChronoUnit.MILLIS);
67 final long w_t2_d = w_t2.until(w_t2_b, ChronoUnit.MILLIS);
68 final long w_t3_d = w_t3.until(w_t3_b, ChronoUnit.MILLIS);
69
70 TSPrinter.printf(System.err, "mono t0 %s, %d ms\n", m_t0.atZone(ZoneOffset.UTC), m_t0_d);
71 TSPrinter.printf(System.err, "mono t1 %s, %d ms\n", m_t1.atZone(ZoneOffset.UTC), m_t1_d);
72 TSPrinter.printf(System.err, "wall t2 %s, %d ms\n", w_t2.atZone(ZoneOffset.UTC), w_t2_d);
73 TSPrinter.printf(System.err, "wall t3 %s, %d ms\n", w_t3.atZone(ZoneOffset.UTC), w_t3_d);
74
75 final long td_min = 50;
76 final long td_max = 150;
77 Assert.assertTrue( td_min <= m_t0_d );
78 Assert.assertTrue( td_max >= m_t0_d );
79
80 Assert.assertTrue( td_min <= m_t1_d );
81 Assert.assertTrue( td_max >= m_t1_d );
82
83 Assert.assertTrue( 0 <= w_t2_d ); // sec granularity only
84 Assert.assertTrue( 1000 >= w_t2_d );
85
86 Assert.assertTrue( td_min <= w_t3_d );
87 Assert.assertTrue( td_max >= w_t3_d );
88 }
89
90 @Test
91 public void test01() {
92 TSPrinter.print(System.err, "test01\n");
93 final long tr0 = Clock.currentMillis(); // relative
94 final long ta0 = Clock.currentTimeMillis(); // absolute
95 final long td_ar = ta0 - tr0;
96 try {
97 Thread.sleep(100);
98 } catch (final InterruptedException e) { }
99 final long td_r = Clock.currentMillis() - tr0; // relative
100 final long td_a = Clock.currentTimeMillis() - ta0; // absolute
101
102 TSPrinter.printf(System.err, "mono ts ms: tr0 rel %d, ta0 abs %d, diff %d\n", tr0, ta0, td_ar);
103 TSPrinter.printf(System.err, "mono td ms: tr* rel %d, ta* abs %d\n", td_r, td_a);
104
105 // JDK-8309361: JDK-21 perf-issue: 10% error margin suffice for JDK < 21, but 20% for JDK 21
106 // See <https://bugs.openjdk.org/browse/JDK-8309361>
107 final long toleranceMS = PlatformPropsImpl.JAVA_21 ? 20 : 10;
108 Assert.assertTrue(td_ar >= 0);
109 Assert.assertTrue(Math.abs(td_r - 100) < toleranceMS);
110 Assert.assertTrue(Math.abs(td_a - 100) < toleranceMS);
111 }
112
113 public static void main(final String args[]) {
114 final String tstname = TestClock01.class.getName();
115 org.junit.runner.JUnitCore.main(tstname);
116 }
117
118}
static Instant getMonotonicTime()
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
Definition: Clock.java:63
static Instant getWallClockTime()
Returns current wall-clock real-time since Unix Epoch 00:00:00 UTC on 1970-01-01.
Definition: Clock.java:87
static native long currentTimeMillis()
Returns the unix based current monotonic time in milliseconds.
static native long wallClockSeconds()
Returns current wall-clock system time of day in seconds since Unix Epoch 00:00:00 UTC on 1 January 1...
static native long currentMillis()
Returns current monotonic milliseconds since start of this application.
Utility class for querying platform specific properties.
Definition: Platform.java:58
static void initSingleton()
kick off static initialization of platform property information and native gluegen_rt lib loading
Definition: Platform.java:359
static void main(final String args[])
A simple millisecond timestamp prepending print*() wrapper for a PrintStream.
Definition: TSPrinter.java:32
static void print(final PrintStream out, final long millis, final String msg)
Definition: TSPrinter.java:69
static synchronized TSPrinter stderr()
Definition: TSPrinter.java:36
static void printf(final PrintStream out, final long millis, final String format, final Object ... args)
Definition: TSPrinter.java:55