|
| 1 | +package com.tagtraum.perf.gcviewer.exp; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertThat; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.MalformedURLException; |
| 8 | +import java.net.URL; |
| 9 | + |
| 10 | +import com.tagtraum.perf.gcviewer.exp.impl.SummaryDataWriter; |
| 11 | +import com.tagtraum.perf.gcviewer.model.AbstractGCEvent.Type; |
| 12 | +import com.tagtraum.perf.gcviewer.model.GCEvent; |
| 13 | +import com.tagtraum.perf.gcviewer.model.GCModel; |
| 14 | +import org.hamcrest.Matchers; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test implementation of {@link SummaryDataWriter} |
| 19 | + * |
| 20 | + * <p>hint: don't use memory numbers > 999, because they are not formatted the same on all platforms -> unstable tests</p> |
| 21 | + */ |
| 22 | +public class SummaryDataWriterTest { |
| 23 | + |
| 24 | + private GCModel createGcModel() throws MalformedURLException { |
| 25 | + GCModel model = new GCModel(); |
| 26 | + model.setURL(new URL("file", "localhost", "test-file")); |
| 27 | + |
| 28 | + model.add(new GCEvent(0.1, 996, 768, 999, 0.3, Type.GC)); |
| 29 | + model.add(new GCEvent(0.2, 996, 424, 997, 0.4, Type.GC)); |
| 30 | + model.add(new GCEvent(0.3, 612, 512, 998, 0.3, Type.GC)); |
| 31 | + model.add(new GCEvent(0.4, 816, 768, 999, 0.3, Type.GC)); |
| 32 | + |
| 33 | + return model; |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testWriteForEmptyModel() throws IOException { |
| 38 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 39 | + SummaryDataWriter objectUnderTest = new SummaryDataWriter(output); |
| 40 | + GCModel model = new GCModel(); |
| 41 | + model.setURL(new URL("file", "localhost", "test-file")); |
| 42 | + |
| 43 | + objectUnderTest.write(model); |
| 44 | + |
| 45 | + String csv = output.toString(); |
| 46 | + |
| 47 | + assertThat("totalTenuredAllocMax", csv, Matchers.containsString("totalTenuredAllocMax; n/a; M")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testWrite() throws IOException { |
| 52 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 53 | + SummaryDataWriter objectUnderTest = new SummaryDataWriter(output); |
| 54 | + |
| 55 | + objectUnderTest.write(createGcModel()); |
| 56 | + |
| 57 | + String csv = output.toString(); |
| 58 | + |
| 59 | + assertThat("totalHeapAllocMax", csv, Matchers.containsString("totalHeapAllocMax; 999; K")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testWriteWithFullGc() throws IOException { |
| 64 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 65 | + SummaryDataWriter objectUnderTest = new SummaryDataWriter(output); |
| 66 | + |
| 67 | + GCModel model = createGcModel(); |
| 68 | + model.add(new GCEvent(0.5, 999, 724, 999, 0.8, Type.FULL_GC)); |
| 69 | + |
| 70 | + objectUnderTest.write(model); |
| 71 | + |
| 72 | + String csv = output.toString(); |
| 73 | + |
| 74 | + assertThat("totalHeapAllocMax", csv, Matchers.containsString("avgfootprintAfterFullGC; 724; K")); |
| 75 | + } |
| 76 | +} |
0 commit comments