|
| 1 | +package com.tagtraum.perf.gcviewer.imp; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.closeTo; |
| 4 | +import static org.hamcrest.Matchers.is; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.IOException; |
| 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; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +public class TestDataReaderGo { |
| 20 | + |
| 21 | + @Test |
| 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 | + |
| 28 | + String gcLog = "" |
| 29 | + + "gc starting...\n" // Such a line is not produced by the Go GC; it is just for testing |
| 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" |
| 31 | + + "a line unrelated to GC logging\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"; |
| 34 | + ByteArrayInputStream in = new ByteArrayInputStream(gcLog.getBytes("US-ASCII")); |
| 35 | + DataReader reader = new DataReaderGo(gcResource, in); |
| 36 | + GCModel model = reader.read(); |
| 37 | + |
| 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)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void exampleLog() throws IOException { |
| 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); |
| 58 | + GCModel model = reader.read(); |
| 59 | + |
| 60 | + assertThat("warnings", handler.getCount(), is(0)); |
| 61 | + assertThat("size", model.size(), is(635)); |
| 62 | + } |
| 63 | +} |
0 commit comments