Commit c0abf64e authored by kenton@google.com's avatar kenton@google.com

Convert ProtoBench.java to unix-style line endings.

parent f85d70f9
// Protocol Buffers - Google's data interchange format // Protocol Buffers - Google's data interchange format
// Copyright 2009 Google Inc. All rights reserved. // Copyright 2009 Google Inc. All rights reserved.
// http://code.google.com/p/protobuf/ // http://code.google.com/p/protobuf/
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are // modification, are permitted provided that the following conditions are
// met: // met:
// //
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above // * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer // copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the // in the documentation and/or other materials provided with the
// distribution. // distribution.
// * Neither the name of Google Inc. nor the names of its // * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from // contributors may be used to endorse or promote products derived from
// this software without specific prior written permission. // this software without specific prior written permission.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.protocolbuffers; package com.google.protocolbuffers;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream; import com.google.protobuf.CodedInputStream;
import com.google.protobuf.Message; import com.google.protobuf.Message;
public class ProtoBench { public class ProtoBench {
private static final long MIN_SAMPLE_TIME_MS = 2 * 1000; private static final long MIN_SAMPLE_TIME_MS = 2 * 1000;
private static final long TARGET_TIME_MS = 30 * 1000; private static final long TARGET_TIME_MS = 30 * 1000;
private ProtoBench() { private ProtoBench() {
// Prevent instantiation // Prevent instantiation
} }
public static void main(String[] args) { public static void main(String[] args) {
if (args.length < 2 || (args.length % 2) != 0) { if (args.length < 2 || (args.length % 2) != 0) {
System.err.println("Usage: ProtoBench <descriptor type name> <input data>"); System.err.println("Usage: ProtoBench <descriptor type name> <input data>");
System.err.println("The descriptor type name is the fully-qualified message name,"); System.err.println("The descriptor type name is the fully-qualified message name,");
System.err.println("e.g. com.google.protocolbuffers.benchmark.Message1"); System.err.println("e.g. com.google.protocolbuffers.benchmark.Message1");
System.err.println("(You can specify multiple pairs of descriptor type name and input data.)"); System.err.println("(You can specify multiple pairs of descriptor type name and input data.)");
System.exit(1); System.exit(1);
} }
boolean success = true; boolean success = true;
for (int i = 0; i < args.length; i += 2) { for (int i = 0; i < args.length; i += 2) {
success &= runTest(args[i], args[i + 1]); success &= runTest(args[i], args[i + 1]);
} }
System.exit(success ? 0 : 1); System.exit(success ? 0 : 1);
} }
/** /**
* Runs a single test. Error messages are displayed to stderr, and the return value * Runs a single test. Error messages are displayed to stderr, and the return value
* indicates general success/failure. * indicates general success/failure.
*/ */
public static boolean runTest(String type, String file) { public static boolean runTest(String type, String file) {
System.out.println("Benchmarking " + type + " with file " + file); System.out.println("Benchmarking " + type + " with file " + file);
final Message defaultMessage; final Message defaultMessage;
try { try {
Class<?> clazz = Class.forName(type); Class<?> clazz = Class.forName(type);
Method method = clazz.getDeclaredMethod("getDefaultInstance"); Method method = clazz.getDeclaredMethod("getDefaultInstance");
defaultMessage = (Message) method.invoke(null); defaultMessage = (Message) method.invoke(null);
} catch (Exception e) { } catch (Exception e) {
// We want to do the same thing with all exceptions. Not generally nice, // We want to do the same thing with all exceptions. Not generally nice,
// but this is slightly different. // but this is slightly different.
System.err.println("Unable to get default message for " + type); System.err.println("Unable to get default message for " + type);
return false; return false;
} }
try { try {
final byte[] inputData = readAllBytes(file); final byte[] inputData = readAllBytes(file);
final ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData); final ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData);
final ByteString inputString = ByteString.copyFrom(inputData); final ByteString inputString = ByteString.copyFrom(inputData);
final Message sampleMessage = defaultMessage.newBuilderForType().mergeFrom(inputString).build(); final Message sampleMessage = defaultMessage.newBuilderForType().mergeFrom(inputString).build();
benchmark("Serialize to byte string", inputData.length, new Action() { benchmark("Serialize to byte string", inputData.length, new Action() {
public void execute() { sampleMessage.toByteString(); } public void execute() { sampleMessage.toByteString(); }
}); });
benchmark("Serialize to byte array", inputData.length, new Action() { benchmark("Serialize to byte array", inputData.length, new Action() {
public void execute() { sampleMessage.toByteArray(); } public void execute() { sampleMessage.toByteArray(); }
}); });
benchmark("Serialize to memory stream", inputData.length, new Action() { benchmark("Serialize to memory stream", inputData.length, new Action() {
public void execute() throws IOException { public void execute() throws IOException {
sampleMessage.writeTo(new ByteArrayOutputStream()); sampleMessage.writeTo(new ByteArrayOutputStream());
} }
}); });
benchmark("Deserialize from byte string", inputData.length, new Action() { benchmark("Deserialize from byte string", inputData.length, new Action() {
public void execute() throws IOException { public void execute() throws IOException {
defaultMessage.newBuilderForType().mergeFrom(inputString).build(); defaultMessage.newBuilderForType().mergeFrom(inputString).build();
} }
}); });
benchmark("Deserialize from byte array", inputData.length, new Action() { benchmark("Deserialize from byte array", inputData.length, new Action() {
public void execute() throws IOException { public void execute() throws IOException {
defaultMessage.newBuilderForType() defaultMessage.newBuilderForType()
.mergeFrom(CodedInputStream.newInstance(inputData)).build(); .mergeFrom(CodedInputStream.newInstance(inputData)).build();
} }
}); });
benchmark("Deserialize from memory stream", inputData.length, new Action() { benchmark("Deserialize from memory stream", inputData.length, new Action() {
public void execute() throws IOException { public void execute() throws IOException {
defaultMessage.newBuilderForType() defaultMessage.newBuilderForType()
.mergeFrom(CodedInputStream.newInstance(inputStream)).build(); .mergeFrom(CodedInputStream.newInstance(inputStream)).build();
inputStream.reset(); inputStream.reset();
} }
}); });
System.out.println(); System.out.println();
return true; return true;
} catch (Exception e) { } catch (Exception e) {
System.err.println("Error: " + e.getMessage()); System.err.println("Error: " + e.getMessage());
System.err.println("Detailed exception information:"); System.err.println("Detailed exception information:");
e.printStackTrace(System.err); e.printStackTrace(System.err);
return false; return false;
} }
} }
private static void benchmark(String name, long dataSize, Action action) throws IOException { private static void benchmark(String name, long dataSize, Action action) throws IOException {
// Make sure it's JITted "reasonably" hard before running the first progress test // Make sure it's JITted "reasonably" hard before running the first progress test
for (int i=0; i < 100; i++) { for (int i=0; i < 100; i++) {
action.execute(); action.execute();
} }
// Run it progressively more times until we've got a reasonable sample // Run it progressively more times until we've got a reasonable sample
int iterations = 1; int iterations = 1;
long elapsed = timeAction(action, iterations); long elapsed = timeAction(action, iterations);
while (elapsed < MIN_SAMPLE_TIME_MS) { while (elapsed < MIN_SAMPLE_TIME_MS) {
iterations *= 2; iterations *= 2;
elapsed = timeAction(action, iterations); elapsed = timeAction(action, iterations);
} }
// Upscale the sample to the target time. Do this in floating point arithmetic // Upscale the sample to the target time. Do this in floating point arithmetic
// to avoid overflow issues. // to avoid overflow issues.
iterations = (int) ((TARGET_TIME_MS / (double) elapsed) * iterations); iterations = (int) ((TARGET_TIME_MS / (double) elapsed) * iterations);
elapsed = timeAction(action, iterations); elapsed = timeAction(action, iterations);
System.out.println(name + ": " + iterations + " iterations in " System.out.println(name + ": " + iterations + " iterations in "
+ (elapsed/1000f) + "s; " + (elapsed/1000f) + "s; "
+ (iterations * dataSize) / (elapsed * 1024 * 1024 / 1000f) + (iterations * dataSize) / (elapsed * 1024 * 1024 / 1000f)
+ "MB/s"); + "MB/s");
} }
private static long timeAction(Action action, int iterations) throws IOException { private static long timeAction(Action action, int iterations) throws IOException {
System.gc(); System.gc();
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
action.execute(); action.execute();
} }
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
return end - start; return end - start;
} }
private static byte[] readAllBytes(String filename) throws IOException { private static byte[] readAllBytes(String filename) throws IOException {
RandomAccessFile file = new RandomAccessFile(new File(filename), "r"); RandomAccessFile file = new RandomAccessFile(new File(filename), "r");
byte[] content = new byte[(int) file.length()]; byte[] content = new byte[(int) file.length()];
file.readFully(content); file.readFully(content);
return content; return content;
} }
/** /**
* Interface used to capture a single action to benchmark. * Interface used to capture a single action to benchmark.
*/ */
interface Action { interface Action {
void execute() throws IOException; void execute() throws IOException;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment