|
| 1 | +using System.Linq; |
| 2 | +using BenchmarkDotNet.Attributes; |
| 3 | + |
| 4 | +namespace HdrHistogram.Benchmarking.Recording |
| 5 | +{ |
| 6 | + [Config(typeof(ExhuastiveJobWithMemoryDiagnosisConfig))] |
| 7 | + public class Recording32BitBenchmark |
| 8 | + { |
| 9 | + private readonly long[] _testValues; |
| 10 | + private readonly LongHistogram _longHistogram; |
| 11 | + private readonly IntHistogram _intHistogram; |
| 12 | + private readonly ShortHistogram _shortHistogram; |
| 13 | + private readonly Recorder _longRecorder; |
| 14 | + private readonly Recorder _intRecorder; |
| 15 | + private readonly Recorder _shortRecorder; |
| 16 | + |
| 17 | + public Recording32BitBenchmark() |
| 18 | + { |
| 19 | + //Create array of +ve numbers in the 'maxBit' bit range (i.e. 32 bit or 64bit) |
| 20 | + var highestTrackableValue = TimeStamp.Minutes(10); |
| 21 | + _testValues = Enumerable.Range(0, 32) |
| 22 | + .Select(exp => new { Value = 1L << exp, LZC = 63 - exp }) |
| 23 | + .SelectMany(x => new[] |
| 24 | + { |
| 25 | + x.Value-1, |
| 26 | + x.Value, |
| 27 | + x.Value+1, |
| 28 | + }) |
| 29 | + .Where(x => x > 0) |
| 30 | + .Where(x=>x< highestTrackableValue) |
| 31 | + .Distinct() |
| 32 | + .ToArray(); |
| 33 | + |
| 34 | + _longHistogram = new LongHistogram(highestTrackableValue, 3); |
| 35 | + _intHistogram = new IntHistogram(highestTrackableValue, 3); |
| 36 | + _shortHistogram = new ShortHistogram(highestTrackableValue, 3); |
| 37 | + |
| 38 | + _longRecorder = new Recorder(1, highestTrackableValue, 3, (id, low, hi, sf) => new LongHistogram(id, low, hi, sf)); |
| 39 | + _intRecorder = new Recorder(1, highestTrackableValue, 3, (id, low, hi, sf) => new IntHistogram(id, low, hi, sf)); |
| 40 | + _shortRecorder = new Recorder(1, highestTrackableValue, 3, (id, low, hi, sf) => new ShortHistogram(id, low, hi, sf)); |
| 41 | + } |
| 42 | + |
| 43 | + [Benchmark(Baseline = true)] |
| 44 | + public long LongHistogramRecording() |
| 45 | + { |
| 46 | + long counter = 0L; |
| 47 | + for (int i = 0; i < _testValues.Length; i++) |
| 48 | + { |
| 49 | + var value = _testValues[i]; |
| 50 | + _longHistogram.RecordValue(value); |
| 51 | + counter += value; |
| 52 | + } |
| 53 | + return counter; |
| 54 | + } |
| 55 | + |
| 56 | + [Benchmark] |
| 57 | + public long IntHistogramRecording() |
| 58 | + { |
| 59 | + long counter = 0L; |
| 60 | + for (int i = 0; i < _testValues.Length; i++) |
| 61 | + { |
| 62 | + var value = _testValues[i]; |
| 63 | + _intHistogram.RecordValue(value); |
| 64 | + counter += value; |
| 65 | + } |
| 66 | + return counter; |
| 67 | + } |
| 68 | + |
| 69 | + [Benchmark] |
| 70 | + public long ShortHistogramRecording() |
| 71 | + { |
| 72 | + for (int i = 0; i < _testValues.Length; i++) |
| 73 | + { |
| 74 | + _shortHistogram.RecordValue(_testValues[i]); |
| 75 | + } |
| 76 | + return _shortHistogram.TotalCount; |
| 77 | + } |
| 78 | + |
| 79 | + [Benchmark] |
| 80 | + public long LongRecorderRecording() |
| 81 | + { |
| 82 | + long counter = 0L; |
| 83 | + |
| 84 | + for (int i = 0; i < _testValues.Length; i++) |
| 85 | + { |
| 86 | + var value = _testValues[i]; |
| 87 | + _longRecorder.RecordValue(value); |
| 88 | + counter += value; |
| 89 | + } |
| 90 | + return counter; |
| 91 | + } |
| 92 | + |
| 93 | + [Benchmark] |
| 94 | + public long IntRecorderRecording() |
| 95 | + { |
| 96 | + long counter = 0L; |
| 97 | + for (int i = 0; i < _testValues.Length; i++) |
| 98 | + { |
| 99 | + var value = _testValues[i]; |
| 100 | + _intRecorder.RecordValue(value); |
| 101 | + counter += value; |
| 102 | + } |
| 103 | + return counter; |
| 104 | + } |
| 105 | + |
| 106 | + [Benchmark] |
| 107 | + public long ShortRecorderRecording() |
| 108 | + { |
| 109 | + long counter = 0L; |
| 110 | + for (int i = 0; i < _testValues.Length; i++) |
| 111 | + { |
| 112 | + var value = _testValues[i]; |
| 113 | + _shortRecorder.RecordValue(value); |
| 114 | + counter += value; |
| 115 | + } |
| 116 | + return counter; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments