jstat를 jfreechart로 그래프화

import java.awt.Color; import java.awt.Font; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardChartTheme; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CombinedDomainXYPlot; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.StandardXYItemRenderer; import org.jfree.chart.title.LegendTitle; import org.jfree.data.time.Millisecond; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; public class GraphForJstat { static int interval = 10; // second static SimpleDateFormat sdf = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”); static class Journal implements Serializable { private static final long serialVersionUID = -3906379676663036836L; private String timestampString; private Date timestamp; private double s0c; private double s1c; private double s0u; private double s1u; private double ec; private double eu; private double oc; private double ou; private double pc; private double pu; private int ygc; private double ygct; private double ygctDiff; private int fgc; private double fgct; private double fgctDiff; private double gct; private double gctDiff; public Journal(String timestamp) { try { this.timestamp = sdf.parse(timestamp); } catch (Exception e) { throw new RuntimeException(e); } this.timestampString = timestamp; } public Journal(Date timestamp) { this.timestamp = timestamp; this.timestampString = sdf.format(timestamp); } } public static List<Journal> readFile(String file) throws Exception { long started = System.currentTimeMillis(); System.out.println(“Open ” + file); List<Journal> result = new ArrayList<Journal>(); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String[] tokens = null; String line = null; Journal j = null; int row = 0; int journalCount = 0; long lastTimed = System.currentTimeMillis(); Calendar timestamp = Calendar.getInstance(); while ((line = br.readLine()) != null) { row++; if (row % 100000 == 0) { System.out.println(row + ” line(s) buffered. (memory ” + (Runtime.getRuntime().freeMemory() * 100 / Runtime.getRuntime().maxMemory()) + “% used) ” + (System.currentTimeMillis() – lastTimed) + “ms”); lastTimed = System.currentTimeMillis(); } if (line.trim().length() == 0) continue; line = line.trim(); if (line.startsWith(“S0C”)) { // header line continue; } if (line.startsWith(“—–“)) { // 2014/10/28 23:04:04 timestamp.setTime(sdf.parse(line.replaceAll(“—–“, “”).trim())); } else { // data field List<String> buffers = new ArrayList<String>(); StringBuffer outB = new StringBuffer(); for (int i = 0; i < line.length(); i++) { if (line.charAt(i) != ‘ ‘) { outB.append(line.charAt(i)); } else { if (outB.length() > 0) { buffers.add(outB.toString()); outB = new StringBuffer(); } } } if (outB.length() > 0) { buffers.add(outB.toString()); } tokens = buffers.toArray(new String[] {}); j = new Journal(timestamp.getTime()); timestamp.add(Calendar.SECOND, interval); try { j.s0c = toDouble(tokens[0]); j.s1c = toDouble(tokens[1]); j.s0u = toDouble(tokens[2]); j.s1u = toDouble(tokens[3]); j.ec = toDouble(tokens[4]); j.eu = toDouble(tokens[5]); j.oc = toDouble(tokens[6]); j.ou = toDouble(tokens[7]); j.pc = toDouble(tokens[8]); j.pu = toDouble(tokens[9]); j.ygc = toInt(tokens[10]); j.ygct = toDouble(tokens[11]); j.fgc = toInt(tokens[12]); j.fgct = toDouble(tokens[13]); j.gct = toDouble(tokens[14]); if (journalCount == 0) { j.fgctDiff = j.fgct; j.ygctDiff = j.ygct; j.gctDiff = j.gct; } else { Journal jBack = result.get(journalCount – 1); j.fgctDiff = j.fgct – jBack.fgct; j.ygctDiff = j.ygct – jBack.ygct; j.gctDiff = j.gct – jBack.gct; } // ////////////////// // 追加 // ////////////////// result.add(j); journalCount++; } catch (Exception e) { System.err.println(“line ” + row + ” (tokens=” + tokens.length + “) : ” + line); for (int i = 0; i < tokens.length; i++) { System.err.println(“token[” + i + “]=[” + tokens[i] + “]”); } e.printStackTrace(); System.exit(1); } } } br.close(); System.out.println(“completed. (” + (System.currentTimeMillis() – started) + “ms, ” + row + “rows)”); return result; } public static double toDouble(String v) { return Double.parseDouble(v); } public static int toInt(String v) { return Integer.parseInt(v); } public static List<String> getFiles(String path) { List<String> files = new ArrayList<String>(); File f = new File(path); File f2 = null; for (String name : f.list()) { f2 = new File(path + “\\” + name); if (f2.isDirectory()) { files.addAll(getFiles(f2.getAbsolutePath())); } else { files.add(f2.getAbsolutePath()); } } return files; } public static void main(String[] args) throws Exception { File file = new File(“C:\\\\jstat.txt”); String workDir = file.getParentFile().getAbsolutePath() + “\\” + file.getName() + “.output”; File outputDir = new File(workDir); if (!outputDir.exists()) { if (!outputDir.mkdirs()) { System.err.println(workDir + ” could not created.”); System.exit(1); } } List<Journal> alls = new ArrayList<Journal>(); List<String> files = new ArrayList<String>(); if (file.isDirectory()) { files.addAll(getFiles(file.getAbsolutePath())); } else { files.add(file.getAbsolutePath()); } for (String filename : files) { alls.addAll(readFile(filename)); } // //////////////////////////////////////////////////////////////////// // Graph // //////////////////////////////////////////////////////////////////// ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()); CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new DateAxis(“Time”)); plot.setBackgroundPaint(Color.white); plot.setDomainGridlinePaint(Color.black); plot.setRangeGridlinePaint(Color.black); plot.setForegroundAlpha(0.5f); plot.setBackgroundAlpha(0.5f); TimeSeriesCollection dataset1 = new TimeSeriesCollection(); dataset1.addSeries(new TimeSeries(“OC”, Millisecond.class)); dataset1.addSeries(new TimeSeries(“OU”, Millisecond.class)); dataset1.addSeries(new TimeSeries(“PC”, Millisecond.class)); dataset1.addSeries(new TimeSeries(“PU”, Millisecond.class)); dataset1.addSeries(new TimeSeries(“EC”, Millisecond.class)); dataset1.addSeries(new TimeSeries(“EU”, Millisecond.class)); TimeSeriesCollection dataset2 = new TimeSeriesCollection(); dataset2.addSeries(new TimeSeries(“FGC”, Millisecond.class)); dataset2.addSeries(new TimeSeries(“YGC”, Millisecond.class)); TimeSeriesCollection dataset3 = new TimeSeriesCollection(); dataset3.addSeries(new TimeSeries(“FGCT”, Millisecond.class)); dataset3.addSeries(new TimeSeries(“YGCT”, Millisecond.class)); NumberAxis axisY1 = new NumberAxis(“Kbyte”); // axisY.setAutoRangeIncludesZero(false); // axisY.setRange(0, 60000); NumberAxis axisY2 = new NumberAxis(“GC Count”); NumberAxis axisY3 = new NumberAxis(“GC Time (second)”); XYPlot subplot1 = new XYPlot(dataset1, null, axisY1, new StandardXYItemRenderer()); subplot1.setBackgroundPaint(Color.white); subplot1.setDomainGridlinePaint(Color.DARK_GRAY); subplot1.setRangeGridlinePaint(Color.DARK_GRAY); plot.add(subplot1); subplot1.getRenderer().setSeriesStroke(0, new java.awt.BasicStroke(2)); subplot1.getRenderer().setSeriesStroke(1, new java.awt.BasicStroke(2)); subplot1.getRenderer().setSeriesStroke(2, new java.awt.BasicStroke(2)); subplot1.getRenderer().setSeriesStroke(3, new java.awt.BasicStroke(2)); subplot1.getRenderer().setSeriesStroke(4, new java.awt.BasicStroke(2)); subplot1.getRenderer().setSeriesStroke(5, new java.awt.BasicStroke(2)); XYPlot subplot2 = new XYPlot(dataset2, null, axisY2, new StandardXYItemRenderer()); subplot2.setBackgroundPaint(Color.white); subplot2.setDomainGridlinePaint(Color.DARK_GRAY); subplot2.setRangeGridlinePaint(Color.DARK_GRAY); plot.add(subplot2); subplot2.getRenderer().setSeriesStroke(0, new java.awt.BasicStroke(2)); subplot2.getRenderer().setSeriesStroke(1, new java.awt.BasicStroke(2)); XYPlot subplot3 = new XYPlot(dataset3, null, axisY3, new StandardXYItemRenderer()); subplot3.setBackgroundPaint(Color.white); subplot3.setDomainGridlinePaint(Color.DARK_GRAY); subplot3.setRangeGridlinePaint(Color.DARK_GRAY); plot.add(subplot3); subplot3.getRenderer().setSeriesStroke(0, new java.awt.BasicStroke(2)); subplot3.getRenderer().setSeriesStroke(1, new java.awt.BasicStroke(2)); for (Journal j : alls) { try { dataset1.getSeries(0).add(new Millisecond(j.timestamp), j.oc); dataset1.getSeries(1).add(new Millisecond(j.timestamp), j.ou); dataset1.getSeries(2).add(new Millisecond(j.timestamp), j.pc); dataset1.getSeries(3).add(new Millisecond(j.timestamp), j.pu); dataset1.getSeries(4).add(new Millisecond(j.timestamp), j.ec); dataset1.getSeries(5).add(new Millisecond(j.timestamp), j.eu); dataset2.getSeries(0).add(new Millisecond(j.timestamp), j.fgc); dataset2.getSeries(1).add(new Millisecond(j.timestamp), j.ygc); dataset3.getSeries(0).add(new Millisecond(j.timestamp), j.fgctDiff); dataset3.getSeries(1).add(new Millisecond(j.timestamp), j.ygctDiff); } catch (org.jfree.data.general.SeriesException duplicated) { } } // SAVE GRAPH JFreeChart chart = new JFreeChart(“JVM Heap Memory”, plot); chart.setBorderPaint(Color.black); chart.setBorderVisible(true); chart.setBackgroundPaint(Color.white); chart.getTitle().setFont(new Font(“Meiryo UI”, 1, 22)); // 凡例 LegendTitle legend = chart.getLegend(); legend.setBackgroundPaint(Color.white); legend.setMargin(10d, 10d, 10d, 10d); legend.setItemFont(new Font(“Meiryo UI”, 1, 12)); // save File pngFile = new File(workDir + “\\jstat.png”); try { ChartUtilities.saveChartAsPNG(pngFile, chart, 700, 750); } catch (IOException e) { e.printStackTrace(); } } } ]]>

Related Posts