Java tutorial
package com.conwet.silbops.model; /* * #%L * SilboPS Performance Test * %% * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.junit.Test; import com.conwet.jsp.Message; import com.conwet.jsp.MessageImpl; import com.conwet.silbops.model.basic.Attribute; import com.conwet.silbops.model.basic.Type; import com.conwet.silbops.util.JSONizable; import com.conwet.xjsp.features.FeatureMap; import com.conwet.xjsp.features.ImmutableMessage; /** * * @author aitor * @author gonzalo */ public class JSONvsRMIPerfT { public static void externalizeObject(Externalizable object, ByteArrayOutputStream baos) { Externalizable[] objects = new Externalizable[1]; objects[0] = object; externalizeSeveralObjects(objects, baos); } public static void externalizeSeveralObjects(Externalizable[] objects, ByteArrayOutputStream baos) { long start = 0, end = 0; try { ObjectOutputStream out = new ObjectOutputStream(baos); //Measure the elapsed time start = System.nanoTime(); for (Externalizable object : objects) { out.writeObject(object); } end = System.nanoTime(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } long elapsed = (end - start); long elapsedPerMessage = elapsed / objects.length; System.out.println(" Elapsed Time " + objects.length + " messages - Externalize: " + elapsed + " nanoseconds (" + elapsedPerMessage + " nanoseconds/msg)"); } public static void sizeExternalizeWithTypes(String type, Externalizable object) { Externalizable[] objects = new Externalizable[1]; objects[0] = object; sizeExternalizeSeveralWithType(type, objects); } public static void sizeExternalizeSeveralWithType(String type, Externalizable[] objects) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream out = new ObjectOutputStream(baos); for (Externalizable object : objects) { Message msg = new MessageImpl(type, object, null); out.writeObject(msg); } out.close(); } catch (IOException ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size indicating types " + objects.length + " messages - RMI: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); } public static void sizeExternalizeWithoutTypes(Externalizable object) { Externalizable[] objects = new Externalizable[1]; objects[0] = object; sizeExternalizeSeveralWithoutTypes(objects); } public static void sizeExternalizeSeveralWithoutTypes(Externalizable[] objects) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream out = new ObjectOutputStream(baos); for (Externalizable object : objects) { object.writeExternal(out); } out.close(); } catch (IOException ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size no indicating types " + objects.length + " messages - RMI: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); } public static void desexternalizeObjects(ByteArrayInputStream bais, int nElements) { long start = 0, end = 0; try { ObjectInputStream in = new ObjectInputStream(bais); start = System.nanoTime(); for (int i = 0; i < nElements; i++) { in.readObject(); } end = System.nanoTime(); in.close(); } catch (Exception ex) { ex.printStackTrace(); } long elapsed = end - start; long elapsedPerMessage = elapsed / nElements; System.out.println(" Elapsed Time " + nElements + " messages - Desexternalize: " + elapsed + " nanoseconds (" + elapsedPerMessage + " nanoseconds/msg)"); } public static void jSONizeObject(JSONizable object, ByteArrayOutputStream baos) { JSONizable[] objects = new JSONizable[1]; objects[0] = object; jSONizeObjects(objects, baos); } public static void jSONizeObjects(JSONizable[] objects, ByteArrayOutputStream baos) { long start = 0, end = 0; try { ObjectOutputStream stream = new ObjectOutputStream(baos); start = System.nanoTime(); for (JSONizable object : objects) { String json = object.toJSONString(); stream.writeObject(json); } end = System.nanoTime(); } catch (Exception ex) { ex.printStackTrace(); } long elapsed = end - start; long elapsedPerMessage = elapsed / objects.length; System.out.println(" Elapsed Time " + objects.length + " messages - JSONize: " + elapsed + " nanoseconds (" + elapsedPerMessage + " nanoseconds/msg)"); } public static void sizeJSONWithoutTypes(JSONizable object) { JSONizable[] objects = new JSONizable[1]; objects[0] = object; sizeSeveralJSONWithoutTypes(objects); } public static void sizeSeveralJSONWithoutTypes(JSONizable[] objects) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream stream; try { stream = new ObjectOutputStream(baos); for (JSONizable object : objects) { String json = object.toJSONString(); stream.writeObject(json); } } catch (Exception ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size no indicating type " + objects.length + " messages - JSON: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); } public static void sizeJSONWithTypes(JSONizable object, String feature, String type) { JSONizable[] objects = new JSONizable[1]; objects[0] = object; sizeSeveralJSONWithTypes(objects, feature, type); } @SuppressWarnings("unchecked") public static void sizeSeveralJSONWithTypes(JSONizable[] objects, String feature, String type) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream stream; FeatureMap featureMap = mock(FeatureMap.class); when(featureMap.getPrefix(feature)).thenReturn("ps"); try { stream = new ObjectOutputStream(baos); for (JSONizable object : objects) { JSONObject payLoad = new JSONObject(); payLoad.put(type, object.toJSON()); ImmutableMessage msg = new ImmutableMessage(feature, type, payLoad, "0"); String codedMsg = msg.toJSONObject(featureMap).toJSONString(); stream.writeObject(codedMsg); } } catch (Exception ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size indicating type " + objects.length + " messages - JSON: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); } public static JSONObject desJSONizeObject(ByteArrayInputStream bais) throws Exception { ObjectInputStream inputStream = new ObjectInputStream(bais); JSONParser parser = new JSONParser(); String jsonReceived = (String) inputStream.readObject(); return (JSONObject) parser.parse(jsonReceived); } public static void desJSONizeAdvs(ByteArrayInputStream bais, int nElements) { long start = 0, end = 0; try { ObjectInputStream inputStream = new ObjectInputStream(bais); JSONParser parser = new JSONParser(); start = System.nanoTime(); for (int i = 0; i < nElements; i++) { String jsonReceived = (String) inputStream.readObject(); JSONArray jSONObject = (JSONArray) parser.parse(jsonReceived); Advertise.fromJSON(jSONObject); } end = System.nanoTime(); } catch (Exception ex) { ex.printStackTrace(); } long elapsed = end - start; long elapsedPerMessage = elapsed / nElements; System.out.println(" Elapsed Time " + nElements + " messages - DesJSONize: " + elapsed + " nanoseconds (" + elapsedPerMessage + " nanoseconds/msg)"); } public static void desJSONizeSubscriptions(ByteArrayInputStream bais, int nElements) { long start = 0, end = 0; try { ObjectInputStream inputStream = new ObjectInputStream(bais); JSONParser parser = new JSONParser(); start = System.nanoTime(); for (int i = 0; i < nElements; i++) { String jsonReceived = (String) inputStream.readObject(); JSONObject jSONObject = (JSONObject) parser.parse(jsonReceived); Subscription.fromJSON(jSONObject); } end = System.nanoTime(); } catch (Exception ex) { ex.printStackTrace(); } long elapsed = end - start; long elapsedPerMessage = elapsed / nElements; System.out.println(" Elapsed Time " + nElements + " messages - DesJSONize: " + elapsed + " nanoseconds (" + elapsedPerMessage + " nanoseconds/msg)"); } public static void desJSONizeNots(ByteArrayInputStream bais, int nElements) { long start = 0, end = 0; try { ObjectInputStream inputStream = new ObjectInputStream(bais); JSONParser parser = new JSONParser(); start = System.nanoTime(); for (int i = 0; i < nElements; i++) { String jsonReceived = (String) inputStream.readObject(); JSONObject jSONObject = (JSONObject) parser.parse(jsonReceived); Notification.fromJSON(jSONObject); } end = System.nanoTime(); } catch (Exception ex) { ex.printStackTrace(); } long elapsed = end - start; long elapsedPerMessage = elapsed / nElements; System.out.println(" Elapsed Time " + nElements + " messages - DesJSONize: " + elapsed + " nanoseconds (" + elapsedPerMessage + " nanoseconds/msg)"); } @Test public void testAdvSubscription() { Advertise subscription = new Advertise(); subscription.attribute("Test1", Type.DOUBLE); subscription.attribute("Test2", Type.STRING); Random random = new Random(); Advertise[] subscriptions = new Advertise[1000 * 10]; for (int i = 0; i < subscriptions.length; i++) { subscriptions[i] = new Advertise(); subscriptions[i].attribute(new Double(random.nextDouble()).toString().substring(0, 6), Type.DOUBLE); subscriptions[i].attribute(new Double(random.nextDouble()).toString().substring(0, 6), Type.STRING); } System.out.println("*****************TESTING ADVFILTERS*****************"); //Externalize and desexternalize one ByteArrayOutputStream baos = new ByteArrayOutputStream(); externalizeObject(subscription, baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); desexternalizeObjects(bais, 1); //Externalize and desexternalize several baos = new ByteArrayOutputStream(); externalizeSeveralObjects(subscriptions, baos); bais = new ByteArrayInputStream(baos.toByteArray()); desexternalizeObjects(bais, subscriptions.length); //Size Externalize with types sizeExternalizeWithTypes(MessageType.ADVERTISE.name(), subscription); sizeExternalizeSeveralWithType(MessageType.ADVERTISE.name(), subscriptions); //Size Externalize without types sizeExternalizeWithoutTypes(subscription); sizeExternalizeSeveralWithoutTypes(subscriptions); //JSONize and desJSONize one object baos = new ByteArrayOutputStream(); jSONizeObject(subscription, baos); bais = new ByteArrayInputStream(baos.toByteArray()); desJSONizeAdvs(bais, 1); //JSONize and desJSONize several objects baos = new ByteArrayOutputStream(); jSONizeObjects(subscriptions, baos); bais = new ByteArrayInputStream(baos.toByteArray()); desJSONizeAdvs(bais, subscriptions.length); //Size JSON with types String feature = "silbops-publisher"; String type = "advertise"; sizeJSONWithTypes(subscription, feature, type); sizeSeveralJSONWithTypes(subscriptions, feature, type); //Size JSON without types sizeJSONWithoutTypes(subscription); sizeSeveralJSONWithoutTypes(subscriptions); } private Attribute toAttr(double value) { return new Attribute(String.valueOf(value).substring(0, 6), Type.DOUBLE); } @Test public void testSubscription() { Subscription subscription = new Subscription().constrain("Test1", Type.DOUBLE).ge(50.0D) .constrain("Test2", Type.STRING).startsWith("TEST").subscription(); Random random = new Random(); Subscription[] subscriptions = new Subscription[1000 * 10]; for (int i = 0; i < subscriptions.length; i++) { subscriptions[i] = new Subscription(); subscriptions[i].constrain(toAttr(random.nextDouble())).ge(random.nextDouble()); subscriptions[i].constrain(toAttr(random.nextDouble())) .startsWith(toAttr(random.nextDouble()).getName()); } System.out.println("*****************TESTING FILTERS*****************"); //Externalize and desexternalize one ByteArrayOutputStream baos = new ByteArrayOutputStream(); externalizeObject(subscription, baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); desexternalizeObjects(bais, 1); //Externalize and desexternalize several baos = new ByteArrayOutputStream(); externalizeSeveralObjects(subscriptions, baos); bais = new ByteArrayInputStream(baos.toByteArray()); desexternalizeObjects(bais, subscriptions.length); //Size Externalize with types sizeExternalizeWithTypes(MessageType.SUBSCRIBE.name(), subscription); sizeExternalizeSeveralWithType(MessageType.SUBSCRIBE.name(), subscriptions); //Size Externalize without types sizeExternalizeWithoutTypes(subscription); sizeExternalizeSeveralWithoutTypes(subscriptions); //JSONize and desJSONize one object baos = new ByteArrayOutputStream(); jSONizeObject(subscription, baos); bais = new ByteArrayInputStream(baos.toByteArray()); desJSONizeSubscriptions(bais, 1); //JSONize and desJSONize several objects baos = new ByteArrayOutputStream(); jSONizeObjects(subscriptions, baos); bais = new ByteArrayInputStream(baos.toByteArray()); desJSONizeSubscriptions(bais, subscriptions.length); //Size JSON with types String feature = "silbops-subscriber"; String type = "subscribe"; sizeJSONWithTypes(subscription, feature, type); sizeSeveralJSONWithTypes(subscriptions, feature, type); //Size JSON without types sizeJSONWithoutTypes(subscription); sizeSeveralJSONWithoutTypes(subscriptions); } @Test public void testNotification() { Attribute timestamp = new Attribute("timestamp", Type.LONG); Attribute fqn = new Attribute("fqn", Type.STRING); Attribute measureUnit = new Attribute("measureUnit", Type.STRING); Attribute eventType = new Attribute("eventType", Type.STRING); Attribute measureName = new Attribute("measureName", Type.STRING); Attribute measureValue = new Attribute("measureValue", Type.DOUBLE); Notification not = new Notification().attribute(timestamp, 1319015169373L) .attribute(fqn, "es.tid.customers.cc1.services.monitoring." + "vees.taxiapp.replicas.1.processes.collectd") .attribute(measureUnit, "bytes").attribute(eventType, "monitoring") .attribute(measureName, "programDiskWriteOperations").attribute(measureValue, 5.0D); Notification[] nots = new Notification[1000 * 10]; for (int i = 0; i < nots.length; i++) { nots[i] = new Notification().attribute(timestamp, 1319015169373L + i) .attribute(fqn, "es.tid.customers.cc1.services.monitoring." + "vees.taxiapp.replicas." + i + ".processes.collectd") .attribute(measureUnit, "bytes").attribute(eventType, "monitoring") .attribute(measureName, "programDiskWriteOperations" + i).attribute(measureValue, 5.0D + i); } System.out.println("*****************TESTING NOTIFICATIONS*****************"); //Externalize and desexternalize one ByteArrayOutputStream baos = new ByteArrayOutputStream(); externalizeObject(not, baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); desexternalizeObjects(bais, 1); //Externalize and desexternalize several baos = new ByteArrayOutputStream(); externalizeSeveralObjects(nots, baos); bais = new ByteArrayInputStream(baos.toByteArray()); desexternalizeObjects(bais, nots.length); //Size Externalize with types sizeExternalizeWithTypes(MessageType.PUBLISH.name(), not); sizeExternalizeSeveralWithType(MessageType.PUBLISH.name(), nots); //Size Externalize without types sizeExternalizeWithoutTypes(not); sizeExternalizeSeveralWithoutTypes(nots); //JSONize and desJSONize one object baos = new ByteArrayOutputStream(); jSONizeObject(not, baos); bais = new ByteArrayInputStream(baos.toByteArray()); desJSONizeNots(bais, 1); //JSONize and desJSONize several objects baos = new ByteArrayOutputStream(); jSONizeObjects(nots, baos); bais = new ByteArrayInputStream(baos.toByteArray()); desJSONizeNots(bais, nots.length); //Size JSON with types String feature = "silbops-publisher"; String type = "publish"; sizeJSONWithTypes(not, feature, type); sizeSeveralJSONWithTypes(nots, feature, type); //Size JSON without types sizeJSONWithoutTypes(not); sizeSeveralJSONWithoutTypes(nots); } }