]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/test/org/apache/thrift/test/SerializationBenchmark.java
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / test / org / apache / thrift / test / SerializationBenchmark.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20
21 package org.apache.thrift.test;
22
23 import org.apache.thrift.Fixtures;
24 import org.apache.thrift.TBase;
25 import org.apache.thrift.protocol.TBinaryProtocol;
26 import org.apache.thrift.protocol.TProtocol;
27 import org.apache.thrift.protocol.TProtocolFactory;
28 import org.apache.thrift.transport.TMemoryBuffer;
29 import org.apache.thrift.transport.TMemoryInputTransport;
30 import org.apache.thrift.transport.TTransport;
31 import org.apache.thrift.transport.TTransportException;
32
33 import thrift.test.OneOfEach;
34
35 public class SerializationBenchmark {
36 private final static int HOW_MANY = 10000000;
37
38 public static void main(String[] args) throws Exception {
39 TProtocolFactory factory = new TBinaryProtocol.Factory();
40
41 testSerialization(factory, Fixtures.oneOfEach);
42 testDeserialization(factory, Fixtures.oneOfEach, OneOfEach.class);
43 }
44
45 public static void testSerialization(TProtocolFactory factory, TBase object) throws Exception {
46 TTransport trans = new TTransport() {
47 public void write(byte[] bin, int x, int y) throws TTransportException {}
48 public int read(byte[] bin, int x, int y) throws TTransportException {return 0;}
49 public void close() {}
50 public void open() {}
51 public boolean isOpen() {return true;}
52 };
53
54 TProtocol proto = factory.getProtocol(trans);
55
56 long startTime = System.currentTimeMillis();
57 for (int i = 0; i < HOW_MANY; i++) {
58 object.write(proto);
59 }
60 long endTime = System.currentTimeMillis();
61
62 System.out.println("Serialization test time: " + (endTime - startTime) + " ms");
63 }
64
65 public static <T extends TBase> void testDeserialization(TProtocolFactory factory, T object, Class<T> klass) throws Exception {
66 TMemoryBuffer buf = new TMemoryBuffer(0);
67 object.write(factory.getProtocol(buf));
68 byte[] serialized = new byte[100*1024];
69 buf.read(serialized, 0, 100*1024);
70
71 long startTime = System.currentTimeMillis();
72 for (int i = 0; i < HOW_MANY; i++) {
73 T o2 = klass.newInstance();
74 o2.read(factory.getProtocol(new TMemoryInputTransport(serialized)));
75 }
76 long endTime = System.currentTimeMillis();
77
78 System.out.println("Deserialization test time: " + (endTime - startTime) + " ms");
79 }
80 }