1 /* 2 * Copyright (c) 2016, 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 package jdk.jfr.api.event; 26 27 import java.time.Duration; 28 import java.util.List; 29 30 import jdk.jfr.Recording; 31 import jdk.jfr.consumer.RecordedEvent; 32 import jdk.testlibrary.jfr.Events; 33 import jdk.testlibrary.jfr.SimpleEvent; 34 35 /* 36 * @test 37 * @summary Test for RecordedEvent.getDuration() 38 * @key jfr 39 * @library /lib/testlibrary 40 * @run main/othervm jdk.jfr.api.event.TestBeginEnd 41 */ 42 public class TestBeginEnd { 43 44 public static void main(String[] args) throws Exception { 45 46 Recording r = new Recording(); 47 r.enable(SimpleEvent.class); 48 r.start(); 49 50 // Test enabled - single commit 51 SimpleEvent e1 = new SimpleEvent(); 52 e1.id = 1; // should be included 53 e1.commit(); 54 55 // Test enabled - begin - commit 56 SimpleEvent e2 = new SimpleEvent(); 57 e2.begin(); 58 e2.id = 2; // should be included 59 e2.commit(); 60 61 // Test enabled - begin - end - commit 62 SimpleEvent e3 = new SimpleEvent(); 63 e3.begin(); 64 e3.id = 3; // should be included 65 e3.end(); 66 e3.commit(); 67 68 // Test enabled - end - commit 69 SimpleEvent e4 = new SimpleEvent(); 70 e4.id = 4; // should be included 71 e4.end(); 72 e4.commit(); 73 74 // Test half enabled - begin - commit 75 r.disable(SimpleEvent.class); 76 SimpleEvent e5 = new SimpleEvent(); 77 e5.begin(); 78 r.enable(SimpleEvent.class); 79 e5.id = 5; // should be included 80 e5.commit(); 81 82 // Test half enabled - begin - end - commit 83 r.disable(SimpleEvent.class); 84 SimpleEvent r6 = new SimpleEvent(); 85 r6.begin(); 86 r.enable(SimpleEvent.class); 87 r6.id = 6; // should be included 88 r6.end(); 89 r6.commit(); 90 91 // Test half enabled - begin - commit with high threshold 92 r.disable(SimpleEvent.class); 93 SimpleEvent r7 = new SimpleEvent(); 94 r7.begin(); 95 r.enable(SimpleEvent.class).withThreshold(Duration.ofDays(1)); 96 r7.id = 7; // should not be included 97 r7.commit(); 98 r.stop(); 99 100 List<RecordedEvent> recordedEvents = Events.fromRecording(r); 101 for (RecordedEvent re : recordedEvents) { 102 Integer id = re.getValue("id"); 103 System.out.println("Found if " + id); 104 if (id < 1 || id > 6) { 105 throw new Exception("Unexpected id found " + id); 106 } 107 } 108 if (recordedEvents.size() != 6) { 109 throw new Exception("Expected 6 events"); 110 } 111 } 112 113 }