From 9a1d2f01c6fc7726432292dbec2a741d6a72684c Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Thu, 29 Sep 2011 22:51:54 +0000 Subject: [PATCH] add utility for benchmarking protocols against each other git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1177458 13f79535-47bb-0310-9956-ffa450edef68 --- .../thrift/protocol/BenchmarkProtocols.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 lib/java/test/org/apache/thrift/protocol/BenchmarkProtocols.java diff --git a/lib/java/test/org/apache/thrift/protocol/BenchmarkProtocols.java b/lib/java/test/org/apache/thrift/protocol/BenchmarkProtocols.java new file mode 100644 index 00000000..e8816075 --- /dev/null +++ b/lib/java/test/org/apache/thrift/protocol/BenchmarkProtocols.java @@ -0,0 +1,88 @@ +package org.apache.thrift.protocol; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.thrift.Fixtures; +import org.apache.thrift.TException; +import org.apache.thrift.transport.TMemoryBuffer; + +public class BenchmarkProtocols { + + private static final Set FACTORIES = new LinkedHashSet(){{ + add(new TTupleProtocol.Factory()); + add(new TCompactProtocol.Factory()); + add(new TBinaryProtocol.Factory()); + }}; + + private static final int NUM_REPS = 100000; + private static final int NUM_TRIALS = 10; + + public static void main(String[] args) throws TException { + Map> timesByFactory = new HashMap>(); + + for (int trial = 0; trial < NUM_TRIALS; trial++) { + for (int i = 0; i < 16; i++) { + System.gc(); + } +// TProtocol proto = factory.getProtocol(new TTransport() { +// @Override +// public void write(byte[] buf, int off, int len) throws TTransportException { +// } +// +// @Override +// public int read(byte[] buf, int off, int len) throws TTransportException { +// return 0; +// } +// +// @Override +// public void open() throws TTransportException { +// } +// +// @Override +// public boolean isOpen() { +// return true; +// } +// +// @Override +// public void close() { +// } +// }); + + + for (TProtocolFactory factory : FACTORIES) { + if (timesByFactory.get(factory) == null) { + timesByFactory.put(factory, new ArrayList()); + } + + long start = System.currentTimeMillis(); + for (int rep = 0; rep < NUM_REPS; rep++) { + TProtocol proto = factory.getProtocol(new TMemoryBuffer(128*1024)); + Fixtures.compactProtoTestStruct.write(proto); + Fixtures.nesting.write(proto); + } + long end = System.currentTimeMillis(); + timesByFactory.get(factory).add(end-start); + } + } + + for (TProtocolFactory factory : FACTORIES) { + List times = timesByFactory.get(factory); +// System.out.println("raw times pre-drop: " + times ); + times.remove(Collections.max(times)); + long total = 0; + for (long t : times) { + total += t; + } + Collections.sort(times); + System.out.println(factory.getClass().getName() + " average time: " + (total / times.size()) + "ms"); + System.out.println("raw times: " + times); + } + } + +} -- 2.17.1