|
| 1 | +using System.Text; |
| 2 | +using Baseline; |
| 3 | +using BenchmarkDotNet.Attributes; |
| 4 | +using BenchmarkDotNet.Diagnosers; |
| 5 | +using Org.SbeTool.Sbe.Dll; |
| 6 | + |
| 7 | +namespace Org.SbeTool.Sbe.Benchmarks.Bench.Benchmarks |
| 8 | +{ |
| 9 | + [MemoryDiagnoser] |
| 10 | + public class CarBenchmark |
| 11 | + { |
| 12 | + // string values are deliberately encoded outside of the benchmark |
| 13 | + private static readonly byte[] VehicleCode = Encoding.GetEncoding(Car.VehicleCodeCharacterEncoding).GetBytes("CODE12"); |
| 14 | + private static readonly byte[] ManufacturerCode = Encoding.GetEncoding(Engine.ManufacturerCodeCharacterEncoding).GetBytes("123"); |
| 15 | + private static readonly byte[] Manufacturer = Encoding.GetEncoding(Car.ManufacturerCharacterEncoding).GetBytes("Honda"); |
| 16 | + private static readonly byte[] Model = Encoding.GetEncoding(Car.ModelCharacterEncoding).GetBytes("Civic VTi"); |
| 17 | + private static readonly byte[] ActivationCode = Encoding.GetEncoding(Car.ActivationCodeCharacterEncoding).GetBytes("code"); |
| 18 | + private static readonly byte[] UrbanCycle = Encoding.GetEncoding(Car.FuelFiguresGroup.UsageDescriptionCharacterEncoding).GetBytes("Urban Cycle"); |
| 19 | + private static readonly byte[] CombinedCycle = Encoding.GetEncoding(Car.FuelFiguresGroup.UsageDescriptionCharacterEncoding).GetBytes("Combined Cycle"); |
| 20 | + private static readonly byte[] HighwayCycle = Encoding.GetEncoding(Car.FuelFiguresGroup.UsageDescriptionCharacterEncoding).GetBytes("Highway Cycle"); |
| 21 | + |
| 22 | + private readonly byte[] _eBuffer = new byte[1024]; |
| 23 | + private readonly byte[] _dBuffer = new byte[1024]; |
| 24 | + private DirectBuffer _encodeBuffer; |
| 25 | + private DirectBuffer _decodeBuffer; |
| 26 | + private readonly Car _car = new Car(); |
| 27 | + private readonly MessageHeader _messageHeader = new MessageHeader(); |
| 28 | + |
| 29 | + [GlobalSetup] |
| 30 | + public void Setup() |
| 31 | + { |
| 32 | + _encodeBuffer = new DirectBuffer(_eBuffer); |
| 33 | + _decodeBuffer = new DirectBuffer(_dBuffer); |
| 34 | + Encode(_car, _decodeBuffer, 0); |
| 35 | + } |
| 36 | + |
| 37 | + [Benchmark] |
| 38 | + public int Encode() |
| 39 | + { |
| 40 | + return Encode(_car, _encodeBuffer, 0); |
| 41 | + } |
| 42 | + |
| 43 | + [Benchmark] |
| 44 | + public int Decode() |
| 45 | + { |
| 46 | + return Decode(_car, _decodeBuffer, 0); |
| 47 | + } |
| 48 | + |
| 49 | + public int Encode(Car car, DirectBuffer directBuffer, int bufferOffset) |
| 50 | + { |
| 51 | + _car.WrapForEncodeAndApplyHeader(directBuffer, bufferOffset, _messageHeader); |
| 52 | + |
| 53 | + const int srcOffset = 0; |
| 54 | + |
| 55 | + car.SerialNumber = 1234; |
| 56 | + car.ModelYear = 2013; |
| 57 | + car.Available = BooleanType.T; |
| 58 | + car.Code = Baseline.Model.A; |
| 59 | + car.SetVehicleCode(VehicleCode, srcOffset); |
| 60 | + |
| 61 | + for (int i = 0, size = Car.SomeNumbersLength; i < size; i++) |
| 62 | + { |
| 63 | + car.SetSomeNumbers(i, (uint)i); |
| 64 | + } |
| 65 | + |
| 66 | + car.Extras = OptionalExtras.CruiseControl | OptionalExtras.SportsPack; |
| 67 | + car.Engine.Capacity = 2000; |
| 68 | + car.Engine.NumCylinders = 4; |
| 69 | + car.Engine.SetManufacturerCode(ManufacturerCode, srcOffset); |
| 70 | + car.Engine.Efficiency = 35; |
| 71 | + car.Engine.BoosterEnabled = BooleanType.T; |
| 72 | + car.Engine.Booster.BoostType = BoostType.NITROUS; |
| 73 | + car.Engine.Booster.HorsePower = 200; |
| 74 | + |
| 75 | + var fuelFigures = car.FuelFiguresCount(3); |
| 76 | + fuelFigures.Next(); |
| 77 | + fuelFigures.Speed = 30; |
| 78 | + fuelFigures.Mpg = 35.9f; |
| 79 | + fuelFigures.SetUsageDescription(UrbanCycle); |
| 80 | + |
| 81 | + fuelFigures.Next(); |
| 82 | + fuelFigures.Speed = 55; |
| 83 | + fuelFigures.Mpg = 49.0f; |
| 84 | + fuelFigures.SetUsageDescription(CombinedCycle); |
| 85 | + |
| 86 | + fuelFigures.Next(); |
| 87 | + fuelFigures.Speed = 75; |
| 88 | + fuelFigures.Mpg = 40.0f; |
| 89 | + fuelFigures.SetUsageDescription(HighwayCycle); |
| 90 | + |
| 91 | + |
| 92 | + Car.PerformanceFiguresGroup perfFigures = car.PerformanceFiguresCount(2); |
| 93 | + perfFigures.Next(); |
| 94 | + perfFigures.OctaneRating = 95; |
| 95 | + |
| 96 | + Car.PerformanceFiguresGroup.AccelerationGroup acceleration = perfFigures.AccelerationCount(3).Next(); |
| 97 | + acceleration.Mph = 30; |
| 98 | + acceleration.Seconds = 4.0f; |
| 99 | + |
| 100 | + acceleration.Next(); |
| 101 | + acceleration.Mph = 60; |
| 102 | + acceleration.Seconds = 7.5f; |
| 103 | + |
| 104 | + acceleration.Next(); |
| 105 | + acceleration.Mph = 100; |
| 106 | + acceleration.Seconds = 12.2f; |
| 107 | + |
| 108 | + perfFigures.Next(); |
| 109 | + perfFigures.OctaneRating = 99; |
| 110 | + acceleration = perfFigures.AccelerationCount(3).Next(); |
| 111 | + |
| 112 | + acceleration.Mph = 30; |
| 113 | + acceleration.Seconds = 3.8f; |
| 114 | + |
| 115 | + acceleration.Next(); |
| 116 | + acceleration.Mph = 60; |
| 117 | + acceleration.Seconds = 7.1f; |
| 118 | + |
| 119 | + acceleration.Next(); |
| 120 | + acceleration.Mph = 100; |
| 121 | + acceleration.Seconds = 11.8f; |
| 122 | + |
| 123 | + car.SetManufacturer(Manufacturer, srcOffset, Manufacturer.Length); |
| 124 | + car.SetModel(Model, srcOffset, Model.Length); |
| 125 | + car.SetActivationCode(ActivationCode, srcOffset, ActivationCode.Length); |
| 126 | + |
| 127 | + return car.Size; |
| 128 | + } |
| 129 | + |
| 130 | + private readonly byte[] _buffer = new byte[128]; |
| 131 | + |
| 132 | + public int Decode(Car car, DirectBuffer directBuffer, int bufferOffset) |
| 133 | + { |
| 134 | + _messageHeader.Wrap(directBuffer, bufferOffset, 0); |
| 135 | + |
| 136 | + car.WrapForDecode(directBuffer, bufferOffset + MessageHeader.Size, _messageHeader.BlockLength, _messageHeader.Version); |
| 137 | + |
| 138 | + var templateId = Car.TemplateId; |
| 139 | + var schemaVersion = Car.SchemaVersion; |
| 140 | + var serialNumber = car.SerialNumber; |
| 141 | + var modelYear = car.ModelYear; |
| 142 | + var available = car.Available; |
| 143 | + var code = car.Code; |
| 144 | + |
| 145 | + for (int i = 0, size = Car.SomeNumbersLength; i < size; i++) |
| 146 | + { |
| 147 | + var number = car.GetSomeNumbers(i); |
| 148 | + } |
| 149 | + |
| 150 | + // strings are not actually decoded, only copied out into a buffer |
| 151 | + car.GetVehicleCode(_buffer, 0); |
| 152 | + |
| 153 | + OptionalExtras extras = car.Extras; |
| 154 | + var cruiseControl = (extras & OptionalExtras.CruiseControl) == OptionalExtras.CruiseControl; |
| 155 | + var sportsPack = (extras & OptionalExtras.SportsPack) == OptionalExtras.SportsPack; |
| 156 | + var sunRoof = (extras & OptionalExtras.SunRoof) == OptionalExtras.SunRoof; |
| 157 | + |
| 158 | + Engine engine = car.Engine; |
| 159 | + var capacity = engine.Capacity; |
| 160 | + var numCylinders = engine.NumCylinders; |
| 161 | + var maxRpm = engine.MaxRpm; |
| 162 | + for (int i = 0, size = Engine.ManufacturerCodeLength; i < size; i++) |
| 163 | + { |
| 164 | + engine.GetManufacturerCode(i); |
| 165 | + } |
| 166 | + |
| 167 | + int length = engine.GetFuel(_buffer, 0, _buffer.Length); |
| 168 | + |
| 169 | + var efficiency = engine.Efficiency; |
| 170 | + var boosterEnabled = engine.BoosterEnabled; |
| 171 | + var boostType = engine.Booster.BoostType; |
| 172 | + var horsePower = engine.Booster.HorsePower; |
| 173 | + |
| 174 | + var fuelFiguresGroup = car.FuelFigures; |
| 175 | + while (fuelFiguresGroup.HasNext) |
| 176 | + { |
| 177 | + var fuelFigures = fuelFiguresGroup.Next(); |
| 178 | + var speed = fuelFigures.Speed; |
| 179 | + var mpg = fuelFigures.Mpg; |
| 180 | + fuelFigures.GetUsageDescription(_buffer, 0, _buffer.Length); |
| 181 | + } |
| 182 | + |
| 183 | + var performanceFiguresGroup = car.PerformanceFigures; |
| 184 | + while (performanceFiguresGroup.HasNext) |
| 185 | + { |
| 186 | + performanceFiguresGroup.Next(); |
| 187 | + var octanceRating = performanceFiguresGroup.OctaneRating; |
| 188 | + |
| 189 | + var accelerationGroup = performanceFiguresGroup.Acceleration; |
| 190 | + for (int i = 0; i < accelerationGroup.Count; i++) |
| 191 | + { |
| 192 | + var acceleration = accelerationGroup.Next(); |
| 193 | + var mpg = acceleration.Mph; |
| 194 | + var seconds = acceleration.Seconds; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + length = car.GetManufacturer(_buffer, 0, _buffer.Length); |
| 199 | + length = car.GetModel(_buffer, 0, _buffer.Length); |
| 200 | + length = car.GetActivationCode(_buffer, 0, _buffer.Length); |
| 201 | + |
| 202 | + return car.Size; |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments