Package com.bubble.serializer.benchmark

Source Code of com.bubble.serializer.benchmark.SerializationBenchmark

/*
* Copyright (c) 2006 Leonardo "Bubble" Mesquita
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.bubble.serializer.benchmark;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

import com.bubble.serializer.LightSerializationContext;
import com.bubble.serializer.SerializationContext;
import com.bubble.serializer.objects.Pojo;
import com.bubble.utils.HelperOutputStream;

import junit.framework.TestCase;

public class SerializationBenchmark extends TestCase {
 
  private HelperOutputStream ho;
  private ObjectOutputStream oos;
  protected void setUp() throws Exception {
    this.ho = new HelperOutputStream();
    this.oos = new ObjectOutputStream(ho);
  }
 
  protected void tearDown() throws Exception {
    this.ho.close();
    this.ho = null;   
    this.oos = null;
  }
 
  private static final int TEST_VALUE = 7;
  private static final int POJOS = 2048;
  private static final int PASSES = 100;
  public void testJavaSpeedSingle() throws IOException {
    Pojo pojo = new Pojo(TEST_VALUE);
    long startTime = System.currentTimeMillis();
    oos.writeObject(pojo);
    long endTime = System.currentTimeMillis();
    long delay = endTime-startTime;
    System.out.println("Java time: "+delay+"ms");
  }
 
  private Pojo[] initPojos() {
    Pojo[] pojos = new Pojo[POJOS];
    for (int i = 0; i < pojos.length; i++) {
      pojos[i] = new Pojo(i);
    }
    return pojos;
  }

  public void testJavaSpeedMany() throws IOException {
    Pojo[] pojos;
    long startTime = System.currentTimeMillis();
    for(int j = 0; j < PASSES ; j++) {
      ho.reset();
      pojos = initPojos();
      for (int i = 0; i < pojos.length; i++) {
        oos.writeObject(pojos[i]);
      }
    }
    long endTime = System.currentTimeMillis();
    long delay = endTime-startTime;
    System.out.println("Java many time: "+delay+"ms");
  }


 
  public void testBubbleSpeedSingle()  {
    Pojo pojo = new Pojo(TEST_VALUE);
    SerializationContext context = new SerializationContext();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    long startTime = System.currentTimeMillis();
    context.serialize(pojo, buffer);
    long endTime = System.currentTimeMillis();
    long delay = endTime-startTime;
    System.out.println("Bubble time: "+delay+"ms");
  }


  public void testBubbleSpeedMany() {
    Pojo[] pojos;
    LightSerializationContext context = new LightSerializationContext();
    ByteBuffer buffer = ByteBuffer.allocate(32768);
    long startTime = System.currentTimeMillis();
    for(int j = 0; j < PASSES ; j++) {
      buffer.clear();
      pojos = initPojos();
      for (int i = 0; i < pojos.length; i++) {
        context.serialize(pojos[i], buffer);
      }
    }
    long endTime = System.currentTimeMillis();
    long delay = endTime-startTime;
    System.out.println("Bubble many time: "+delay+"ms");
  }

}
TOP

Related Classes of com.bubble.serializer.benchmark.SerializationBenchmark

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.