changeset 2649:79ec3fd40d61 web-gateway

Add TypeAdapter for serializing VmCompilerStat objects.
author Joshua Matsuoka <jmatsuok@redhat.com>
date Tue, 18 Apr 2017 15:37:13 -0400
parents e4c63e795c39
children 327034673caa
files plugins/vm-compiler/common/pom.xml plugins/vm-compiler/common/src/main/java/com/redhat/thermostat/vm/compiler/common/internal/VmCompilerStatTypeAdapter.java plugins/vm-compiler/common/src/test/java/com/redhat/thermostat/vm/compiler/common/internal/VmCompilerStatTypeAdapterTest.java
diffstat 3 files changed, 178 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/vm-compiler/common/pom.xml	Tue Apr 18 11:22:31 2017 -0400
+++ b/plugins/vm-compiler/common/pom.xml	Tue Apr 18 15:37:13 2017 -0400
@@ -124,5 +124,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/vm-compiler/common/src/main/java/com/redhat/thermostat/vm/compiler/common/internal/VmCompilerStatTypeAdapter.java	Tue Apr 18 15:37:13 2017 -0400
@@ -0,0 +1,105 @@
+/*
+ * 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.compiler.common.internal;
+
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+import com.redhat.thermostat.vm.compiler.common.VmCompilerStat;
+
+import java.io.IOException;
+
+public class VmCompilerStatTypeAdapter extends TypeAdapter<VmCompilerStat> {
+
+    private static final String TYPE_LONG = "$numberLong";
+    private static final String AGENT_ID = "agentId";
+    private static final String VM_ID = "vmId";
+    private static final String TIMESTAMP = "timeStamp";
+    private static final String TOTAL_COMPILES = "totalCompiles";
+    private static final String TOTAL_BAILOUTS = "totalBailouts";
+    private static final String TOTAL_INVALIDATES = "totalInvalidates";
+    private static final String COMPILATION_TIME = "compilationTime";
+    private static final String LAST_SIZE = "lastSize";
+    private static final String LAST_TYPE = "lastType";
+    private static final String LAST_METHOD = "lastMethod";
+    private static final String LAST_FAILED_TYPE = "lastFailedType";
+    private static final String LAST_FAILED_METHOD = "lastFailedMethod";
+
+
+    @Override
+    public VmCompilerStat read(JsonReader in) throws IOException {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void write(JsonWriter out, VmCompilerStat 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(TOTAL_COMPILES);
+        writeLong(out, stat.getTotalCompiles());
+        out.name(TOTAL_BAILOUTS);
+        writeLong(out, stat.getTotalBailouts());
+        out.name(TOTAL_INVALIDATES);
+        writeLong(out, stat.getTotalInvalidates());
+        out.name(COMPILATION_TIME);
+        writeLong(out, stat.getCompilationTime());
+        out.name(LAST_SIZE);
+        writeLong(out, stat.getLastSize());
+        out.name(LAST_TYPE);
+        writeLong(out, stat.getLastType());
+        out.name(LAST_METHOD);
+        out.value(stat.getLastMethod());
+        out.name(LAST_FAILED_TYPE);
+        writeLong(out, stat.getLastFailedType());
+        out.name(LAST_FAILED_METHOD);
+        out.value(stat.getLastFailedMethod());
+        out.endObject();
+    }
+
+    private 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-compiler/common/src/test/java/com/redhat/thermostat/vm/compiler/common/internal/VmCompilerStatTypeAdapterTest.java	Tue Apr 18 15:37:13 2017 -0400
@@ -0,0 +1,68 @@
+/*
+ * 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.compiler.common.internal;
+
+import com.google.gson.GsonBuilder;
+import com.google.gson.Gson;
+import com.redhat.thermostat.vm.compiler.common.VmCompilerStat;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+public class VmCompilerStatTypeAdapterTest {
+
+    @Test
+    public void testRead() {
+        GsonBuilder builder = new GsonBuilder();
+        builder.registerTypeAdapter(VmCompilerStat.class, new VmCompilerStatTypeAdapter());
+        Gson gson = builder.create();
+        VmCompilerStat stat = new VmCompilerStat();
+        stat.setTimeStamp(100l);
+        stat.setVmId("1");
+        stat.setAgentId("1");
+        stat.setCompilationTime(20l);
+        stat.setLastFailedMethod("methodFail()");
+        stat.setLastFailedType(1l);
+        stat.setLastMethod("successfulMethod()");
+        stat.setLastType(2l);
+        stat.setLastSize(300l);
+        stat.setTotalInvalidates(10l);
+        stat.setTotalBailouts(30l);
+        stat.setTotalCompiles(40l);
+        System.out.println(gson.toJson(stat));
+        assertEquals("{\"agentId\":\"1\",\"vmId\":\"1\",\"timeStamp\":{\"$numberLong\":\"100\"},\"totalCompiles\":{\"$numberLong\":\"40\"},\"totalBailouts\":{\"$numberLong\":\"30\"},\"totalInvalidates\":{\"$numberLong\":\"10\"},\"compilationTime\":{\"$numberLong\":\"20\"},\"lastSize\":{\"$numberLong\":\"300\"},\"lastType\":{\"$numberLong\":\"2\"},\"lastMethod\":\"successfulMethod()\",\"lastFailedType\":{\"$numberLong\":\"1\"},\"lastFailedMethod\":\"methodFail()\"}", gson.toJson(stat));
+    }
+}