public VeryExpensiveMemoizedServiceCallTask_DictionaryMemoizer(Barrier barrier, DictionaryMemoizer <string> memoizer, string stringArg, long longArg) : base(barrier) { TaskNumber = Interlocked.Increment(ref TASK_COUNTER); ParticipantNumber = Interlocked.Increment(ref PARTICIPANT_COUNTER); this.memoizer = memoizer; Action = () => this.Result = this.memoizer.Invoke(MemoizerTests.ReallySlowNetworkStaticInvocation, stringArg, longArg); if (Instrumented) { Console.WriteLine(this.GetType().Name + " #" + TaskNumber + " created... [(possible) barrier participant #" + ParticipantNumber + "]"); } }
public void SingleThreadedMemoizedInvocation_DictionaryMemoizer_WithLocks() { long startTime = DateTime.Now.Ticks; DictionaryMemoizer <string> memoizer = new DictionaryMemoizer <string>(); for (int i = 0; i < MemoizerTests.NUMBER_OF_ITERATIONS; ++i) { for (int j = 0; j < MemoizerTests.NUMBER_OF_CONCURRENT_TASKS; ++j) { string retVal = memoizer.Invoke(MemoizerTests.ReallySlowNetworkStaticInvocation, "SingleThreadedMemoizedInvocation_DictionaryMemoizer_WithLocks", 14L); Assert.That(retVal, Is.EqualTo(MemoizerTests.METHOD_RESPONSE_ELEMENT + "SingleThreadedMemoizedInvocation_DictionaryMemoizer_WithLocks" + 14L)); } } long durationInMilliseconds = (DateTime.Now.Ticks - startTime) / 10000; Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(MemoizerTests.NETWORK_RESPONSE_LATENCY_IN_MILLIS)); Console.WriteLine( "SingleThreadedMemoizedInvocation_DictionaryMemoizer_WithLocks: " + MemoizerTests.NUMBER_OF_ITERATIONS + " iterations of " + MemoizerTests.NUMBER_OF_CONCURRENT_TASKS + " sequential, identical, memoized method invocations with " + MemoizerTests.NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms latency" + " took " + durationInMilliseconds + " ms" + " (should take > " + MemoizerTests.NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms)"); }
public void MultiThreadedMemoizedInvocation_DictionaryMemoizer_WithLocks() { long startTime = DateTime.Now.Ticks; DictionaryMemoizer <string> memoizer = new DictionaryMemoizer <string>(); for (int i = 0; i < MemoizerTests.NUMBER_OF_ITERATIONS; ++i) { // Arrange TwoPhaseExecutor twoPhaseExecutor = new TwoPhaseExecutor(MemoizerTests.NUMBER_OF_CONCURRENT_TASKS); VeryExpensiveMemoizedServiceCallTask_DictionaryMemoizer[] tasks = new VeryExpensiveMemoizedServiceCallTask_DictionaryMemoizer[MemoizerTests.NUMBER_OF_CONCURRENT_TASKS]; for (int j = 0; j < MemoizerTests.NUMBER_OF_CONCURRENT_TASKS; ++j) { tasks[j] = new VeryExpensiveMemoizedServiceCallTask_DictionaryMemoizer(twoPhaseExecutor.Barrier, memoizer, "MultiThreadedMemoizedWithLocksInvocation", 15L); } // Act for (int j = 0; j < MemoizerTests.NUMBER_OF_CONCURRENT_TASKS; ++j) { tasks[j].Start(); } twoPhaseExecutor.Start(); // Assert for (int j = 0; j < MemoizerTests.NUMBER_OF_CONCURRENT_TASKS; ++j) { Assert.That(tasks[j].Result, Is.EqualTo(MemoizerTests.METHOD_RESPONSE_ELEMENT + "MultiThreadedMemoizedWithLocksInvocation" + 15L)); } } long durationInMilliseconds = (DateTime.Now.Ticks - startTime) / 10000; Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(MemoizerTests.NETWORK_RESPONSE_LATENCY_IN_MILLIS)); Console.WriteLine( "MultiThreadedMemoizedInvocation_DictionaryMemoizer_WithLocks: " + MemoizerTests.NUMBER_OF_CONCURRENT_TASKS + " concurrent, identical, memoized method invocations." + " " + MemoizerTests.NUMBER_OF_ITERATIONS + " iterations with " + MemoizerTests.NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms latency took " + durationInMilliseconds + " ms" + " (should take > " + MemoizerTests.NETWORK_RESPONSE_LATENCY_IN_MILLIS + " ms)."); }