public void ShouldCacheInvocationWithNullArgs() { Func <string, string> expensiveInvocationFunc1 = delegate(string arg) { Thread.Sleep(765); return("Cowabunga-" + arg + "!"); }; IMemoizer <string, string> expensiveInvocationFuncMemoizer1 = expensiveInvocationFunc1.CreateMemoizer(); Func <string, string, string, string> expensiveInvocationFunc3 = delegate(string arg1, string arg2, string arg3) { Thread.Sleep(765); return("Deadly-" + arg1 + "-" + arg2 + "-" + arg3 + "!"); }; IMemoizer <string, string, string, string> expensiveInvocationFuncMemoizer3 = expensiveInvocationFunc3.CreateMemoizer(); // 1 arg long startTime = DateTime.Now.Ticks; string retVal = expensiveInvocationFuncMemoizer1.InvokeWith(null); long durationInTicks = DateTime.Now.Ticks - startTime; long durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Cowabunga-!")); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(765)); startTime = DateTime.Now.Ticks; retVal = expensiveInvocationFuncMemoizer1.InvokeWith(null); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Cowabunga-!")); Assert.That(durationInMilliseconds, Is.LessThan(25)); // 3 args startTime = DateTime.Now.Ticks; retVal = expensiveInvocationFuncMemoizer3.InvokeWith(null, null, null); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Deadly---!")); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(765)); startTime = DateTime.Now.Ticks; retVal = expensiveInvocationFuncMemoizer3.InvokeWith(null, "gaga", null); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Deadly--gaga-!")); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(765)); startTime = DateTime.Now.Ticks; retVal = expensiveInvocationFuncMemoizer3.InvokeWith(null, null, null); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Deadly---!")); Assert.That(durationInMilliseconds, Is.LessThan(25)); startTime = DateTime.Now.Ticks; retVal = expensiveInvocationFuncMemoizer3.InvokeWith(null, "gaga", null); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Deadly--gaga-!")); Assert.That(durationInMilliseconds, Is.LessThan(25)); }
public void ExceptionsShouldBubbleAllTheWay2() { Func <long, string> exceptionInvocationFunc = delegate { throw new ArgumentException("Aiii!"); }; IMemoizer <long, string> memoizer = exceptionInvocationFunc.CacheFor(1).Seconds.GetMemoizer(); memoizer.InvokeWith(42L); }
public void SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer() { long startTime = DateTime.Now.Ticks; IMemoizer <string, long, string> memoizer = reallySlowNetworkInvocation1b.CacheFor(NETWORK_RESPONSE_LATENCY_IN_MILLIS * 3).Milliseconds.GetMemoizer(); // New function value, not yet cached var retVal = memoizer.InvokeWith("SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer", 15L); Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + "SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer" + 15L)); long durationInMilliseconds = (DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond; Assert.That(durationInMilliseconds, Is.InRange(NETWORK_RESPONSE_LATENCY_IN_MILLIS, NETWORK_RESPONSE_LATENCY_IN_MILLIS + 50L)); Console.WriteLine("SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer: first non-memoized method invocation with latency " + NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms took " + durationInMilliseconds + " ms (should take >= " + NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms)"); // Memoized function within time span, should use cached value startTime = DateTime.Now.Ticks; retVal = memoizer.InvokeWith("SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer", 15L); Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + "SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer" + 15L)); durationInMilliseconds = (DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond; Assert.That(durationInMilliseconds, Is.LessThan(10L)); Console.WriteLine("SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer: second memoized method invocation with latency " + NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms took " + durationInMilliseconds + " ms (should take < 10 ms)"); // New function value, not yet cached startTime = DateTime.Now.Ticks; retVal = memoizer.InvokeWith("SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer", 16L); Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + "SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer" + 16L)); durationInMilliseconds = (DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond; Assert.That(durationInMilliseconds, Is.InRange(NETWORK_RESPONSE_LATENCY_IN_MILLIS, NETWORK_RESPONSE_LATENCY_IN_MILLIS + 50L)); Console.WriteLine("SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer: another first non-memoized method invocation with latency " + NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms took " + durationInMilliseconds + " ms (should take >= " + NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms)"); Console.WriteLine("SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer: waiting for memoizer cache item evictions ..."); Thread.Sleep(NETWORK_RESPONSE_LATENCY_IN_MILLIS * 3); // Memoized function evicted due to exceeded time span, should take its time (for re-memoization) startTime = DateTime.Now.Ticks; retVal = memoizer.InvokeWith("SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer", 15L); Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + "SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer" + 15L)); durationInMilliseconds = (DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond; Assert.That(durationInMilliseconds, Is.InRange(NETWORK_RESPONSE_LATENCY_IN_MILLIS, NETWORK_RESPONSE_LATENCY_IN_MILLIS + 50L)); Console.WriteLine("SingleThreadedMemoizedDirectInvocationWithPolicy_Memoizer: third memoized (but evicted) method invocation with latency " + NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms took " + durationInMilliseconds + " ms (should take >= " + NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms)"); }
public void ShouldBuildFullBlownMemoizerWithOnelineAndStillGetMemoization() { long startTime = DateTime.Now.Ticks; MemoizerFactory <long, long> slowSquareMemoizerFactory = slow500Square.Memoize(); IMemoizer <long, long> memoizedSlowSquare = slowSquareMemoizerFactory.GetMemoizer(); long durationInTicks = DateTime.Now.Ticks - startTime; Console.WriteLine("Memoizer construction: square.Memoize().Get() took " + durationInTicks / 10000 + " ms | " + durationInTicks + " ticks]"); Assert.That(durationInTicks / 10000, Is.LessThan(20)); // ms (MemoizerFactory and Memoizer creation) startTime = DateTime.Now.Ticks; long result = memoizedSlowSquare.InvokeWith(123); durationInTicks = DateTime.Now.Ticks - startTime; Console.WriteLine("Square(123) = " + result + " [memoized first time invocation took " + durationInTicks / 10000 + " ms | " + durationInTicks + " ticks]"); Assert.That(durationInTicks / 10000, Is.GreaterThanOrEqualTo(500)); // ms (not memoized invocation) startTime = DateTime.Now.Ticks; result = memoizedSlowSquare.InvokeWith(123); durationInTicks = DateTime.Now.Ticks - startTime; Console.WriteLine("Square(123) = " + result + " [memoized second time invocation took " + durationInTicks / 10000 + " ms | " + durationInTicks + " ticks]"); Assert.That(durationInTicks / 10000, Is.LessThan(10)); // ms (memoized invocation) slowSquareMemoizerFactory.InstrumentWith(Console.WriteLine); IMemoizer <long, long> memoizedSlowSquare2 = slowSquareMemoizerFactory.GetMemoizer(); startTime = DateTime.Now.Ticks; result = slowSquareMemoizerFactory.GetMemoizer().InvokeWith(123); //result = memoizedSlowSquare2.InvokeWith(123); durationInTicks = DateTime.Now.Ticks - startTime; Console.WriteLine("Square(123) = " + result + " [memoized first time (instrumented) invocation took " + durationInTicks / 10000 + " ms | " + durationInTicks + " ticks]"); Assert.That(durationInTicks / 10000, Is.LessThan(10)); // ms (memoized invocation) startTime = DateTime.Now.Ticks; result = memoizedSlowSquare2.InvokeWith(123); durationInTicks = DateTime.Now.Ticks - startTime; Console.WriteLine("Square(123) = " + result + " [memoized second time (instrumented) invocation took " + durationInTicks / 10000 + " ms | " + durationInTicks + " ticks]"); Assert.That(durationInTicks / 10000, Is.LessThan(10)); // ms (memoized invocation) }
public void MultipleCacheItemPolicies_MemoizerFactoryIsMutable___OnGoing() { MemoizerFactory <long, string> myMessyMemoizerFactory = typicalDatabaseInvocation_InlinedFunc2.CacheFor(12).Hours .InstrumentWith(Console.WriteLine); IMemoizer <long, string> myMemoizedFunction = myMessyMemoizerFactory.GetMemoizer(); //Assert.That(LatencyInstrumentedRun(myMessyMemoizerFactory, DATABASE_RESPONSE_LATENCY_IN_MILLIS, "First method invocation", "(should take > " + DATABASE_RESPONSE_LATENCY_IN_MILLIS + " ms)"), // Is.GreaterThanOrEqualTo(DATABASE_RESPONSE_LATENCY_IN_MILLIS)); long startTime = DateTime.Now.Ticks; string retVal = myMemoizedFunction.InvokeWith(4224L); long durationInTicks = DateTime.Now.Ticks - startTime; long durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + 4224L)); Console.WriteLine("(4224) invocation with latency " + durationInMilliseconds + " ms took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks " + "(should take > " + DATABASE_RESPONSE_LATENCY_IN_MILLIS + " ms)"); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(DATABASE_RESPONSE_LATENCY_IN_MILLIS)); //Assert.That(LatencyInstrumentedRun(myMessyMemoizerFactory, DATABASE_RESPONSE_LATENCY_IN_MILLIS, "Second method invocation", "(cached, should take < 5 ms)"), // Is.LessThan(20)); // ms (20 ms just for LatencyInstrumentedRun method to finish...) startTime = DateTime.Now.Ticks; retVal = myMemoizedFunction.InvokeWith(4224L); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + 4224L)); Console.WriteLine("(4224) invocation with latency " + durationInMilliseconds + " ms took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks " + "(cached, should take < 5 ms)"); // TODO: hmm, takes its time... Assert.That(durationInMilliseconds, Is.LessThan(30)); // ms myMessyMemoizerFactory = myMessyMemoizerFactory.KeepItemsCachedFor(0).Milliseconds .InstrumentWith(Console.WriteLine) .KeepItemsCachedFor(120).Milliseconds; IMemoizer <long, string> myMemoizedFunction2 = myMessyMemoizerFactory.GetMemoizer(); //Assert.That(LatencyInstrumentedRun(myMessyMemoizerFactory, DATABASE_RESPONSE_LATENCY_IN_MILLIS, "Second method invocation", "(cached, should take < 5 ms)"), // Is.LessThan(20)); // ms (20 ms just for LatencyInstrumentedRun method to finish...) startTime = DateTime.Now.Ticks; retVal = myMemoizedFunction2.InvokeWith(123456L); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + 123456L)); Console.WriteLine("(123456) invocation with latency " + durationInMilliseconds + " ms took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks " + "(should take > " + DATABASE_RESPONSE_LATENCY_IN_MILLIS + " ms)"); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(DATABASE_RESPONSE_LATENCY_IN_MILLIS)); startTime = DateTime.Now.Ticks; retVal = myMemoizedFunction2.InvokeWith(123456); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + 123456)); Console.WriteLine("(123456) invocation with latency " + durationInMilliseconds + " ms took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks " + "(cached, should take < 5 ms)"); Assert.That(durationInMilliseconds, Is.LessThan(5)); // ms Thread.Sleep(300); //Assert.That(LatencyInstrumentedRun(myMessyMemoizerFactory, DATABASE_RESPONSE_LATENCY_IN_MILLIS, "Third method invocation", "(cached, should take < 5 ms)"), // Is.LessThan(20)); // ms (20 ms just for LatencyInstrumentedRun method to finish...) // Previous memoizer still intact startTime = DateTime.Now.Ticks; retVal = myMemoizedFunction.InvokeWith(4224L); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + 4224L)); Console.WriteLine("(4224) invocation with latency " + durationInMilliseconds + " ms took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks " + "(cached, should take < 5 ms)"); Assert.That(durationInMilliseconds, Is.LessThan(5)); // ms startTime = DateTime.Now.Ticks; retVal = myMemoizedFunction2.InvokeWith(123456L); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo(METHOD_RESPONSE_ELEMENT + 123456L)); Console.WriteLine("(123456L) invocation with latency " + durationInMilliseconds + " ms took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks " + "(should take > " + DATABASE_RESPONSE_LATENCY_IN_MILLIS + " ms)"); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(DATABASE_RESPONSE_LATENCY_IN_MILLIS)); }
string ExpensiveFunctionWithExpiration2(long someId) { return(myExpensiveFunctionMemoizer.InvokeWith(someId)); }