Java Code Examples for jdk.test.lib.Asserts#assertLessThan()

The following examples show how to use jdk.test.lib.Asserts#assertLessThan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestThreadCpuTimeEvent.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void verifyPerThreadInvariant(List<RecordedEvent> events, String threadName) {
    List<RecordedEvent> filteredEvents = events.stream()
            .filter(e -> e.getThread().getJavaName().equals(threadName))
            .sorted(Comparator.comparing(RecordedEvent::getStartTime))
            .collect(Collectors.toList());

    int numCpus = Runtime.getRuntime().availableProcessors();
    Iterator<RecordedEvent> i = filteredEvents.iterator();
    while (i.hasNext()) {
        RecordedEvent event = i.next();

        Float systemLoad = (Float)event.getValue("system");
        Float userLoad = (Float)event.getValue("user");

        Asserts.assertLessThan(systemLoad + userLoad, 1.01f / numCpus); // 100% + rounding errors
    }
}
 
Example 2
Source File: TestThreadCpuTimeEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static void verifyPerThreadInvariant(List<RecordedEvent> events, String threadName) {
    List<RecordedEvent> filteredEvents = events.stream()
            .filter(e -> e.getThread().getJavaName().equals(threadName))
            .sorted(Comparator.comparing(RecordedEvent::getStartTime))
            .collect(Collectors.toList());

    int numCpus = Runtime.getRuntime().availableProcessors();
    Iterator<RecordedEvent> i = filteredEvents.iterator();
    while (i.hasNext()) {
        RecordedEvent event = i.next();

        Float systemLoad = (Float)event.getValue("system");
        Float userLoad = (Float)event.getValue("user");

        Asserts.assertLessThan(systemLoad + userLoad, 1.01f / numCpus); // 100% + rounding errors
    }
}
 
Example 3
Source File: TestSnapshot.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void testEmpty() throws IOException {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();
    Instant before = Instant.now().minusNanos(1);
    try (Recording snapshot = recorder.takeSnapshot()) {
        Instant after = Instant.now().plusNanos(1);
        Asserts.assertEquals(snapshot.getSize(), 0L);
        Asserts.assertLessThan(before, snapshot.getStartTime());
        Asserts.assertGreaterThan(after, snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getStartTime(), snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getDuration(), Duration.ZERO);
        assertStaticOptions(snapshot);
        Asserts.assertEquals(snapshot.getStream(null, null), null);
    }
}
 
Example 4
Source File: TestThreadCpuTimeEvent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void testCompareWithMXBean() throws Throwable {
    Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
    CyclicBarrier barrier = new CyclicBarrier(2);
    CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);
    thread.start();

    List<RecordedEvent> beforeEvents = generateEvents(2, barrier);
    verifyPerThreadInvariant(beforeEvents, cpuConsumerThreadName);

    // Run a second single pass
    barrier.await();
    barrier.await();

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    Duration cpuTime = Duration.ofNanos(bean.getThreadCpuTime(thread.getId()));
    Duration userTime = Duration.ofNanos(bean.getThreadUserTime(thread.getId()));

    // Check something that should hold even in the presence of unfortunate scheduling
    Asserts.assertGreaterThanOrEqual(cpuTime.toMillis(), eventPeriodMillis);
    Asserts.assertGreaterThanOrEqual(userTime.toMillis(), eventPeriodMillis);

    Duration systemTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "system");
    Duration userTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "user");
    Duration cpuTimeBefore = userTimeBefore.plus(systemTimeBefore);

    Asserts.assertLessThan(cpuTimeBefore, cpuTime);
    Asserts.assertLessThan(userTimeBefore, userTime);
    Asserts.assertGreaterThan(cpuTimeBefore, Duration.ZERO);

    thread.interrupt();
    thread.join();
}
 
Example 5
Source File: TestSnapshot.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void testEmpty() throws IOException {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();
    Instant before = Instant.now().minusNanos(1);
    try (Recording snapshot = recorder.takeSnapshot()) {
        Instant after = Instant.now().plusNanos(1);
        Asserts.assertEquals(snapshot.getSize(), 0L);
        Asserts.assertLessThan(before, snapshot.getStartTime());
        Asserts.assertGreaterThan(after, snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getStartTime(), snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getDuration(), Duration.ZERO);
        assertStaticOptions(snapshot);
        Asserts.assertEquals(snapshot.getStream(null, null), null);
    }
}
 
Example 6
Source File: TestThreadCpuTimeEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void testCompareWithMXBean() throws Throwable {
    Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
    CyclicBarrier barrier = new CyclicBarrier(2);
    CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);
    thread.start();

    List<RecordedEvent> beforeEvents = generateEvents(2, barrier);
    verifyPerThreadInvariant(beforeEvents, cpuConsumerThreadName);

    // Run a second single pass
    barrier.await();
    barrier.await();

    ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
    Duration cpuTime = Duration.ofNanos(bean.getThreadCpuTime(thread.getId()));
    Duration userTime = Duration.ofNanos(bean.getThreadUserTime(thread.getId()));

    // Check something that should hold even in the presence of unfortunate scheduling
    Asserts.assertGreaterThanOrEqual(cpuTime.toMillis(), eventPeriodMillis);
    Asserts.assertGreaterThanOrEqual(userTime.toMillis(), eventPeriodMillis);

    Duration systemTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "system");
    Duration userTimeBefore = getAccumulatedTime(beforeEvents, cpuConsumerThreadName, "user");
    Duration cpuTimeBefore = userTimeBefore.plus(systemTimeBefore);

    Asserts.assertLessThan(cpuTimeBefore, cpuTime);
    Asserts.assertLessThan(userTimeBefore, userTime);
    Asserts.assertGreaterThan(cpuTimeBefore, Duration.ZERO);

    thread.interrupt();
    thread.join();
}