Skip to content

Commit 7a5b5a3

Browse files
committed
Parse logfile series from commandline (#61)
Commandline support for parsing logfile series has been added. Syntactically this is close to existing commandline arguments. Some additional refactoring was done to have a unified handling of the various types of resources that can be used.
1 parent 1895bd3 commit 7a5b5a3

File tree

5 files changed

+84
-39
lines changed

5 files changed

+84
-39
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ is free software released under GNU LGPL.
1111
You can start GCViewer (gui) by simply double-clicking on gcviewer-1.3x.jar
1212
or running java -jar gcviewer-1.3x.jar (it needs a java 1.8 vm to run).
1313

14-
For a cmdline based report summary just type:
15-
java -jar gcviewer-1.3x.jar gc.log summary.csv [chart.png] [-t PLAIN|CSV|CSV_TS|SIMPLE|SUMMARY]
16-
17-
to generate a report (including optional chart image file).
14+
For a cmdline based report summary just type the following to generate a report (including optional chart image file):
15+
`java -jar gcviewer-1.3x.jar gc.log summary.csv [chart.png] [-t PLAIN|CSV|CSV_TS|SIMPLE|SUMMARY]`
16+
When logfile rotation (-XX:+UseGCLogFileRotation) is enabled, the logfiles can be read at once:
17+
`java -jar gcviewer-1.3x.jar gc.log.0;gc.log.1;gc.log.2;gc.log.current summary.csv [chart.png] [-t PLAIN|CSV|CSV_TS|SIMPLE|SUMMARY]`
1818

1919

2020
Supported verbose:gc formats are:

src/main/java/com/tagtraum/perf/gcviewer/GCViewer.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.tagtraum.perf.gcviewer.imp.DataReaderException;
88
import com.tagtraum.perf.gcviewer.imp.DataReaderFacade;
99
import com.tagtraum.perf.gcviewer.model.GCModel;
10-
import com.tagtraum.perf.gcviewer.model.GcResourceFile;
10+
import com.tagtraum.perf.gcviewer.model.GCResource;
1111
import com.tagtraum.perf.gcviewer.view.SimpleChartRenderer;
1212

1313
import java.io.File;
@@ -27,7 +27,7 @@ public class GCViewer {
2727
private static final int EXIT_EXPORT_FAILED = -1;
2828
private static final int EXIT_ARGS_PARSE_FAILED = -2;
2929

30-
public static void main(final String[] args) throws InvocationTargetException, InterruptedException {
30+
public static void main(final String[] args) throws InvocationTargetException, InterruptedException {
3131
new GCViewer().doMain(args);
3232
}
3333

@@ -47,14 +47,14 @@ public void doMain(String[] args) throws InvocationTargetException, InterruptedE
4747
}
4848
else if (argsParser.getArgumentCount() >= 2) {
4949
LOGGER.info("GCViewer command line mode");
50-
String gcfile = argsParser.getGcfile();
50+
GCResource gcResource = argsParser.getGcResource();
5151
String summaryFilePath = argsParser.getSummaryFilePath();
5252
String chartFilePath = argsParser.getChartFilePath();
5353
DataWriterType type = argsParser.getType();
5454

5555
//export summary:
5656
try {
57-
export(gcfile, summaryFilePath, chartFilePath, type);
57+
export(gcResource, summaryFilePath, chartFilePath, type);
5858
LOGGER.info("export completed successfully");
5959
System.exit(EXIT_OK);
6060
}
@@ -64,22 +64,22 @@ else if (argsParser.getArgumentCount() >= 2) {
6464
}
6565
}
6666
else {
67-
new GCViewerGuiController().startGui(argsParser.getArgumentCount() == 1 ? argsParser.getGcfile() : null);
67+
new GCViewerGuiController().startGui(argsParser.getArgumentCount() == 1 ? argsParser.getGcResource() : null);
6868
}
6969
}
7070

71-
private void export(String gcFilename, String summaryFilePath, String chartFilePath, DataWriterType type)
71+
private void export(GCResource gcResource, String summaryFilePath, String chartFilePath, DataWriterType type)
7272
throws IOException, DataReaderException {
7373

7474
DataReaderFacade dataReaderFacade = new DataReaderFacade();
75-
GCModel model = dataReaderFacade.loadModel(new GcResourceFile(gcFilename));
75+
GCModel model = dataReaderFacade.loadModel(gcResource);
7676

7777
exportType(model, summaryFilePath, type);
7878
if (chartFilePath != null)
7979
renderChart(model, chartFilePath);
8080
}
8181

82-
private void exportType(GCModel model, String summaryFilePath, DataWriterType type) throws IOException {
82+
private void exportType(GCModel model, String summaryFilePath, DataWriterType type) throws IOException {
8383
try (DataWriter summaryWriter = DataWriterFactory.getDataWriter(new File(summaryFilePath), type)) {
8484
summaryWriter.write(model);
8585
}
@@ -90,13 +90,16 @@ private void renderChart(GCModel model, String chartFilePath) throws IOException
9090
renderer.render(model, new FileOutputStream(new File(chartFilePath)));
9191
}
9292

93-
private static void usage() {
94-
System.out.println("Welcome to GCViewer with cmdline");
93+
private static void usage() {
94+
System.out.println("Welcome to GCViewer with cmdline");
9595
System.out.println("java -jar gcviewer.jar [<gc-log-file|url>] -> opens gui and loads given file");
96+
System.out.println("java -jar gcviewer.jar [<gc-log-file|url>];[<gc-log-file|url>];[...] -> opens gui and loads given files as series of rotated logfiles");
9697
System.out.println("java -jar gcviewer.jar [<gc-log-file>] [<export.csv>] -> cmdline: writes report to <export.csv>");
97-
System.out.println("java -jar gcviewer.jar [<gc-log-file>] [<export.csv>] [<chart.png>] " +
98-
"-> cmdline: writes report to <export.csv> and renders gc chart to <chart.png>");
99-
System.out.println("java -jar gcviewer.jar [<gc-log-file>] [<export.csv>] [<chart.png>] [-t <SUMMARY, CSV, CSV_TS, PLAIN, SIMPLE>]");
98+
System.out.println("java -jar gcviewer.jar [<gc-log-file|url>];[<gc-log-file|url>];[...] [<export.csv>] -> cmdline: loads given files as series of rotated logfiles and writes report to <export.csv>");
99+
System.out.println("java -jar gcviewer.jar [<gc-log-file>] [<export.csv>] [<chart.png>] -> cmdline: writes report to <export.csv> and renders gc chart to <chart.png>");
100+
System.out.println("java -jar gcviewer.jar [<gc-log-file|url>];[<gc-log-file|url>];[...] [<export.csv>] [<chart.png>] -> cmdline: loads given files as series of rotated logfiles and writes report to <export.csv> and renders gc chart to <chart.png>");
101+
System.out.println("java -jar gcviewer.jar [<gc-log-file|url>] [<export.csv>] [<chart.png>] [-t <SUMMARY, CSV, CSV_TS, PLAIN, SIMPLE>]");
102+
System.out.println("java -jar gcviewer.jar [<gc-log-file|url>];[<gc-log-file|url>];[...] [<export.csv>] [<chart.png>] [-t <SUMMARY, CSV, CSV_TS, PLAIN, SIMPLE>]");
100103
}
101104

102105
}

src/main/java/com/tagtraum/perf/gcviewer/GCViewerArgsParser.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.tagtraum.perf.gcviewer;
22

3+
import com.tagtraum.perf.gcviewer.exp.DataWriterType;
4+
import com.tagtraum.perf.gcviewer.model.GCResource;
5+
import com.tagtraum.perf.gcviewer.model.GcResourceFile;
6+
import com.tagtraum.perf.gcviewer.model.GcResourceSeries;
7+
38
import java.util.ArrayList;
49
import java.util.Arrays;
510
import java.util.List;
6-
7-
import com.tagtraum.perf.gcviewer.exp.DataWriterType;
11+
import java.util.stream.Collectors;
812

913
/**
1014
* Parser for commandline arguments.
@@ -18,7 +22,7 @@ public class GCViewerArgsParser {
1822

1923
private int argumentCount;
2024
private String chartFilePath;
21-
private String gcfile;
25+
private String gcFile;
2226
private String summaryFilePath;
2327
private DataWriterType type = DataWriterType.SUMMARY;
2428

@@ -29,9 +33,19 @@ public int getArgumentCount() {
2933
public String getChartFilePath() {
3034
return chartFilePath;
3135
}
32-
33-
public String getGcfile() {
34-
return gcfile;
36+
37+
public GCResource getGcResource() {
38+
List<String> files = Arrays.asList(gcFile.split(";"));
39+
List<GCResource> resources = files.stream().map(GcResourceFile::new).collect(Collectors.toList());
40+
if (resources.isEmpty())
41+
throw new IllegalStateException("Found no valid resource!");
42+
43+
if (resources.size() == 1) {
44+
return resources.get(0);
45+
}
46+
else {
47+
return new GcResourceSeries(resources);
48+
}
3549
}
3650

3751
public String getSummaryFilePath() {
@@ -65,7 +79,7 @@ else if (typeIdx != -1) {
6579
}
6680

6781
argumentCount = argsList.size();
68-
gcfile = safeGetArgument(argsList, ARG_POS_GCFILE);
82+
gcFile = safeGetArgument(argsList, ARG_POS_GCFILE);
6983
summaryFilePath = safeGetArgument(argsList, ARG_POS_SUMMARY_FILE);
7084
chartFilePath = safeGetArgument(argsList, ARG_POS_CHART_FILE);
7185
}

src/main/java/com/tagtraum/perf/gcviewer/ctrl/impl/GCViewerGuiController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.tagtraum.perf.gcviewer.ctrl.GCModelLoaderController;
44
import com.tagtraum.perf.gcviewer.ctrl.action.OpenFile;
5-
import com.tagtraum.perf.gcviewer.model.GcResourceFile;
5+
import com.tagtraum.perf.gcviewer.model.GCResource;
66
import com.tagtraum.perf.gcviewer.view.ActionCommands;
77
import com.tagtraum.perf.gcviewer.view.GCDocument;
88
import com.tagtraum.perf.gcviewer.view.GCViewerGui;
@@ -107,16 +107,16 @@ private GCPreferences copyPreferencesFromGui(GCViewerGui gui) {
107107
/**
108108
* Start graphical user interface and load a log file (resourceName - if not <code>null</code>).
109109
*
110-
* @param resourceName log file to be loaded at startup or <code>null</code>
110+
* @param gcResource {@link GCResource} to be loaded at startup or <code>null</code>
111111
* @throws InvocationTargetException Some problem trying to start the gui
112112
* @throws InterruptedException Some problem trying to start the gui
113113
*/
114-
public void startGui(final String resourceName) throws InvocationTargetException, InterruptedException {
114+
public void startGui(final GCResource gcResource) throws InvocationTargetException, InterruptedException {
115115
final GCViewerGui gcViewerGui = new GCViewerGui();
116116
final GCModelLoaderController modelLoaderController = new GCModelLoaderControllerImpl(gcViewerGui);
117117

118118
Runnable guiStarter = new Runnable() {
119-
119+
120120
@Override
121121
public void run() {
122122
new GCViewerGuiBuilder().initGCViewerGui(gcViewerGui, modelLoaderController);
@@ -129,12 +129,12 @@ public void run() {
129129

130130
SwingUtilities.invokeAndWait(guiStarter);
131131

132-
if (resourceName != null) {
132+
if (gcResource != null) {
133133
Runnable resourceLoader = new Runnable() {
134134

135135
@Override
136136
public void run() {
137-
modelLoaderController.open(new GcResourceFile(resourceName));
137+
modelLoaderController.open(gcResource);
138138
}
139139

140140
};

src/test/java/com/tagtraum/perf/gcviewer/TestGCViewerArgsParser.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.tagtraum.perf.gcviewer;
22

3-
import static org.hamcrest.Matchers.startsWith;
4-
import static org.junit.Assert.assertEquals;
5-
import static org.junit.Assert.assertThat;
6-
import static org.junit.Assert.fail;
7-
3+
import com.tagtraum.perf.gcviewer.exp.DataWriterType;
4+
import com.tagtraum.perf.gcviewer.model.GCResource;
5+
import com.tagtraum.perf.gcviewer.model.GcResourceFile;
6+
import com.tagtraum.perf.gcviewer.model.GcResourceSeries;
87
import org.junit.Test;
98

10-
import com.tagtraum.perf.gcviewer.exp.DataWriterType;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
import static org.hamcrest.Matchers.startsWith;
13+
import static org.junit.Assert.*;
1114

1215
/**
1316
* Tests the class {@link com.tagtraum.perf.gcviewer.GCViewerArgsParser}
@@ -35,7 +38,19 @@ public void onlyGCLog() throws Exception {
3538
gcViewerArgsParser.parseArguments(args);
3639

3740
assertEquals(gcViewerArgsParser.getArgumentCount(), 1);
38-
assertEquals(gcViewerArgsParser.getGcfile(), "some_gc.log");
41+
assertEquals(gcViewerArgsParser.getGcResource(), new GcResourceFile("some_gc.log"));
42+
assertEquals(gcViewerArgsParser.getType(), DataWriterType.SUMMARY);
43+
}
44+
45+
@Test
46+
public void onlyGcLogSeries() throws Exception {
47+
String[] args = {"some_gc.log.0;some_gc.log.1;some_gc.log.2"};
48+
GCViewerArgsParser gcViewerArgsParser = new GCViewerArgsParser();
49+
gcViewerArgsParser.parseArguments(args);
50+
51+
assertEquals(gcViewerArgsParser.getArgumentCount(), 1);
52+
List<GCResource> resources = Arrays.asList(new GcResourceFile("some_gc.log.0"), new GcResourceFile("some_gc.log.1"), new GcResourceFile("some_gc.log.2"));
53+
assertEquals(gcViewerArgsParser.getGcResource(), new GcResourceSeries(resources));
3954
assertEquals(gcViewerArgsParser.getType(), DataWriterType.SUMMARY);
4055
}
4156

@@ -46,7 +61,20 @@ public void gcAndExportFile() throws Exception {
4661
gcViewerArgsParser.parseArguments(args);
4762

4863
assertEquals(gcViewerArgsParser.getArgumentCount(), 2);
49-
assertEquals(gcViewerArgsParser.getGcfile(), "some_gc.log");
64+
assertEquals(gcViewerArgsParser.getGcResource(), new GcResourceFile("some_gc.log"));
65+
assertEquals(gcViewerArgsParser.getSummaryFilePath(), "export_to.csv");
66+
assertEquals(gcViewerArgsParser.getType(), DataWriterType.SUMMARY);
67+
}
68+
69+
@Test
70+
public void gcSeriesAndExportFile() throws Exception {
71+
String[] args = {"some_gc.log.0;some_gc.log.1;some_gc.log.2", "export_to.csv"};
72+
GCViewerArgsParser gcViewerArgsParser = new GCViewerArgsParser();
73+
gcViewerArgsParser.parseArguments(args);
74+
75+
assertEquals(gcViewerArgsParser.getArgumentCount(), 2);
76+
List<GCResource> resources = Arrays.asList(new GcResourceFile("some_gc.log.0"), new GcResourceFile("some_gc.log.1"), new GcResourceFile("some_gc.log.2"));
77+
assertEquals(gcViewerArgsParser.getGcResource(), new GcResourceSeries(resources));
5078
assertEquals(gcViewerArgsParser.getSummaryFilePath(), "export_to.csv");
5179
assertEquals(gcViewerArgsParser.getType(), DataWriterType.SUMMARY);
5280
}
@@ -68,7 +96,7 @@ public void allInitialArgsWithType() throws Exception {
6896
gcViewerArgsParser.parseArguments(args);
6997

7098
assertEquals(gcViewerArgsParser.getArgumentCount(), 3);
71-
assertEquals(gcViewerArgsParser.getGcfile(), "some_gc.log");
99+
assertEquals(gcViewerArgsParser.getGcResource(), new GcResourceFile("some_gc.log"));
72100
assertEquals(gcViewerArgsParser.getSummaryFilePath(), "export_to.csv");
73101
assertEquals(gcViewerArgsParser.getChartFilePath(), "the_chart.png");
74102
assertEquals(gcViewerArgsParser.getType(), DataWriterType.CSV);

0 commit comments

Comments
 (0)