|
1 | 1 | package com.tagtraum.perf.gcviewer.imp; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertEquals; |
| 3 | +import static org.hamcrest.Matchers.closeTo; |
| 4 | +import static org.hamcrest.Matchers.is; |
| 5 | +import static org.junit.Assert.assertThat; |
4 | 6 |
|
5 | | -import com.tagtraum.perf.gcviewer.UnittestHelper; |
6 | | -import com.tagtraum.perf.gcviewer.model.GCModel; |
7 | | -import com.tagtraum.perf.gcviewer.model.GcResourceFile; |
8 | 7 | import java.io.ByteArrayInputStream; |
9 | 8 | import java.io.IOException; |
10 | 9 | import java.io.InputStream; |
| 10 | +import java.util.logging.Level; |
| 11 | + |
| 12 | +import com.tagtraum.perf.gcviewer.UnittestHelper; |
| 13 | +import com.tagtraum.perf.gcviewer.model.AbstractGCEvent; |
| 14 | +import com.tagtraum.perf.gcviewer.model.GCModel; |
| 15 | +import com.tagtraum.perf.gcviewer.model.GCResource; |
| 16 | +import com.tagtraum.perf.gcviewer.model.GcResourceFile; |
11 | 17 | import org.junit.Test; |
12 | 18 |
|
13 | 19 | public class TestDataReaderGo { |
14 | 20 |
|
15 | 21 | @Test |
16 | 22 | public void test() throws IOException { |
| 23 | + TestLogHandler handler = new TestLogHandler(); |
| 24 | + handler.setLevel(Level.WARNING); |
| 25 | + GCResource gcResource = new GcResourceFile("byteArray"); |
| 26 | + gcResource.getLogger().addHandler(handler); |
| 27 | + |
17 | 28 | String gcLog = "" |
18 | 29 | + "gc starting...\n" // Such a line is not produced by the Go GC; it is just for testing |
19 | | - + "gc 1 @997.597s 3%: 68+0.36+0.51 ms clock, 205+0/16/89+1.5 ms cpu, 84->84->42 MB, 86 MB goal, 3 P\n" |
| 30 | + + "gc 1 @0.058s 0%: 0+1.9+0 ms clock, 0+0.94/1.9/2.9+0 ms cpu, 4->5->1 MB, 5 MB goal, 4 P\n" |
20 | 31 | + "a line unrelated to GC logging\n" |
21 | | - + "gc 2 @997.597s 3%: 68+0.36+0.51 ms clock, 205+0/16/89+1.5 ms cpu, 11111111111111111111111111111111111->84->42 MB, 86 MB goal, 3 P\n" |
22 | | - + "gc 3 @997.597s 3%: 68+0.36+0.51 ms clock, 205+0/16/89+1.5 ms cpu, 84->84->42 MB, 86 MB goal, 3 P\n"; |
| 32 | + + "gc 2 @0.073s 3%: 68+0.36+0.51 ms clock, 205+0/16/89+1.5 ms cpu, 11111111111111111111111111111111111->84->42 MB, 86 MB goal, 3 P\n" |
| 33 | + + "gc 58 @17.837s 0%: 0.48+17+0 ms clock, 1.9+9.3/7.9/15+0 ms cpu, 30->30->15 MB, 31 MB goal, 4 P\n"; |
23 | 34 | ByteArrayInputStream in = new ByteArrayInputStream(gcLog.getBytes("US-ASCII")); |
24 | | - DataReader reader = new DataReaderGo(new GcResourceFile("byteArray"), in); |
| 35 | + DataReader reader = new DataReaderGo(gcResource, in); |
25 | 36 | GCModel model = reader.read(); |
26 | 37 |
|
27 | | - assertEquals(2, model.size()); |
| 38 | + assertThat("gc 2 -> warning", handler.getCount(), is(1)); |
| 39 | + assertThat("size", model.size(), is(2)); |
| 40 | + |
| 41 | + AbstractGCEvent<?> event1 = model.get(0); |
| 42 | + assertThat("timestamp", event1.getTimestamp(), closeTo(0.058, 0.0001)); |
| 43 | + assertThat("pause", event1.getPause(), closeTo(0 + 0, 0.1)); |
| 44 | + assertThat("preused", event1.getPreUsed(), is(4096)); |
| 45 | + assertThat("postused", event1.getPostUsed(), is(1024)); |
| 46 | + assertThat("heap", event1.getTotal(), is(5120)); |
28 | 47 | } |
29 | 48 |
|
30 | 49 | @Test |
31 | 50 | public void exampleLog() throws IOException { |
32 | | - String fileName = "go1.9.txt"; |
33 | | - InputStream in = UnittestHelper.getResourceAsStream(UnittestHelper.FOLDER.GO, fileName); |
34 | | - DataReader reader = new DataReaderGo(new GcResourceFile(fileName), in); |
| 51 | + TestLogHandler handler = new TestLogHandler(); |
| 52 | + handler.setLevel(Level.WARNING); |
| 53 | + GCResource gcResource = new GcResourceFile("go1.9.txt"); |
| 54 | + gcResource.getLogger().addHandler(handler); |
| 55 | + |
| 56 | + InputStream in = UnittestHelper.getResourceAsStream(UnittestHelper.FOLDER.GO, gcResource.getResourceName()); |
| 57 | + DataReader reader = new DataReaderGo(gcResource, in); |
35 | 58 | GCModel model = reader.read(); |
36 | 59 |
|
37 | | - assertEquals(635, model.size()); |
| 60 | + assertThat("warnings", handler.getCount(), is(0)); |
| 61 | + assertThat("size", model.size(), is(635)); |
38 | 62 | } |
39 | 63 | } |
0 commit comments