test/jdk/jdk/jfr/api/consumer/streaming/UseCasesStream.java
branchJEP-349-branch
changeset 58168 945212abbac0
parent 58167 38b5442bcab4
parent 58150 26bfa4d54737
child 58169 95274a695261
equal deleted inserted replaced
58167:38b5442bcab4 58168:945212abbac0
     1 /*
       
     2  * Copyright (c) 2019, 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.api.consumer.streaming;
       
    27 
       
    28 import java.io.FileWriter;
       
    29 import java.io.IOException;
       
    30 import java.io.PrintWriter;
       
    31 import java.nio.file.Path;
       
    32 import java.nio.file.Paths;
       
    33 import java.text.ParseException;
       
    34 import java.time.Duration;
       
    35 import java.util.ArrayDeque;
       
    36 
       
    37 import jdk.jfr.Configuration;
       
    38 import jdk.jfr.EventType;
       
    39 import jdk.jfr.ValueDescriptor;
       
    40 import jdk.jfr.consumer.RecordingStream;
       
    41 
       
    42 class UseCasesStream {
       
    43 
       
    44     //
       
    45     // Use case: Out-of-the-Box Experience
       
    46     //
       
    47     // - Simple things should be simple
       
    48     // - Pique interest, i.e. a one-liner on Stack Overflow
       
    49     // - Few lines of code as possible
       
    50     // - Should be easier than alternative technologies, like JMX and JVM TI
       
    51     //
       
    52     // - Non-goals: Corner-cases, advanced configuration, releasing resources
       
    53     //
       
    54     public static void outOfTheBox() throws InterruptedException {
       
    55         try (RecordingStream rs = new RecordingStream()) {
       
    56             rs.enable("jdk.ExceptionThrown");
       
    57             rs.onEvent(e -> System.out.println(e.getString("message")));
       
    58             rs.start();
       
    59         }
       
    60 
       
    61         // EventStream.start("jdk.JavaMonitorEnter", "threshold", "20 ms",
       
    62         // "stackTrace", "false")
       
    63         // .addConsumer(System.out::println);
       
    64         //
       
    65         // EventStream.start("jdk.CPULoad", "period", "1 s")
       
    66         // .addConsumer(e -> System.out.println(100 *
       
    67         // e.getDouble("totalMachine") + " %"));
       
    68         //
       
    69         // EventStream.start("jdk.GarbageCollection")
       
    70         // .addConsumer(e -> System.out.println("GC: " + e.getStartTime() + "
       
    71         // maxPauseTime=" + e.getDuration("maxPauseTime").toMillis() + " ms"));
       
    72 
       
    73         Thread.sleep(100_000);
       
    74     }
       
    75 
       
    76     // Use case: Event Forwarding
       
    77     //
       
    78     // - Forward arbitrary event to frameworks such as RxJava, JSON/XML and
       
    79     // Kafka
       
    80     // - Handle flooding
       
    81     // - Performant
       
    82     // - Graceful shutdown
       
    83     // - Non-goals: Filter events
       
    84     //
       
    85     public static void eventForwarding() throws InterruptedException, IOException, ParseException {
       
    86         // KafkaProducer producer = new KafkaProducer<String, String>();
       
    87         try (RecordingStream rs = new RecordingStream(Configuration.getConfiguration("default"))) {
       
    88             rs.setMaxAge(Duration.ofMinutes(5));
       
    89             rs.setMaxSize(1000_000_000L);
       
    90             // es.setParallel(true);
       
    91             // es.setReuse(true);
       
    92             // es.consume(e -> producer.send(new ProducerRecord<String,
       
    93             // String>("topic",
       
    94             // e.getString("key"), e.getString("value"))));
       
    95             rs.start();
       
    96         }
       
    97         // Write primitive values to XML
       
    98         try (RecordingStream rs = new RecordingStream(Configuration.getConfiguration("deafult"))) {
       
    99             try (PrintWriter p = new PrintWriter(new FileWriter("recording.xml"))) {
       
   100                 // es.setParallel(false);
       
   101                 // es.setReuse(true);
       
   102                 p.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
       
   103                 p.println("<events>");
       
   104                 rs.onEvent(e -> {
       
   105                     EventType type = e.getEventType();
       
   106                     p.println("  <event type=\"" + type.getName() + "\" start=\"" + e.getStartTime() + "\" end=\"" + e.getEndTime() + "\">");
       
   107                     for (ValueDescriptor field : e.getEventType().getFields()) {
       
   108                         Object value = e.getValue(field.getName());
       
   109                         if (value instanceof Number || field.getTypeName().equals("java.lang.String")) {
       
   110                             p.println("    <value field=\"" + field.getName() + "\">" + value + "</value>");
       
   111                         }
       
   112                     }
       
   113                 });
       
   114                 rs.start();
       
   115                 p.println("</events>");
       
   116             }
       
   117         }
       
   118     }
       
   119 
       
   120     // Use case: Repository Access
       
   121     //
       
   122     // - Read the disk repository from another process, for example a side car
       
   123     // in
       
   124     // Docker container
       
   125     // - Be able to configure flush interval from command line or jcmd.
       
   126     // - Graceful shutdown
       
   127     //
       
   128     public static void repositoryAccess() throws IOException, InterruptedException {
       
   129         Path repository = Paths.get("c:\\repository").toAbsolutePath();
       
   130         String command = new String();
       
   131         command += "java -XX:StartFlightRecording:flush=2s";
       
   132         command += "-XX:FlightRecorderOption:repository=" + repository + " Application";
       
   133         Process myProcess = Runtime.getRuntime().exec(command);
       
   134         try (RecordingStream rs = new RecordingStream()) {
       
   135             rs.onEvent(System.out::println);
       
   136             rs.startAsync();
       
   137             Thread.sleep(10_000);
       
   138             myProcess.destroy();
       
   139             Thread.sleep(10_000);
       
   140         }
       
   141     }
       
   142 
       
   143     // Use: Tooling
       
   144     //
       
   145     // - Monitor a stream of data for a very long time
       
   146     // - Predictable interval, i.e. once every second
       
   147     // - Notification with minimal delay
       
   148     // - Events with the same period should arrive together
       
   149     // - Consume events in chronological order
       
   150     // - Low overhead
       
   151     //
       
   152     public static void tooling() throws IOException, ParseException {
       
   153         ArrayDeque<Double> measurements = new ArrayDeque<>();
       
   154         try (RecordingStream rs = new RecordingStream(Configuration.getConfiguration("profile"))) {
       
   155             rs.setInterval(Duration.ofSeconds(1));
       
   156             rs.setMaxAge(Duration.ofMinutes(1));
       
   157             // rs.setOrdered(true);
       
   158             // rs.setReuse(false);
       
   159             // rs.setParallel(true);
       
   160             rs.onEvent("jdk.CPULoad", e -> {
       
   161                 double d = e.getDouble("totalMachine");
       
   162                 measurements.addFirst(d);
       
   163                 if (measurements.size() > 60) {
       
   164                     measurements.removeLast();
       
   165                 }
       
   166                 // repaint();
       
   167             });
       
   168             rs.start();
       
   169         }
       
   170     }
       
   171 
       
   172     // Use case: Low Impact
       
   173     //
       
   174     // - Support event subscriptions in a low latency environment (minimal GC
       
   175     // pauses)
       
   176     // - Filter out relevant events to minimize disk overhead and allocation
       
   177     // pressure
       
   178     // - Avoid impact from other recordings
       
   179     // - Avoid Heisenberg effects, in particular self-recursion
       
   180     //
       
   181     // Non-goals: one-liner
       
   182     //
       
   183     public static void lowImpact() throws InterruptedException, IOException, ParseException {
       
   184         try (RecordingStream rs = new RecordingStream()) {
       
   185             rs.enable("jdk.JavaMonitorEnter#threshold").withThreshold(Duration.ofMillis(10));
       
   186             rs.enable("jdk.ExceptionThrow#enabled");
       
   187             // ep.setReuse(true);
       
   188             rs.onEvent("jdk.JavaMonitorEnter", System.out::println);
       
   189             rs.onEvent("jdk.ExceptionThrow", System.out::println);
       
   190             rs.start();
       
   191             ;
       
   192         }
       
   193     }
       
   194 }