コード例 #1
0
        public void CreatesTimer()
        {
            using TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(30), 10);
            TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(90));

            Assert.IsNotNull(timer);
        }
コード例 #2
0
        public async Task MultipleTimeouts()
        {
            const int                  timerTimeout = 100;
            const int                  buckets      = 20;
            const int                  resolution   = 50;
            TimerWheelCore             wheel        = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), buckets); // 20 buckets of 50 ms go up to 1000ms
            List <Task <(int, long)> > tasks        = new List <Task <(int, long)> >();

            for (int i = 0; i < 10; i++)
            {
                int             estimatedTimeout = (i + 1) * timerTimeout;
                TimerWheelTimer timer            = wheel.CreateTimer(TimeSpan.FromMilliseconds(estimatedTimeout));
                tasks.Add(Task.Run(async() => {
                    Stopwatch stopwatch = Stopwatch.StartNew();
                    await timer.StartTimerAsync();
                    stopwatch.Stop();
                    return(estimatedTimeout, stopwatch.ElapsedMilliseconds);
                }));
            }

            await Task.WhenAll(tasks);

            foreach (Task <(int, long)> task in tasks)
            {
                Assert.IsTrue(task.Result.Item2 >= task.Result.Item1 - resolution && task.Result.Item2 <= task.Result.Item1 + resolution, $"Timer configured with {task.Result.Item1} took {task.Result.Item2} to fire.");
            }
        }
コード例 #3
0
 private void StartCongestionControlTimer()
 {
     this.congestionControlTimer = this.timerWheel.CreateTimer(BatchAsyncStreamer.congestionControllerDelay);
     this.congestionControlTask  = this.congestionControlTimer.StartTimerAsync().ContinueWith(async(task) =>
     {
         await this.RunCongestionControlAsync();
     }, this.cancellationTokenSource.Token);
 }
コード例 #4
0
        public void DisposedCannotStartTimers()
        {
            TimerWheelCore  wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(30), 3);
            TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(90));

            Assert.IsNotNull(timer);
            wheel.Dispose();
            Assert.ThrowsException <ObjectDisposedException>(() => timer.StartTimerAsync());
        }
コード例 #5
0
        public void DisposeCancelsTimers()
        {
            TimerWheelCore  wheel     = new TimerWheelCore(TimeSpan.FromMilliseconds(30), 3);
            TimerWheelTimer timer     = wheel.CreateTimer(TimeSpan.FromMilliseconds(90));
            Task            timerTask = timer.StartTimerAsync();

            wheel.Dispose();
            Assert.AreEqual(TaskStatus.Canceled, timerTask.Status);
        }
コード例 #6
0
        public override void SubscribeForTimeouts(TimerWheelTimer timer)
        {
            this.ThrowIfDisposed();
            long timerTimeoutInTicks = timer.Timeout.Ticks;
            int  bucket = (int)timerTimeoutInTicks / this.resolutionInTicks;
            int  index  = this.GetIndexForTimeout(bucket);
            ConcurrentQueue <TimerWheelTimer> timerQueue = this.timers[index];

            timerQueue.Enqueue(timer);
        }
コード例 #7
0
 private void ResetTimer()
 {
     this.currentTimer = this.timerWheel.CreateTimer(BatchAsyncStreamer.batchTimeout);
     this.timerTask    = this.currentTimer.StartTimerAsync().ContinueWith((task) =>
     {
         if (task.IsCompleted)
         {
             this.DispatchTimer();
         }
     }, this.cancellationTokenSource.Token);
 }
コード例 #8
0
        public async Task TimeoutFires()
        {
            const int timerTimeout = 2000;
            const int resolution   = 500;

            using TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), 10); // 10 buckets of 500 ms go up to 5000ms
            TimerWheelTimer timer     = wheel.CreateTimer(TimeSpan.FromMilliseconds(timerTimeout));
            Stopwatch       stopwatch = Stopwatch.StartNew();
            await timer.StartTimerAsync();

            stopwatch.Stop();
        }
コード例 #9
0
        public async Task TimeoutFires()
        {
            const int       timerTimeout = 200;
            const int       resolution   = 50;
            TimerWheelCore  wheel        = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), 10); // 10 buckets of 50 ms go up to 500ms
            TimerWheelTimer timer        = wheel.CreateTimer(TimeSpan.FromMilliseconds(timerTimeout));
            Stopwatch       stopwatch    = Stopwatch.StartNew();
            await timer.StartTimerAsync();

            stopwatch.Stop();
            Assert.IsTrue(stopwatch.ElapsedMilliseconds >= timerTimeout - resolution && stopwatch.ElapsedMilliseconds <= timerTimeout + resolution);
        }
