]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | package org.apache.thrift.protocol; |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.Collections; | |
5 | import java.util.HashMap; | |
6 | import java.util.LinkedHashSet; | |
7 | import java.util.List; | |
8 | import java.util.Map; | |
9 | import java.util.Set; | |
10 | ||
11 | import org.apache.thrift.Fixtures; | |
12 | import org.apache.thrift.TException; | |
13 | import org.apache.thrift.transport.TMemoryBuffer; | |
14 | ||
15 | public class BenchmarkProtocols { | |
16 | ||
17 | private static final Set<TProtocolFactory> FACTORIES = new LinkedHashSet<TProtocolFactory>(){{ | |
18 | add(new TTupleProtocol.Factory()); | |
19 | add(new TCompactProtocol.Factory()); | |
20 | add(new TBinaryProtocol.Factory()); | |
21 | }}; | |
22 | ||
23 | private static final int NUM_REPS = 100000; | |
24 | private static final int NUM_TRIALS = 10; | |
25 | ||
26 | public static void main(String[] args) throws TException { | |
27 | Map<TProtocolFactory, List<Long>> timesByFactory = new HashMap<TProtocolFactory, List<Long>>(); | |
28 | ||
29 | for (int trial = 0; trial < NUM_TRIALS; trial++) { | |
30 | for (int i = 0; i < 16; i++) { | |
31 | System.gc(); | |
32 | } | |
33 | // TProtocol proto = factory.getProtocol(new TTransport() { | |
34 | // @Override | |
35 | // public void write(byte[] buf, int off, int len) throws TTransportException { | |
36 | // } | |
37 | // | |
38 | // @Override | |
39 | // public int read(byte[] buf, int off, int len) throws TTransportException { | |
40 | // return 0; | |
41 | // } | |
42 | // | |
43 | // @Override | |
44 | // public void open() throws TTransportException { | |
45 | // } | |
46 | // | |
47 | // @Override | |
48 | // public boolean isOpen() { | |
49 | // return true; | |
50 | // } | |
51 | // | |
52 | // @Override | |
53 | // public void close() { | |
54 | // } | |
55 | // }); | |
56 | ||
57 | ||
58 | for (TProtocolFactory factory : FACTORIES) { | |
59 | if (timesByFactory.get(factory) == null) { | |
60 | timesByFactory.put(factory, new ArrayList<Long>()); | |
61 | } | |
62 | ||
63 | long start = System.currentTimeMillis(); | |
64 | for (int rep = 0; rep < NUM_REPS; rep++) { | |
65 | TProtocol proto = factory.getProtocol(new TMemoryBuffer(128*1024)); | |
66 | Fixtures.compactProtoTestStruct.write(proto); | |
67 | Fixtures.nesting.write(proto); | |
68 | } | |
69 | long end = System.currentTimeMillis(); | |
70 | timesByFactory.get(factory).add(end-start); | |
71 | } | |
72 | } | |
73 | ||
74 | for (TProtocolFactory factory : FACTORIES) { | |
75 | List<Long> times = timesByFactory.get(factory); | |
76 | // System.out.println("raw times pre-drop: " + times ); | |
77 | times.remove(Collections.max(times)); | |
78 | long total = 0; | |
79 | for (long t : times) { | |
80 | total += t; | |
81 | } | |
82 | Collections.sort(times); | |
83 | System.out.println(factory.getClass().getName() + " average time: " + (total / times.size()) + "ms"); | |
84 | System.out.println("raw times: " + times); | |
85 | } | |
86 | } | |
87 | ||
88 | } |