test/jdk/jdk/jfr/event/runtime/TestJavaThreadStatisticsEvent.java
changeset 50113 caf115bb98ad
child 51214 67736b4846a0
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.jfr.event.runtime;
       
    27 
       
    28 import static jdk.test.lib.Asserts.assertTrue;
       
    29 
       
    30 import java.time.Duration;
       
    31 import java.util.ArrayList;
       
    32 import java.util.List;
       
    33 import java.util.Random;
       
    34 
       
    35 import jdk.jfr.Recording;
       
    36 import jdk.jfr.consumer.RecordedEvent;
       
    37 import jdk.test.lib.jfr.EventNames;
       
    38 import jdk.test.lib.jfr.Events;
       
    39 
       
    40 /*
       
    41  * @test
       
    42  * @key jfr
       
    43  * @library /test/lib
       
    44  * @run main/othervm jdk.jfr.event.runtime.TestJavaThreadStatisticsEvent
       
    45  */
       
    46 public class TestJavaThreadStatisticsEvent {
       
    47     private static final String EVENT_NAME = EventNames.JavaThreadStatistics;
       
    48 
       
    49     private static final int THREAD_COUNT = 10;
       
    50     private static final Random RAND = new Random(4711);
       
    51 
       
    52     // verify thread count: 0 <= daemon <= active <= peak <= accumulated.
       
    53     public static void main(String[] args) throws Throwable {
       
    54         Recording recording = new Recording();
       
    55         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(10));
       
    56         recording.start();
       
    57 
       
    58         List<Thread> threads = new ArrayList<>();
       
    59         for (int i = 0; i < THREAD_COUNT; i++) {
       
    60             Thread thread = new Thread(TestJavaThreadStatisticsEvent::sleepRandom);
       
    61             thread.start();
       
    62             threads.add(thread);
       
    63         }
       
    64         for (Thread thread : threads) {
       
    65             thread.join();
       
    66         }
       
    67 
       
    68         recording.stop();
       
    69         List<RecordedEvent> events = Events.fromRecording(recording);
       
    70         boolean isAnyFound = false;
       
    71         for (RecordedEvent event : events) {
       
    72             System.out.println("Event:" + event);
       
    73             isAnyFound = true;
       
    74             // verify thread count: 0 <= daemon <= active <= peak <= accumulated.
       
    75             long daemonCount = Events.assertField(event, "daemonCount").atLeast(0L).getValue();
       
    76             long activeCount = Events.assertField(event, "activeCount").atLeast(daemonCount).getValue();
       
    77             long peakCount = Events.assertField(event, "peakCount").atLeast(activeCount).atLeast(1L).getValue();
       
    78             Events.assertField(event, "accumulatedCount").atLeast(peakCount).getValue();
       
    79         }
       
    80         assertTrue(isAnyFound, "Correct event not found");
       
    81     }
       
    82 
       
    83     static void sleepRandom() {
       
    84         try {
       
    85             Thread.sleep(RAND.nextInt(10));
       
    86         } catch (InterruptedException e) {
       
    87             e.printStackTrace();
       
    88         }
       
    89     }
       
    90 }