コード例 #10
0
        public async Task TimeoutFires_SameTimeout()
        {
            const int       timerTimeout = 200;
            const int       resolution   = 50;
            TimerWheelCore  wheel        = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), 10); // 10 buckets of 50 ms go up to 500ms
            TimerWheelTimer timer        = wheel.CreateTimer(TimeSpan.FromMilliseconds(timerTimeout));
            TimerWheelTimer timer2       = wheel.CreateTimer(TimeSpan.FromMilliseconds(timerTimeout));
            Stopwatch       stopwatch    = Stopwatch.StartNew();
            await Task.WhenAll(timer.StartTimerAsync(), timer2.StartTimerAsync());

            stopwatch.Stop();
            Assert.IsTrue(stopwatch.ElapsedMilliseconds >= timerTimeout - resolution, $"{stopwatch.ElapsedMilliseconds} >= {timerTimeout - resolution}, timerTimeout: {timerTimeout}, resolution: {resolution}");
            Assert.IsTrue(stopwatch.ElapsedMilliseconds <= timerTimeout + resolution, $"{stopwatch.ElapsedMilliseconds} <= {timerTimeout + resolution}, timerTimeout: {timerTimeout}, resolution: {resolution}");
        }
コード例 #11
0
        public async Task TenK_WithTimerWheel()
        {
            TimerWheel  wheel  = TimerWheel.CreateTimerWheel(TimeSpan.FromMilliseconds(1000), 10);
            List <Task> timers = new List <Task>(this.timeouts.Count);

            for (int i = 0; i < this.timeouts.Count; i++)
            {
                TimerWheelTimer timer = wheel.CreateTimer(TimeSpan.FromMilliseconds(this.timeouts[i]));
                timers.Add(timer.StartTimerAsync());
            }

            await Task.WhenAll(timers);

            wheel.Dispose();
        }
コード例 #12
0
        public void Dispose()
        {
            this.cancellationTokenSource.Cancel();
            this.cancellationTokenSource.Dispose();

            this.currentTimer.CancelTimer();
            this.currentTimer = null;
            this.timerTask    = null;

            if (this.congestionControlTimer != null)
            {
                this.congestionControlTimer.CancelTimer();
                this.congestionControlTimer = null;
                this.congestionControlTask  = null;
            }
        }
コード例 #13
0
        public override void SubscribeForTimeouts(TimerWheelTimer timer)
        {
            this.ThrowIfDisposed();
            long timerTimeoutInTicks = timer.Timeout.Ticks;
            int  bucket = (int)timerTimeoutInTicks / this.resolutionInTicks;

            lock (this.subscriptionLock)
            {
                int index = this.GetIndexForTimeout(bucket);
                ConcurrentQueue <TimerWheelTimer> timerQueue = this.timers.GetOrAdd(index,
                                                                                    _ =>
                {
                    return(new ConcurrentQueue <TimerWheelTimer>());
                });
                timerQueue.Enqueue(timer);
            }
        }
コード例 #14
0
        public void IndexMovesAsTimerPasses()
        {
            using TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(30), 3, timer: null); // deactivate timer to fire manually
            TimerWheelTimer timer     = wheel.CreateTimer(TimeSpan.FromMilliseconds(90));
            Task            timerTask = timer.StartTimerAsync();

            wheel.OnTimer(null);
            Assert.AreEqual(TaskStatus.WaitingForActivation, timerTask.Status);
            wheel.OnTimer(null);
            Assert.AreEqual(TaskStatus.WaitingForActivation, timerTask.Status);
            TimerWheelTimer secondTimer     = wheel.CreateTimer(TimeSpan.FromMilliseconds(60));
            Task            secondTimerTask = secondTimer.StartTimerAsync();

            wheel.OnTimer(null);
            Assert.AreEqual(TaskStatus.RanToCompletion, timerTask.Status);
            wheel.OnTimer(null);
            Assert.AreEqual(TaskStatus.RanToCompletion, secondTimerTask.Status);
        }
コード例 #15
0
        public async Task MultipleTimeouts()
        {
            const int timerTimeout = 1000;
            const int buckets      = 20;
            const int resolution   = 500;

            using TimerWheelCore wheel = new TimerWheelCore(TimeSpan.FromMilliseconds(resolution), buckets); // 20 buckets of 500 ms go up to 10000ms
            List <Task <(int, long)> > tasks = new List <Task <(int, long)> >();

            for (int i = 0; i < 10; i++)
            {
                int             estimatedTimeout = (i + 1) * timerTimeout;
                TimerWheelTimer timer            = wheel.CreateTimer(TimeSpan.FromMilliseconds(estimatedTimeout));
                tasks.Add(Task.Run(async() => {
                    Stopwatch stopwatch = Stopwatch.StartNew();
                    await timer.StartTimerAsync();
                    stopwatch.Stop();
                    return(estimatedTimeout, stopwatch.ElapsedMilliseconds);
                }));
            }

            await Task.WhenAll(tasks);
        }
コード例 #16
0
 public abstract void SubscribeForTimeouts(TimerWheelTimer timer);
コード例 #17
0
 public async Task One_WithTimerWheel()
 {
     TimerWheelTimer timer = this.mainWheel.CreateTimer(TimeSpan.FromMilliseconds(1000));
     await timer.StartTimerAsync();
 }
コード例 #18
0
 private void ResetTimer()
 {
     this.currentTimer = this.timerWheel.CreateTimer(BatchAsyncStreamer.batchTimeout);
     this.timerTask    = this.GetTimerTaskAsync();
 }
コード例 #19
0
 public async Task One_WithTimerWheel()
 {
     TimerWheelTimer timer = this.mainWheel.CreateTimer(TimerWheelBenchmark.resolution);
     await timer.StartTimerAsync();
 }