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 26 package jdk.jfr.cmd; 27 28 import java.io.IOException; 29 import java.nio.file.Files; 30 import java.nio.file.Path; 31 import java.nio.file.Paths; 32 import java.text.ParseException; 33 import java.text.SimpleDateFormat; 34 import java.util.Date; 35 36 import jdk.jfr.Configuration; 37 import jdk.jfr.Recording; 38 import jdk.jfr.consumer.RecordingFile; 39 import jdk.testlibrary.OutputAnalyzer; 40 import jdk.testlibrary.jfr.ExecuteHelper; 41 import jdk.testlibrary.jfr.EventNames; 42 43 /* 44 * @test 45 * @summary Test jfr split 46 * @key jfr 47 * @library /lib/testlibrary 48 * @run main/othervm jdk.jfr.cmd.TestSplit 49 */ 50 public class TestSplit { 51 52 public static void main(String[] args) throws Exception { 53 SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); 54 String dateText = formatter.format(new Date()); 55 56 Path recordingFileA = Paths.get("many-chunks-A-" + dateText + ".jfr"); 57 Path recordingFileB = Paths.get("many-chunks-B-" + dateText + ".jfr"); 58 makeRecordingWithChunks(6, recordingFileA); 59 Files.copy(recordingFileA, recordingFileB); 60 61 String fileAText = recordingFileA.toAbsolutePath().toString(); 62 String fileBText = recordingFileB.toAbsolutePath().toString(); 63 64 OutputAnalyzer output = ExecuteHelper.run("split"); 65 output.shouldContain("Missing file"); 66 67 output = ExecuteHelper.run("split", "--wrongOption1", "..wrongOption2", "..wrongOption3", fileAText); 68 output.shouldContain("Too many arguments"); 69 70 output = ExecuteHelper.run("split", "--wrongOption", fileAText); 71 output.shouldContain("Unknown option"); 72 73 output = ExecuteHelper.run("split", "--wrongOption", "1", fileAText); 74 output.shouldContain("Unknown option"); 75 76 output = ExecuteHelper.run("split", "--maxchunks", "-3", fileAText); 77 output.shouldContain("Must be at least one chunk per file"); 78 79 output = ExecuteHelper.run("split", "--maxchunks", "1000", fileAText); 80 output.shouldContain("Number of chunks in recording"); 81 output.shouldContain("doesn't exceed max chunks"); 82 output = ExecuteHelper.run("split", fileAText); // maxchunks is 5 by 83 // default 84 System.out.println(output.getOutput()); 85 System.out.println(fileAText); 86 verifyRecording(fileAText.substring(0, fileAText.length() - 4) + "_1.jfr"); 87 verifyRecording(fileAText.substring(0, fileAText.length() - 4) + "_2.jfr"); 88 89 output = ExecuteHelper.run("split", "--maxchunks", "2", fileBText); 90 91 verifyRecording(fileBText.substring(0, fileBText.length() - 4) + "_1.jfr"); 92 verifyRecording(fileBText.substring(0, fileBText.length() - 4) + "_2.jfr"); 93 verifyRecording(fileBText.substring(0, fileBText.length() - 4) + "_3.jfr"); 94 95 output = ExecuteHelper.run("split", "--maxchunks", "2", fileBText); 96 output.shouldContain("file with that name already exist"); 97 } 98 99 private static void verifyRecording(String name) throws IOException { 100 System.out.println("split name " + name); 101 try (RecordingFile rf = new RecordingFile(Paths.get(name))) { 102 rf.readEvent(); 103 } 104 } 105 106 // Will create at least 2 * count + 1 chunks. 107 private static void makeRecordingWithChunks(int count, Path file) throws IOException, ParseException { 108 Recording main = new Recording(Configuration.getConfiguration("default")); 109 // default config disable all events, so ... 110 main.enable(EventNames.JVMInformation); 111 main.setToDisk(true); 112 main.start(); 113 for (int i = 0; i < count; i++) { 114 Recording r = new Recording(); 115 r.setToDisk(true); 116 r.start(); 117 r.stop(); 118 r.close(); 119 } 120 main.stop(); 121 main.dump(file); 122 main.close(); 123 } 124 }