@@ -0,0 +1,3199 @@
+/*%0A * Copyright (C) 2010 Google Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage examples;%0A%0Aimport com.google.caliper.LinearTranslation;%0Aimport com.google.caliper.Param;%0Aimport com.google.caliper.SimpleBenchmark;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.zip.Deflater;%0A%0A/**%0A * This is a hack, but shows a neat trick that can be done by overriding the nanosToUnits method.%0A * Benchmarks compression ratios instead of times.%0A */%0Apublic class CompressionSizeBenchmark extends SimpleBenchmark %7B%0A%0A  @Param(%7B%0A      %22this string will compress badly%22,%0A      %22aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa%22,%0A      %22asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf%22%7D)%0A  private String toCompress;%0A  @Param(%7B%22bestCompression%22, %22bestSpeed%22, %22noCompression%22, %22huffmanOnly%22%7D)%0A  private String compressionLevel;%0A%0A  private double compressionRatio;%0A%0A  public static final Map%3CString, Integer%3E compressionLevelMap = new HashMap%3CString, Integer%3E();%0A  static %7B%0A      compressionLevelMap.put(%22bestCompression%22, Deflater.BEST_COMPRESSION);%0A      compressionLevelMap.put(%22bestSpeed%22, Deflater.BEST_SPEED);%0A      compressionLevelMap.put(%22noCompression%22, Deflater.NO_COMPRESSION);%0A      compressionLevelMap.put(%22huffmanOnly%22, Deflater.HUFFMAN_ONLY);%0A  %7D%0A%0A  public void timeCompressionSize(int reps) %7B%0A    byte%5B%5D initialBytes = toCompress.getBytes();%0A    byte%5B%5D finalBytes = compress(initialBytes);%0A    compressionRatio = (double) initialBytes.length / (double) finalBytes.length;%0A%0A    // simulate actually doing runs to make caliper happy%0A    LinearTranslation translation = new LinearTranslation(0, 0, 1000000, 1000);%0A    try %7B%0A      Thread.sleep((long) translation.translate(reps));%0A    %7D catch (InterruptedException e) %7B%0A    %7D%0A  %7D%0A%0A  private byte%5B%5D compress(byte%5B%5D bytes) %7B%0A    Deflater compressor = new Deflater();%0A    compressor.setLevel(compressionLevelMap.get(compressionLevel));%0A    compressor.setInput(bytes);%0A    compressor.finish();%0A    ByteArrayOutputStream bos = new ByteArrayOutputStream();%0A    byte%5B%5D buf = new byte%5B1024%5D;%0A    while (!compressor.finished()) %7B%0A      int count = compressor.deflate(buf);%0A      bos.write(buf, 0, count);%0A    %7D%0A    try %7B%0A      bos.close();%0A    %7D catch (IOException e) %7B%0A    %7D%0A    return bos.toByteArray();%0A  %7D%0A%0A  @Override public Map%3CString, Integer%3E unitNames() %7B%0A    Map%3CString, Integer%3E unitNames = new HashMap%3CString, Integer%3E();%0A    unitNames.put(%22 compression ratio (higher is better)%22, 1);%0A    return unitNames;%0A  %7D%0A%0A  @Override public double nanosToUnits(double nanos) %7B%0A    return compressionRatio;%0A  %7D%0A%7D%0A

Add neat hacky benchmark to show capabilities of nanosToUnits.