public void CanTrace_StressTest() { // Create a rate limiter that allows .5 QPS var rateLimiter = new RateLimiter(.5, StopwatchTimer.Create()); int canTraceCounter = 0; DateTime start = DateTime.UtcNow; DateTime end = start.AddSeconds(5.5); // Create 10 threads to run for a little over five seconds. var threads = Enumerable.Range(0, 10) .Select(_ => new Thread(() => { while (DateTime.UtcNow < end) { // Avoid completely tight-looping, so that we can actually start enough threads. // (Note that Thread.Yield isn't available on .NET Core.) Thread.Sleep(1); if (rateLimiter.CanTrace()) { Interlocked.Increment(ref canTraceCounter); } } })) .ToList(); // Start the threads and wait for them all to finish threads.ForEach(t => t.Start()); threads.ForEach(t => t.Join()); // We should have exactly 3 traces: one at t~=0, one at t~=2, one at t~=4. // (The test machine would have to be very highly loaded to take half a second between // the check in the while loop and the increment leading to an over-count, and it would have // to take half a second to start the threads to lead to an under-count.) Assert.Equal(3, canTraceCounter); }
public void CanTrace_Always_StressTest() { // Create a rate limiter that allows 1_000_000 QPS, which effectively means can trace always. var rateLimiter = new RateLimiter(1_000_000, StopwatchTimer.Create()); int canTraceCounter = 0; int canTraceQuestions = 0; DateTime start = DateTime.UtcNow; DateTime end = start.AddSeconds(5.5); // Create 10 threads to run for a little over five seconds. var threads = Enumerable.Range(0, 10) .Select(_ => new Thread(() => { while (DateTime.UtcNow < end) { // Avoid completely tight-looping, so that we can actually start enough threads. // (Note that Thread.Yield isn't available on .NET Core.) Thread.Sleep(1); Interlocked.Increment(ref canTraceQuestions); if (rateLimiter.CanTrace()) { Interlocked.Increment(ref canTraceCounter); } } })) .ToList(); // Start the threads and wait for them all to finish threads.ForEach(t => t.Start()); threads.ForEach(t => t.Join()); // Since everything is to be traced, we should have as many traces as we attempted. Assert.Equal(canTraceQuestions, canTraceCounter); }
public void CanTrace_StressTest() { // Create a rate limiter that allows 1 QPS var rateLimiter = new RateLimiter(1, StopwatchTimer.Create()); int canTraceCounter = 0; DateTime start = DateTime.UtcNow; DateTime end = start.AddSeconds(2.5); // Create 10 threads to run for a little over two seconds. var threads = Enumerable.Range(0, 10) .Select(_ => new Thread(() => { while (DateTime.UtcNow < end) { if (rateLimiter.CanTrace()) { Interlocked.Increment(ref canTraceCounter); } } })) .ToList(); // Start the threads and wait for them all to finish threads.ForEach(t => t.Start()); threads.ForEach(t => t.Join()); // We should have exactly 3 traces: one at t~=0, one at t~=1, one at t~=2. // (The test machine would have to be very highly loaded to take half a second between // the check in the while loop and the increment leading to an over-count, and it would have // to take half a second to start the threads to lead to an under-count.) Assert.Equal(3, canTraceCounter); }
public void GetElapsedMilliseconds_NoStart() { StopwatchTimer timer = StopwatchTimer.Create(); Assert.Equal(0, timer.GetElapsedMilliseconds()); Thread.Sleep(10); Assert.Equal(0, timer.GetElapsedMilliseconds()); }
public void GetElapsedMilliseconds() { StopwatchTimer timer = StopwatchTimer.Create(); timer.Start(); long first = timer.GetElapsedMilliseconds(); Thread.Sleep(10); long second = timer.GetElapsedMilliseconds(); Thread.Sleep(20); long third = timer.GetElapsedMilliseconds(); Assert.True(second > first); Assert.True(third > second); }