1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2009 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protocolbuffers;
32 
33 import java.io.ByteArrayInputStream;
34 import java.io.ByteArrayOutputStream;
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.io.RandomAccessFile;
40 import java.lang.reflect.Method;
41 
42 import com.google.protobuf.ByteString;
43 import com.google.protobuf.CodedInputStream;
44 import com.google.protobuf.CodedOutputStream;
45 import com.google.protobuf.Message;
46 
47 public class ProtoBench {
48 
49   private static final long MIN_SAMPLE_TIME_MS = 2 * 1000;
50   private static final long TARGET_TIME_MS = 30 * 1000;
51 
ProtoBench()52   private ProtoBench() {
53     // Prevent instantiation
54   }
55 
main(String[] args)56   public static void main(String[] args) {
57     if (args.length < 2 || (args.length % 2) != 0) {
58       System.err.println("Usage: ProtoBench <descriptor type name> <input data>");
59       System.err.println("The descriptor type name is the fully-qualified message name,");
60       System.err.println("e.g. com.google.protocolbuffers.benchmark.Message1");
61       System.err.println("(You can specify multiple pairs of descriptor type name and input data.)");
62       System.exit(1);
63     }
64     boolean success = true;
65     for (int i = 0; i < args.length; i += 2) {
66       success &= runTest(args[i], args[i + 1]);
67     }
68     System.exit(success ? 0 : 1);
69   }
70 
71   /**
72    * Runs a single test. Error messages are displayed to stderr, and the return value
73    * indicates general success/failure.
74    */
runTest(String type, String file)75   public static boolean runTest(String type, String file) {
76     System.out.println("Benchmarking " + type + " with file " + file);
77     final Message defaultMessage;
78     try {
79       Class<?> clazz = Class.forName(type);
80       Method method = clazz.getDeclaredMethod("getDefaultInstance");
81       defaultMessage = (Message) method.invoke(null);
82     } catch (Exception e) {
83       // We want to do the same thing with all exceptions. Not generally nice,
84       // but this is slightly different.
85       System.err.println("Unable to get default message for " + type);
86       return false;
87     }
88 
89     try {
90       final byte[] inputData = readAllBytes(file);
91       final ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData);
92       final ByteString inputString = ByteString.copyFrom(inputData);
93       final Message sampleMessage = defaultMessage.newBuilderForType().mergeFrom(inputString).build();
94       FileOutputStream devNullTemp = null;
95       CodedOutputStream reuseDevNullTemp = null;
96       try {
97         devNullTemp = new FileOutputStream("/dev/null");
98         reuseDevNullTemp = CodedOutputStream.newInstance(devNullTemp);
99       } catch (FileNotFoundException e) {
100         // ignore: this is probably Windows, where /dev/null does not exist
101       }
102       final FileOutputStream devNull = devNullTemp;
103       final CodedOutputStream reuseDevNull = reuseDevNullTemp;
104       benchmark("Serialize to byte string", inputData.length, new Action() {
105         public void execute() { sampleMessage.toByteString(); }
106       });
107       benchmark("Serialize to byte array", inputData.length, new Action() {
108         public void execute() { sampleMessage.toByteArray(); }
109       });
110       benchmark("Serialize to memory stream", inputData.length, new Action() {
111         public void execute() throws IOException {
112           sampleMessage.writeTo(new ByteArrayOutputStream());
113         }
114       });
115       if (devNull != null) {
116         benchmark("Serialize to /dev/null with FileOutputStream", inputData.length, new Action() {
117           public void execute() throws IOException {
118             sampleMessage.writeTo(devNull);
119           }
120         });
121         benchmark("Serialize to /dev/null reusing FileOutputStream", inputData.length, new Action() {
122           public void execute() throws IOException {
123             sampleMessage.writeTo(reuseDevNull);
124             reuseDevNull.flush();  // force the write to the OutputStream
125           }
126         });
127       }
128       benchmark("Deserialize from byte string", inputData.length, new Action() {
129         public void execute() throws IOException {
130           defaultMessage.newBuilderForType().mergeFrom(inputString).build();
131         }
132       });
133       benchmark("Deserialize from byte array", inputData.length, new Action() {
134         public void execute() throws IOException {
135           defaultMessage.newBuilderForType()
136             .mergeFrom(CodedInputStream.newInstance(inputData)).build();
137         }
138       });
139       benchmark("Deserialize from memory stream", inputData.length, new Action() {
140         public void execute() throws IOException {
141           defaultMessage.newBuilderForType()
142             .mergeFrom(CodedInputStream.newInstance(inputStream)).build();
143           inputStream.reset();
144         }
145       });
146       System.out.println();
147       return true;
148     } catch (Exception e) {
149       System.err.println("Error: " + e.getMessage());
150       System.err.println("Detailed exception information:");
151       e.printStackTrace(System.err);
152       return false;
153     }
154   }
155 
benchmark(String name, long dataSize, Action action)156   private static void benchmark(String name, long dataSize, Action action) throws IOException {
157     // Make sure it's JITted "reasonably" hard before running the first progress test
158     for (int i=0; i < 100; i++) {
159       action.execute();
160     }
161 
162     // Run it progressively more times until we've got a reasonable sample
163     int iterations = 1;
164     long elapsed = timeAction(action, iterations);
165     while (elapsed < MIN_SAMPLE_TIME_MS) {
166       iterations *= 2;
167       elapsed = timeAction(action, iterations);
168     }
169 
170     // Upscale the sample to the target time. Do this in floating point arithmetic
171     // to avoid overflow issues.
172     iterations = (int) ((TARGET_TIME_MS / (double) elapsed) * iterations);
173     elapsed = timeAction(action, iterations);
174     System.out.println(name + ": " + iterations + " iterations in "
175          + (elapsed/1000f) + "s; "
176          + (iterations * dataSize) / (elapsed * 1024 * 1024 / 1000f)
177          + "MB/s");
178   }
179 
timeAction(Action action, int iterations)180   private static long timeAction(Action action, int iterations) throws IOException {
181     System.gc();
182     long start = System.currentTimeMillis();
183     for (int i = 0; i < iterations; i++) {
184       action.execute();
185     }
186     long end = System.currentTimeMillis();
187     return end - start;
188   }
189 
readAllBytes(String filename)190   private static byte[] readAllBytes(String filename) throws IOException {
191     RandomAccessFile file = new RandomAccessFile(new File(filename), "r");
192     byte[] content = new byte[(int) file.length()];
193     file.readFully(content);
194     return content;
195   }
196 
197   /**
198    * Interface used to capture a single action to benchmark.
199    */
200   interface Action {
execute()201     void execute() throws IOException;
202   }
203 }
204