| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | [Serializable] |
| | 8 | | public class BenchmarkResult |
| | 9 | | { |
| | 10 | | // metrics based on this // https://github.com/bestiejs/benchmark.js/blob/master/benchmark.js#L1912 |
| | 11 | | private const double T_TABLE_INFINITY = 1.96; |
| 0 | 12 | | private static readonly IReadOnlyDictionary<int, double> tTable = new Dictionary<int, double>() |
| | 13 | | { |
| | 14 | | { 01, 12.706 }, { 02, 4.303 }, { 03, 3.182 }, |
| | 15 | | { 04, 02.776 }, { 05, 2.571 }, { 06, 2.447 }, |
| | 16 | | { 07, 02.365 }, { 08, 2.306 }, { 09, 2.262 }, |
| | 17 | | { 10, 02.228 }, { 11, 2.201 }, { 12, 2.179 }, |
| | 18 | | { 13, 02.160 }, { 14, 2.145 }, { 15, 2.131 }, |
| | 19 | | { 16, 02.120 }, { 17, 2.110 }, { 18, 2.101 }, |
| | 20 | | { 19, 02.093 }, { 20, 2.086 }, { 21, 2.080 }, |
| | 21 | | { 22, 02.074 }, { 23, 2.069 }, { 24, 2.064 }, |
| | 22 | | { 25, 02.060 }, { 26, 2.056 }, { 27, 2.052 }, |
| | 23 | | { 28, 02.048 }, { 29, 2.045 }, { 30, 2.042 } |
| | 24 | | }; |
| | 25 | |
|
| | 26 | | public double min; |
| | 27 | | public double max; |
| | 28 | | public double moe; // Margin of error |
| | 29 | | public double rme = 0; // Relative Margin of Error |
| | 30 | | public double sem; // Mean standard error |
| | 31 | | public double stdDev; |
| | 32 | | public double mean; |
| | 33 | | public double variance; |
| | 34 | | private double[] samples; |
| | 35 | |
|
| 0 | 36 | | public BenchmarkResult(double[] samples) |
| | 37 | | { |
| 0 | 38 | | SetSamples(samples); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | // Note (Kinerius): This code is lazy and performs poorly, please optimize if used outside our debugging tools |
| | 42 | | public void SetSamples(double[] samples) |
| | 43 | | { |
| 0 | 44 | | this.samples = samples; |
| 0 | 45 | | int sampleSize = samples.Length; |
| | 46 | |
|
| 0 | 47 | | min = samples.Min(); |
| 0 | 48 | | max = samples.Max(); |
| 0 | 49 | | mean = samples.Average(); |
| 0 | 50 | | variance = Variance(samples); |
| 0 | 51 | | stdDev = StdDev(variance); |
| 0 | 52 | | sem = stdDev / Math.Sqrt(sampleSize); |
| | 53 | | // Compute the degrees of freedom. |
| 0 | 54 | | int df = sampleSize - 1; |
| | 55 | | // Compute the critical value. |
| 0 | 56 | | int tTableIndex = Math.Max(df, 1); //Minimum index is 1; |
| 0 | 57 | | if (!tTable.TryGetValue(tTableIndex, out double critical)) |
| 0 | 58 | | critical = T_TABLE_INFINITY; // we take the infinity |
| | 59 | |
|
| 0 | 60 | | moe = sem * critical; |
| 0 | 61 | | if (mean != 0) |
| 0 | 62 | | rme = (moe / mean) * 100; |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | private static double Variance(double[] samples) |
| | 66 | | { |
| 0 | 67 | | if (samples.Length <= 1) |
| 0 | 68 | | return 0.0; |
| | 69 | |
|
| 0 | 70 | | double avg = samples.Average(); |
| 0 | 71 | | double variance = samples.Sum(value => (value - avg) * (value - avg)); |
| 0 | 72 | | return variance / samples.Length; |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | private static double StdDev(double variance) { return Math.Sqrt(variance); } |
| | 76 | | } |
| | 77 | | } |