1 package org.apache.directmemory.serialization;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }