|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Castle.DynamicProxy; |
| 9 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 10 | +using Proxii.Library.Interceptors; |
| 11 | +using Proxii.Test.Integration.Interceptors; |
| 12 | + |
| 13 | +namespace Proxii.Test.e2e |
| 14 | +{ |
| 15 | + [TestClass] |
| 16 | + public class ProxiiBenchmarkTest |
| 17 | + { |
| 18 | + // TODO encapsulate benchmark test behavior in its own class |
| 19 | + |
| 20 | + // ensure it's within 1 microsecond of the correct timing |
| 21 | + private const double TimingEpsilon = .005; |
| 22 | + |
| 23 | + [TestMethod] |
| 24 | + public void Proxii_Benchmark_RecordsAccurateTiming() |
| 25 | + { |
| 26 | + // so we can independently take the two timings |
| 27 | + double normalTiming = 0, proxyTiming = 0; |
| 28 | + |
| 29 | + Action<double> proxyTimingAction = (d) => proxyTiming = d; |
| 30 | + |
| 31 | + // create the object normally |
| 32 | + var normalObject = new BenchmarkTestObject(); |
| 33 | + |
| 34 | + // create a proxy |
| 35 | + var proxy = Proxii.Proxy<IBenchmarkTestObject, BenchmarkTestObject>() |
| 36 | + .Benchmark(proxyTimingAction) |
| 37 | + .Create(); |
| 38 | + |
| 39 | + // take timing via interceptor |
| 40 | + proxy.Do(); |
| 41 | + |
| 42 | + // take timing manually |
| 43 | + normalTiming = TakeDoTiming(normalObject); |
| 44 | + |
| 45 | + // ensure the interceptor at least called the action |
| 46 | + Assert.AreNotEqual(0, proxyTiming, "proxy timing is set"); |
| 47 | + |
| 48 | + // ensure the interceptor gave an accurate timing |
| 49 | + Assert.IsTrue(EpsilonEqual(TimingEpsilon, normalTiming, proxyTiming)); |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + public void BenchmarkInterceptor_CallsWithCorrectMethodInfo() |
| 54 | + { |
| 55 | + string result = null; |
| 56 | + |
| 57 | + Action<double, MethodInfo> proxyTimingAction = (d, m) => result = m.Name; |
| 58 | + |
| 59 | + // create a proxy |
| 60 | + var proxy = Proxii.Proxy<IBenchmarkTestObject, BenchmarkTestObject>() |
| 61 | + .Benchmark(proxyTimingAction) |
| 62 | + .Create(); |
| 63 | + |
| 64 | + proxy.Do(); |
| 65 | + |
| 66 | + Assert.AreEqual("Do", result); |
| 67 | + } |
| 68 | + |
| 69 | + [TestMethod] |
| 70 | + public void BenchmarkInterceptor_CallsWithCorrectMethodInfoAndArguments() |
| 71 | + { |
| 72 | + string result = null; |
| 73 | + |
| 74 | + Action<double, MethodInfo, object[]> proxyTimingAction = (d, m, args) => result = $"called method {m.Name} with arguments ({string.Join(", ", args)})"; |
| 75 | + |
| 76 | + // create a proxy |
| 77 | + var proxy = Proxii.Proxy<IBenchmarkTestObject, BenchmarkTestObject>() |
| 78 | + .Benchmark(proxyTimingAction) |
| 79 | + .Create(); |
| 80 | + |
| 81 | + proxy.Do(1, "foo"); |
| 82 | + |
| 83 | + Assert.AreEqual("called method Do with arguments (1, foo)", result); |
| 84 | + } |
| 85 | + |
| 86 | + private double TakeDoTiming(IBenchmarkTestObject obj) |
| 87 | + { |
| 88 | + double timing; |
| 89 | + |
| 90 | + var stopwatch = Stopwatch.StartNew(); |
| 91 | + |
| 92 | + // time the proxy |
| 93 | + try |
| 94 | + { |
| 95 | + obj.Do(); |
| 96 | + } |
| 97 | + finally |
| 98 | + { |
| 99 | + stopwatch.Stop(); |
| 100 | + timing = stopwatch.ElapsedMilliseconds; |
| 101 | + } |
| 102 | + |
| 103 | + return timing; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// compares two doubles for equality, allowing for a maximum difference of epsilon |
| 108 | + /// </summary> |
| 109 | + /// <param name="epsilon">maximum difference allowed for the numbers to be considered equal (inclusive)</param> |
| 110 | + /// <param name="a"></param> |
| 111 | + /// <param name="b"></param> |
| 112 | + /// <returns>true if the numbers fall within epsilon range of one another</returns> |
| 113 | + private bool EpsilonEqual(double epsilon, double a, double b) |
| 114 | + { |
| 115 | + return Math.Abs(a - b) <= epsilon; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments