mnaufaldillah

ThreadDumper Jmeter

Oct 23rd, 2021
589
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to you under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. package org.apache.jorphan.util;
  19.  
  20. import java.io.BufferedWriter;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStreamWriter;
  25. import java.io.Writer;
  26. import java.lang.management.ManagementFactory;
  27. import java.lang.management.ThreadInfo;
  28. import java.lang.management.ThreadMXBean;
  29. import java.nio.charset.StandardCharsets;
  30. import java.text.SimpleDateFormat;
  31. import java.util.Date;
  32.  
  33. /**
  34.  * Utility class to create a Thread Dump
  35.  * @since 3.2
  36.  */
  37. public class ThreadDumper {
  38.  
  39.     // Only invoked by IODH class
  40.     private ThreadDumper() {
  41.         super();
  42.     }
  43.  
  44.     /**
  45.      * Returns name of file containing thread dump.
  46.      * @return Name of file containing thread dump
  47.      * @throws Exception if file cannot be written
  48.      */
  49.     public static String threadDump() throws Exception {
  50.         return threadDump(new File(".")); //$NON-NLS-1$
  51.     }
  52.  
  53.     /**
  54.      * Returns name of file containing thread dump.
  55.      * @param basedir {@link File} Base directory
  56.      * @return Name of file containing thread dump
  57.      * @throws Exception  if file cannot we written
  58.      */
  59.     @SuppressWarnings("JdkObsolete")
  60.     public static String threadDump(File basedir) throws Exception {
  61.         SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMdd_hhmmss_SSS");
  62.         String stamp = timestampFormat.format(new Date());
  63.         File temp = new File(basedir,"thread_dump_"+stamp+".log");
  64.         final String path = temp.getPath();
  65.         try (FileOutputStream fos = new FileOutputStream(temp);
  66.                 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
  67.                 BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter)) {
  68.             writeThreadDump(bufferedWriter);
  69.         }
  70.         return path;
  71.     }
  72.  
  73.     /**
  74.      * Write Thread Dump
  75.      * @param writer {@link Writer}
  76.      * @throws IOException if file cannot be written
  77.      */
  78.     public static void writeThreadDump(Writer writer) throws IOException {
  79.         ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
  80.         for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
  81.             writer.write(ti.toString());
  82.         }
  83.     }
  84. }
RAW Paste Data