View Javadoc

1   package org.apache.directmemory.serialization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  
25  import java.io.IOException;
26  
27  import org.apache.directmemory.measures.Monitor;
28  import org.apache.directmemory.measures.MonitorService;
29  import org.apache.directmemory.measures.Ram;
30  import org.apache.directmemory.misc.DummyPojo;
31  import org.junit.Test;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
36  import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
37  import com.carrotsearch.junitbenchmarks.annotation.AxisRange;
38  import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart;
39  import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
40  import com.carrotsearch.junitbenchmarks.annotation.LabelType;
41  
42  @AxisRange( min = 0, max = 1 )
43  @BenchmarkMethodChart()
44  @BenchmarkHistoryChart( labelWith = LabelType.CUSTOM_KEY, maxRuns = 5 )
45  @BenchmarkOptions( benchmarkRounds = 2, warmupRounds = 1, concurrency = 1 )
46  
47  public class SerializerTest
48      extends AbstractBenchmark
49  {
50  
51      private static Logger logger = LoggerFactory.getLogger( SerializerTest.class );
52  
53      private void testSerializer( String name, Serializer serializer, int size, int howMany )
54          throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
55      {
56          logger.info( "begin " + serializer.getClass().toString() );
57          MonitorService stopWatch = Monitor.get( "serializer." + name + "." + size + "bytes" );
58          MonitorService stopWatch2 = Monitor.get( "deserializer." + name + "." + size + "bytes" );
59          DummyPojo pojo = new DummyPojo( "test", size );
60          for ( int i = 0; i < howMany; i++ )
61          {
62              long split = stopWatch.start();
63              final byte[] array = serializer.serialize( pojo );
64              stopWatch.stop( split );
65              long split2 = stopWatch2.start();
66              DummyPojo check = (DummyPojo) serializer.deserialize( array, pojo.getClass() );
67              stopWatch2.stop( split2 );
68              assertNotNull( "object has not been serialized", check );
69              assertEquals( pojo.name, check.name );
70          }
71          logger.info( "end serialize " + serializer.getClass().toString() + "\r\n" + stopWatch.toString() );
72          logger.info( "end deserialize " + serializer.getClass().toString() + "\r\n" + stopWatch2.toString() );
73      }
74  
75      @Test
76      public void StandardTest()
77          throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
78      {
79          testSerializer( "java-serialization", new StandardSerializer(), Ram.Kb( 1 ), 20000 );
80          testSerializer( "java-serialization", new StandardSerializer(), Ram.Kb( 2 ), 20000 );
81          testSerializer( "java-serialization", new StandardSerializer(), Ram.Kb( 3 ), 20000 );
82          testSerializer( "java-serialization", new StandardSerializer(), Ram.Kb( 4 ), 20000 );
83      }
84  
85  }