changeset 2667:455e0ad2aedd

Add NumaNodeStat TypeAdapter Reviewed-By: ebaron Review-Thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023243.html
author Joshua Matsuoka <jmatsuok@redhat.com>
date Thu, 25 May 2017 14:07:06 -0400
parents b87ce89c3c79
children a242cbc8c2ef
files plugins/numa/common/pom.xml plugins/numa/common/src/main/java/com/redhat/thermostat/numa/common/internal/NumaNodeStatTypeAdapter.java plugins/numa/common/src/test/java/com/redhat/thermostat/numa/common/internal/NumaNodeStatTypeAdapterTest.java
diffstat 3 files changed, 174 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/numa/common/pom.xml	Thu May 25 14:06:31 2017 -0400
+++ b/plugins/numa/common/pom.xml	Thu May 25 14:07:06 2017 -0400
@@ -107,5 +107,10 @@
       <version>${project.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.google.code.gson</groupId>
+      <artifactId>gson</artifactId>
+      <version>${gson.version}</version>
+    </dependency>
   </dependencies>
 </project>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/numa/common/src/main/java/com/redhat/thermostat/numa/common/internal/NumaNodeStatTypeAdapter.java	Thu May 25 14:07:06 2017 -0400
@@ -0,0 +1,89 @@
+/*
+ * 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.numa.common.internal;
+
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.redhat.thermostat.numa.common.NumaNodeStat;
+
+import java.io.IOException;
+
+public class NumaNodeStatTypeAdapter extends TypeAdapter<NumaNodeStat> {
+
+    private static final String TYPE_LONG = "$numberLong";
+    private static final String NUMA_HIT = "numaHit";
+    private static final String NUMA_MISS = "numaMiss";
+    private static final String NUMA_FOREIGN = "numaForeign";
+    private static final String INTERLEAVE_HIT = "interleaveHit";
+    private static final String LOCAL_NODE = "localNode";
+    private static final String OTHER_NODE = "otherNode";
+    private static final String NODE_ID = "nodeId";
+
+    @Override
+    public NumaNodeStat read(JsonReader in) throws IOException {
+        return null;
+    }
+
+    @Override
+    public void write(JsonWriter out, NumaNodeStat stat) throws IOException {
+        out.beginObject();
+        out.name(NUMA_HIT);
+        writeLong(out, stat.getNumaHit());
+        out.name(NUMA_MISS);
+        writeLong(out, stat.getNumaMiss());
+        out.name(NUMA_FOREIGN);
+        writeLong(out, stat.getNumaForeign());
+        out.name(INTERLEAVE_HIT);
+        writeLong(out, stat.getInterleaveHit());
+        out.name(LOCAL_NODE);
+        writeLong(out, stat.getLocalNode());
+        out.name(OTHER_NODE);
+        writeLong(out, stat.getOtherNode());
+        out.name(NODE_ID);
+        out.value(stat.getNodeId());
+        out.endObject();
+    }
+
+    private void writeLong(JsonWriter out, long timestamp) throws IOException {
+        // Write MongoDB representation of a Long
+        out.beginObject();
+        out.name(TYPE_LONG);
+        out.value(String.valueOf(timestamp));
+        out.endObject();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/numa/common/src/test/java/com/redhat/thermostat/numa/common/internal/NumaNodeStatTypeAdapterTest.java	Thu May 25 14:07:06 2017 -0400
@@ -0,0 +1,80 @@
+/*
+ * 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.numa.common.internal;
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+import com.redhat.thermostat.numa.common.NumaNodeStat;
+
+import com.google.gson.GsonBuilder;
+import com.google.gson.Gson;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by Jmatsuok on 06/04/17.
+ */
+public class NumaNodeStatTypeAdapterTest {
+
+    @Test
+    public void testNumaNodeStatGetsSerializedCorrectly() {
+        GsonBuilder builder = new GsonBuilder();
+        builder.registerTypeAdapter(NumaNodeStat.class, new NumaNodeStatTypeAdapter().nullSafe());
+        Gson gson = builder.create();
+        List<NumaNodeStat> stats = new ArrayList<>();
+        NumaNodeStat s = new NumaNodeStat();
+        s.setNumaHit(1l);
+        s.setNumaMiss(2l);
+        s.setNumaForeign(3l);
+        s.setInterleaveHit(4l);
+        s.setLocalNode(5l);
+        s.setOtherNode(6l);
+        s.setNodeId(7);
+        stats.add(s);
+        assertEquals("{" +
+                "\"numaHit\":{\"$numberLong\":\"1\"}," +
+                "\"numaMiss\":{\"$numberLong\":\"2\"}," +
+                "\"numaForeign\":{\"$numberLong\":\"3\"}," +
+                "\"interleaveHit\":{\"$numberLong\":\"4\"}," +
+                "\"localNode\":{\"$numberLong\":\"5\"}," +
+                "\"otherNode\":{\"$numberLong\":\"6\"}," +
+                "\"nodeId\":7" +
+                "}", gson.toJson(s));
+    }
+}