|
| 1 | +/* |
| 2 | + * Copyright 2013 Real Logic Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package uk.co.real_logic.protobuf; |
| 18 | + |
| 19 | +import org.openjdk.jmh.annotations.GenerateMicroBenchmark; |
| 20 | +import org.openjdk.jmh.annotations.Scope; |
| 21 | +import org.openjdk.jmh.annotations.State; |
| 22 | +import uk.co.real_logic.protobuf.examples.Examples; |
| 23 | +import uk.co.real_logic.protobuf.examples.Examples.Car.Model; |
| 24 | +import uk.co.real_logic.protobuf.examples.Examples.PerformanceFigures; |
| 25 | + |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | + |
| 29 | +public class CarBenchmark |
| 30 | +{ |
| 31 | + private static final String VEHICLE_CODE = "abcdef"; |
| 32 | + private static final String ENG_MAN_CODE = "abc"; |
| 33 | + private static final String MAKE = "AUDI"; |
| 34 | + private static final String MODEL = "R8"; |
| 35 | + |
| 36 | + @State(Scope.Benchmark) |
| 37 | + public static class MyState |
| 38 | + { |
| 39 | + final Examples.Car.Builder car = Examples.Car.newBuilder(); |
| 40 | + final byte[] decodeBuffer; |
| 41 | + |
| 42 | + final ByteArrayInputStream in; |
| 43 | + final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 44 | + |
| 45 | + { |
| 46 | + try |
| 47 | + { |
| 48 | + encode(car, out); |
| 49 | + } |
| 50 | + catch (final Exception ex) |
| 51 | + { |
| 52 | + throw new RuntimeException(ex); |
| 53 | + } |
| 54 | + |
| 55 | + decodeBuffer = out.toByteArray(); |
| 56 | + in = new ByteArrayInputStream(decodeBuffer); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @GenerateMicroBenchmark |
| 61 | + public int testEncode(final MyState state) throws Exception |
| 62 | + { |
| 63 | + final Examples.Car.Builder car = state.car; |
| 64 | + final ByteArrayOutputStream out = state.out; |
| 65 | + |
| 66 | + return encode(car, out); |
| 67 | + } |
| 68 | + |
| 69 | + @GenerateMicroBenchmark |
| 70 | + public int testDecode(final MyState state) throws Exception |
| 71 | + { |
| 72 | + final Examples.Car.Builder car = state.car; |
| 73 | + final ByteArrayInputStream in = state.in; |
| 74 | + |
| 75 | + return decode(car, in); |
| 76 | + } |
| 77 | + |
| 78 | + private static int encode(final Examples.Car.Builder car, final ByteArrayOutputStream out) throws Exception |
| 79 | + { |
| 80 | + out.reset(); |
| 81 | + |
| 82 | + car.clear() |
| 83 | + .setCode(Model.A) |
| 84 | + .setModelYear(2005) |
| 85 | + .setSerialNumber(12345) |
| 86 | + .setAvailable(true) |
| 87 | + .setVehicleCode(VEHICLE_CODE); |
| 88 | + |
| 89 | + for (int i = 0, size = 5; i < size; i++) |
| 90 | + { |
| 91 | + car.addSomeNumbers(i); |
| 92 | + } |
| 93 | + |
| 94 | + car.addOptionalExtras(Examples.Car.Extras.SPORTS_PACK) |
| 95 | + .addOptionalExtras(Examples.Car.Extras.SUN_ROOF); |
| 96 | + |
| 97 | + car.getEngineBuilder().setCapacity(4200) |
| 98 | + .setNumCylinders(8) |
| 99 | + .setManufacturerCode(ENG_MAN_CODE); |
| 100 | + |
| 101 | + car.addFuelFiguresBuilder().setSpeed(30).setMpg(35.9F); |
| 102 | + car.addFuelFiguresBuilder().setSpeed(30).setMpg(49.0F); |
| 103 | + car.addFuelFiguresBuilder().setSpeed(30).setMpg(40.0F); |
| 104 | + |
| 105 | + final PerformanceFigures.Builder perf1 = car.addPerformanceBuilder().setOctaneRating(95); |
| 106 | + perf1.addAccelerationBuilder().setMph(30).setSeconds(4.0f); |
| 107 | + perf1.addAccelerationBuilder().setMph(60).setSeconds(7.5f); |
| 108 | + perf1.addAccelerationBuilder().setMph(100).setSeconds(12.2f); |
| 109 | + |
| 110 | + final PerformanceFigures.Builder perf2 = car.addPerformanceBuilder().setOctaneRating(95); |
| 111 | + perf2.addAccelerationBuilder().setMph(30).setSeconds(3.8f); |
| 112 | + perf2.addAccelerationBuilder().setMph(60).setSeconds(7.1f); |
| 113 | + perf2.addAccelerationBuilder().setMph(100).setSeconds(11.8f); |
| 114 | + |
| 115 | + car.setMake(MAKE); |
| 116 | + car.setModel(MODEL); |
| 117 | + |
| 118 | + car.build().writeTo(out); |
| 119 | + |
| 120 | + return out.size(); |
| 121 | + } |
| 122 | + |
| 123 | + private static int decode(final Examples.Car.Builder car, |
| 124 | + final ByteArrayInputStream in) throws Exception |
| 125 | + { |
| 126 | + in.mark(in.available()); |
| 127 | + |
| 128 | + car.clear(); |
| 129 | + car.mergeFrom(in); |
| 130 | + |
| 131 | + car.getSerialNumber(); |
| 132 | + car.getModelYear(); |
| 133 | + car.hasAvailable(); |
| 134 | + car.getCode(); |
| 135 | + |
| 136 | + for (int i = 0, size = car.getSomeNumbersCount(); i < size; i++) |
| 137 | + { |
| 138 | + car.getSomeNumbers(i); |
| 139 | + } |
| 140 | + |
| 141 | + car.getVehicleCode(); |
| 142 | + |
| 143 | + for (int i = 0, size = car.getOptionalExtrasCount(); i < size; i++) |
| 144 | + { |
| 145 | + car.getOptionalExtras(i); |
| 146 | + } |
| 147 | + |
| 148 | + final Examples.Engine engine = car.getEngine(); |
| 149 | + engine.getCapacity(); |
| 150 | + engine.getNumCylinders(); |
| 151 | + engine.getMaxRpm(); |
| 152 | + engine.getManufacturerCode(); |
| 153 | + engine.getFuel(); |
| 154 | + |
| 155 | + for (final Examples.FuelFigures fuelFigures : car.getFuelFiguresList()) |
| 156 | + { |
| 157 | + fuelFigures.getSpeed(); |
| 158 | + fuelFigures.getMpg(); |
| 159 | + } |
| 160 | + |
| 161 | + for (final PerformanceFigures performanceFigures : car.getPerformanceList()) |
| 162 | + { |
| 163 | + performanceFigures.getOctaneRating(); |
| 164 | + |
| 165 | + for (final Examples.Acceleration acceleration : performanceFigures.getAccelerationList()) |
| 166 | + { |
| 167 | + acceleration.getMph(); |
| 168 | + acceleration.getSeconds(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + car.getMake(); |
| 173 | + car.getModel(); |
| 174 | + |
| 175 | + in.reset(); |
| 176 | + |
| 177 | + return in.available(); |
| 178 | + } |
| 179 | + |
| 180 | + /* |
| 181 | + * Benchmarks to allow execution outside of JMH. |
| 182 | + */ |
| 183 | + |
| 184 | + public static void main(final String[] args) throws Exception |
| 185 | + { |
| 186 | + for (int i = 0; i < 10; i++) |
| 187 | + { |
| 188 | + perfTestEncode(i); |
| 189 | + perfTestDecode(i); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static void perfTestEncode(final int runNumber) throws Exception |
| 194 | + { |
| 195 | + final int reps = 1 * 1000 * 1000; |
| 196 | + final MyState state = new MyState(); |
| 197 | + final CarBenchmark benchmark = new CarBenchmark(); |
| 198 | + |
| 199 | + final long start = System.nanoTime(); |
| 200 | + for (int i = 0; i < reps; i++) |
| 201 | + { |
| 202 | + benchmark.testEncode(state); |
| 203 | + } |
| 204 | + |
| 205 | + final long totalDuration = System.nanoTime() - start; |
| 206 | + |
| 207 | + System.out.printf("%d - %d(ns) average duration for %s.testEncode() - message size %d\n", |
| 208 | + Integer.valueOf(runNumber), |
| 209 | + Long.valueOf(totalDuration / reps), |
| 210 | + benchmark.getClass().getName(), |
| 211 | + Integer.valueOf(state.car.getSomeNumbersCount())); |
| 212 | + } |
| 213 | + |
| 214 | + private static void perfTestDecode(final int runNumber) throws Exception |
| 215 | + { |
| 216 | + final int reps = 1 * 1000 * 1000; |
| 217 | + final MyState state = new MyState(); |
| 218 | + final CarBenchmark benchmark = new CarBenchmark(); |
| 219 | + |
| 220 | + final long start = System.nanoTime(); |
| 221 | + for (int i = 0; i < reps; i++) |
| 222 | + { |
| 223 | + benchmark.testDecode(state); |
| 224 | + } |
| 225 | + |
| 226 | + final long totalDuration = System.nanoTime() - start; |
| 227 | + |
| 228 | + System.out.printf("%d - %d(ns) average duration for %s.testDecode() - message size %d\n", |
| 229 | + Integer.valueOf(runNumber), |
| 230 | + Long.valueOf(totalDuration / reps), |
| 231 | + benchmark.getClass().getName(), |
| 232 | + Integer.valueOf(state.car.getSomeNumbersCount())); |
| 233 | + } |
| 234 | +} |
0 commit comments