changeset 2648:e2fffc5709ad

Add vm-memory TypeAdapters. Reviewed By: ebaron Review Thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023033.html
author Joshua Matsuoka <jmatsuok@redhat.com>
date Fri, 12 May 2017 09:53:33 -0400
parents 4e29550f635a
children 7184d65b1aa7
files plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatTypeAdapter.java plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmTlabStatTypeAdapter.java plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatTypeAdapterTest.java plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmTlabStatTypeAdapterTest.java
diffstat 4 files changed, 427 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatTypeAdapter.java	Fri May 12 09:53:33 2017 -0400
@@ -0,0 +1,149 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.vm.memory.common.internal;
+
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.redhat.thermostat.vm.memory.common.model.VmMemoryStat;
+
+import java.io.IOException;
+import java.util.List;
+
+import static com.redhat.thermostat.vm.memory.common.model.VmMemoryStat.Generation;
+import static com.redhat.thermostat.vm.memory.common.model.VmMemoryStat.Space;
+
+public class VmMemoryStatTypeAdapter extends TypeAdapter<List<VmMemoryStat>> {
+
+    private static final String GENERATIONS = "generations";
+    private static final String TIMESTAMP = "timestamp";
+    private static final String AGENT_ID = "agentId";
+    private static final String VM_ID = "vmId";
+    private static final String METASPACE_MAX_CAPACITY = "metaspaceMaxCapacity";
+    private static final String METASPACE_MIN_CAPACITY = "metaspaceMinCapacity";
+    private static final String METASPACE_CAPACITY = "metaspaceCapacity";
+    private static final String METASPACE_USED = "metaspaceUsed";
+    private static final String NAME = "name";
+    private static final String CAPACITY = "capacity";
+    private static final String MAX_CAPACITY = "maxCapacity";
+    private static final String SPACES = "spaces";
+    private static final String COLLECTOR = "collector";
+    private static final String INDEX = "index";
+    private static final String USED = "used";
+    private static final String TYPE_LONG = "$numberLong";
+
+    @Override
+    public List<VmMemoryStat> read(JsonReader in) throws IOException {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void write(JsonWriter out, List<VmMemoryStat> stats) throws IOException {
+        out.beginArray();
+        for (VmMemoryStat stat : stats) {
+            writeStat(out, stat);
+        }
+        out.endArray();
+    }
+
+    public void writeStat(JsonWriter out, VmMemoryStat stat) throws IOException {
+        out.beginObject();
+        out.name(AGENT_ID);
+        out.value(stat.getAgentId());
+        out.name(VM_ID);
+        out.value(stat.getVmId());
+        out.name(TIMESTAMP);
+        writeLong(out, stat.getTimeStamp());
+        out.name(METASPACE_MAX_CAPACITY);
+        writeLong(out, stat.getMetaspaceMaxCapacity());
+        out.name(METASPACE_MIN_CAPACITY);
+        writeLong(out, stat.getMetaspaceMinCapacity());
+        out.name(METASPACE_CAPACITY);
+        writeLong(out, stat.getMetaspaceCapacity());
+        out.name(METASPACE_USED);
+        writeLong(out, stat.getMetaspaceUsed());
+        out.name(GENERATIONS);
+        out.beginArray();
+        for (Generation g : stat.getGenerations()) {
+            writeGeneration(out, g);
+        }
+        out.endArray();
+        out.endObject();
+    }
+
+    public void writeLong(JsonWriter out, long value) throws IOException {
+        // Write MongoDB representation of a Long
+        out.beginObject();
+        out.name(TYPE_LONG);
+        out.value(String.valueOf(value));
+        out.endObject();
+    }
+
+    public void writeGeneration(JsonWriter out, Generation g) throws IOException {
+        out.beginObject();
+        out.name(NAME);
+        out.value(g.getName());
+        out.name(CAPACITY);
+        writeLong(out, g.getCapacity());
+        out.name(MAX_CAPACITY);
+        writeLong(out, g.getMaxCapacity());
+        out.name(COLLECTOR);
+        out.value(g.getCollector());
+        out.name(SPACES);
+        out.beginArray();
+        for (Space s : g.getSpaces()) {
+            writeSpace(out, s);
+        }
+        out.endArray();
+        out.endObject();
+    }
+
+    public void writeSpace(JsonWriter out, Space s) throws IOException {
+        out.beginObject();
+        out.name(INDEX);
+        out.value(s.getIndex());
+        out.name(NAME);
+        out.value(s.getName());
+        out.name(CAPACITY);
+        writeLong(out, s.getCapacity());
+        out.name(MAX_CAPACITY);
+        writeLong(out, s.getMaxCapacity());
+        out.name(USED);
+        writeLong(out, s.getUsed());
+        out.endObject();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmTlabStatTypeAdapter.java	Fri May 12 09:53:33 2017 -0400
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.vm.memory.common.internal;
+
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.redhat.thermostat.vm.memory.common.model.VmTlabStat;
+
+import java.io.IOException;
+import java.util.List;
+
+public class VmTlabStatTypeAdapter extends TypeAdapter<List<VmTlabStat>> {
+
+    private static final String AGENT_ID = "agentId";
+    private static final String VM_ID = "vmId";
+    private static final String TIMESTAMP = "timeStamp";
+    private static final String ALLOC_THREADS = "allocThreads";
+    private static final String TOTAL_ALLOCATIONS = "totalAllocations";
+    private static final String REFILLS = "refills";
+    private static final String MAX_REFILLS = "maxRefills";
+    private static final String SLOW_ALLOCATIONS = "slowAllocations";
+    private static final String MAX_SLOW_ALLOCATIONS = "maxSlowAllocations";
+    private static final String GC_WASTE = "gcWaste";
+    private static final String MAX_GC_WASTE = "maxGcWaste";
+    private static final String SLOW_WASTE = "slowWaste";
+    private static final String MAX_SLOW_WASTE = "maxSlowWaste";
+    private static final String FAST_WASTE = "fastWaste";
+    private static final String MAX_FAST_WASTE = "maxFastWaste";
+    private static final String TYPE_LONG = "$numberLong";
+
+    @Override
+    public List<VmTlabStat> read(JsonReader in) throws IOException {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void write(JsonWriter out, List<VmTlabStat> stats) throws IOException {
+        out.beginArray();
+        for (VmTlabStat tlabStat : stats) {
+            writeStat(out, tlabStat);
+        }
+        out.endArray();
+    }
+
+    public void writeStat(JsonWriter out, VmTlabStat stat) throws IOException {
+        out.beginObject();
+        out.name(VM_ID);
+        out.value(stat.getVmId());
+        out.name(AGENT_ID);
+        out.value(stat.getAgentId());
+        out.name(TIMESTAMP);
+        writeLong(out, stat.getTimeStamp());
+        out.name(ALLOC_THREADS);
+        writeLong(out, stat.getTotalAllocatingThreads());
+        out.name(TOTAL_ALLOCATIONS);
+        writeLong(out, stat.getTotalAllocations());
+        out.name(REFILLS);
+        writeLong(out, stat.getTotalRefills());
+        out.name(MAX_REFILLS);
+        writeLong(out, stat.getMaxRefills());
+        out.name(SLOW_ALLOCATIONS);
+        writeLong(out, stat.getTotalSlowAllocations());
+        out.name(MAX_SLOW_ALLOCATIONS);
+        writeLong(out, stat.getMaxSlowAllocations());
+        out.name(GC_WASTE);
+        writeLong(out, stat.getTotalGcWaste());
+        out.name(MAX_GC_WASTE);
+        writeLong(out, stat.getMaxGcWaste());
+        out.name(SLOW_WASTE);
+        writeLong(out, stat.getTotalSlowWaste());
+        out.name(MAX_SLOW_WASTE);
+        writeLong(out, stat.getMaxSlowWaste());
+        out.name(FAST_WASTE);
+        writeLong(out, stat.getTotalFastWaste());
+        out.name(MAX_FAST_WASTE);
+        writeLong(out, stat.getMaxFastWaste());
+        out.endObject();
+    }
+
+    public void writeLong(JsonWriter out, long value) throws IOException {
+        // Write MongoDB representation of a Long
+        out.beginObject();
+        out.name(TYPE_LONG);
+        out.value(String.valueOf(value));
+        out.endObject();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatTypeAdapterTest.java	Fri May 12 09:53:33 2017 -0400
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.vm.memory.common.internal;
+
+import com.google.gson.GsonBuilder;
+import com.google.gson.Gson;
+import com.redhat.thermostat.vm.memory.common.model.VmMemoryStat;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static com.redhat.thermostat.vm.memory.common.model.VmMemoryStat.Generation;
+import static com.redhat.thermostat.vm.memory.common.model.VmMemoryStat.Space;
+import static org.junit.Assert.assertEquals;
+
+public class VmMemoryStatTypeAdapterTest {
+
+    @Test
+    public void testWrite() throws IOException {
+        VmMemoryStatTypeAdapter typeAdapter = new VmMemoryStatTypeAdapter();
+        VmMemoryStat stat = new VmMemoryStat();
+        stat.setTimeStamp(100l);
+        stat.setVmId("VM-1");
+        stat.setAgentId("AGENT-1");
+        stat.setMetaspaceCapacity(2000l);
+        stat.setMetaspaceMaxCapacity(4096l);
+        stat.setMetaspaceMinCapacity(2048l);
+        stat.setMetaspaceUsed(3000l);
+        Generation[] gens = new Generation[1];
+        Generation gen1 = new Generation();
+        gen1.setCapacity(1002l);
+        gen1.setCollector("Collector 1");
+        gen1.setMaxCapacity(2048l);
+        gen1.setName("Name");
+        Space[] spaces = new Space[1];
+        Space space = new Space();
+        space.setName("Space Name");
+        space.setMaxCapacity(1024l);
+        space.setCapacity(500l);
+        space.setIndex(1);
+        space.setUsed(400l);
+        spaces[0] = space;
+        gen1.setSpaces(spaces);
+        gens[0] = gen1;
+        stat.setGenerations(gens);
+        assertEquals("[{\"agentId\":\"AGENT-1\",\"vmId\":\"VM-1\",\"timestamp\":{\"$numberLong\":\"100\"},\"metaspaceMaxCapacity\":{\"$numberLong\":\"4096\"},\"metaspaceMinCapacity\":{\"$numberLong\":\"2048\"},\"metaspaceCapacity\":{\"$numberLong\":\"2000\"},\"metaspaceUsed\":{\"$numberLong\":\"3000\"},\"generations\":[{\"name\":\"Name\",\"capacity\":{\"$numberLong\":\"1002\"},\"maxCapacity\":{\"$numberLong\":\"2048\"},\"collector\":\"Collector 1\",\"spaces\":[{\"index\":1,\"name\":\"Space Name\",\"capacity\":{\"$numberLong\":\"500\"},\"maxCapacity\":{\"$numberLong\":\"1024\"},\"used\":{\"$numberLong\":\"400\"}}]}]}]", typeAdapter.toJson(Arrays.asList(stat)));
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmTlabStatTypeAdapterTest.java	Fri May 12 09:53:33 2017 -0400
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2012-2017 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.vm.memory.common.internal;
+
+import com.google.gson.GsonBuilder;
+import com.google.gson.Gson;
+import com.redhat.thermostat.vm.memory.common.model.VmTlabStat;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+
+public class VmTlabStatTypeAdapterTest {
+
+    @Test
+    public void testWrite() throws IOException {
+        VmTlabStatTypeAdapter typeAdapter = new VmTlabStatTypeAdapter();
+        VmTlabStat stat = new VmTlabStat();
+        stat.setAgentId("AGENT-1");
+        stat.setVmId("VM-1");
+        stat.setTimeStamp(1000l);
+        stat.setTotalAllocatingThreads(10l);
+        stat.setTotalAllocations(1342l);
+        stat.setTotalRefills(58l);
+        stat.setMaxRefills(90l);
+        stat.setTotalSlowAllocations(343l);
+        stat.setMaxSlowAllocations(989l);
+        stat.setTotalGcWaste(788l);
+        stat.setMaxGcWaste(992l);
+        stat.setTotalSlowWaste(899l);
+        stat.setMaxSlowWaste(634l);
+        stat.setTotalFastWaste(678l);
+        stat.setMaxFastWaste(333l);
+        assertEquals("[{\"vmId\":\"VM-1\",\"agentId\":\"AGENT-1\",\"timeStamp\":{\"$numberLong\":\"1000\"},\"allocThreads\":{\"$numberLong\":\"10\"},\"totalAllocations\":{\"$numberLong\":\"1342\"},\"refills\":{\"$numberLong\":\"58\"},\"maxRefills\":{\"$numberLong\":\"90\"},\"slowAllocations\":{\"$numberLong\":\"343\"},\"maxSlowAllocations\":{\"$numberLong\":\"989\"},\"gcWaste\":{\"$numberLong\":\"788\"},\"maxGcWaste\":{\"$numberLong\":\"992\"},\"slowWaste\":{\"$numberLong\":\"899\"},\"maxSlowWaste\":{\"$numberLong\":\"634\"},\"fastWaste\":{\"$numberLong\":\"678\"},\"maxFastWaste\":{\"$numberLong\":\"333\"}}]", typeAdapter.toJson(Arrays.asList(stat)));
+    }
+
+}