@@ -0,0 +1,2635 @@
+package org.apache.thrift.protocol;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.LinkedHashSet;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0Aimport org.apache.thrift.Fixtures;%0Aimport org.apache.thrift.TException;%0Aimport org.apache.thrift.transport.TMemoryBuffer;%0A%0Apublic class BenchmarkProtocols %7B%0A%0A  private static final Set%3CTProtocolFactory%3E FACTORIES = new LinkedHashSet%3CTProtocolFactory%3E()%7B%7B%0A    add(new TTupleProtocol.Factory());%0A    add(new TCompactProtocol.Factory());%0A    add(new TBinaryProtocol.Factory());%0A  %7D%7D;%0A%0A  private static final int NUM_REPS = 100000;%0A  private static final int NUM_TRIALS = 10;%0A%0A  public static void main(String%5B%5D args) throws TException %7B%0A    Map%3CTProtocolFactory, List%3CLong%3E%3E timesByFactory = new HashMap%3CTProtocolFactory, List%3CLong%3E%3E();%0A%0A    for (int trial = 0; trial %3C NUM_TRIALS; trial++) %7B%0A      for (int i = 0; i %3C 16; i++) %7B%0A        System.gc();%0A      %7D%0A//      TProtocol proto = factory.getProtocol(new TTransport() %7B%0A//        @Override%0A//        public void write(byte%5B%5D buf, int off, int len) throws TTransportException %7B%0A//        %7D%0A//%0A//        @Override%0A//        public int read(byte%5B%5D buf, int off, int len) throws TTransportException %7B%0A//          return 0;%0A//        %7D%0A//%0A//        @Override%0A//        public void open() throws TTransportException %7B%0A//        %7D%0A//%0A//        @Override%0A//        public boolean isOpen() %7B%0A//          return true;%0A//        %7D%0A//%0A//        @Override%0A//        public void close() %7B%0A//        %7D%0A//      %7D);%0A%0A%0A      for (TProtocolFactory factory : FACTORIES) %7B%0A        if (timesByFactory.get(factory) == null) %7B%0A          timesByFactory.put(factory, new ArrayList%3CLong%3E());%0A        %7D%0A%0A        long start = System.currentTimeMillis();%0A        for (int rep = 0; rep %3C NUM_REPS; rep++) %7B%0A          TProtocol proto = factory.getProtocol(new TMemoryBuffer(128*1024));%0A          Fixtures.compactProtoTestStruct.write(proto);%0A          Fixtures.nesting.write(proto);%0A        %7D%0A        long end = System.currentTimeMillis();%0A        timesByFactory.get(factory).add(end-start);%0A      %7D%0A    %7D%0A%0A    for (TProtocolFactory factory : FACTORIES) %7B%0A      List%3CLong%3E times = timesByFactory.get(factory);%0A//      System.out.println(%22raw times pre-drop: %22 + times );%0A      times.remove(Collections.max(times));%0A      long total = 0;%0A      for (long t : times) %7B%0A        total += t;%0A      %7D%0A      Collections.sort(times);%0A      System.out.println(factory.getClass().getName() + %22 average time: %22 + (total / times.size()) + %22ms%22);%0A      System.out.println(%22raw times: %22 + times);%0A    %7D%0A  %7D%0A%0A%7D%0A

add utility for benchmarking protocols against each other