Exemple #1
0
        public void TestParse()
        {
            ValueStopwatch stopwatch = ValueStopwatch.StartNew();

            for (int i = 0; i < 100000; i++)
            {
                BackendMetricsParser.TryParse(delimitedString, out BackendMetrics backendMetrics);
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds);
        }
        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));
            ValueStopwatch  stopwatch = ValueStopwatch.StartNew();
            await timer.StartTimerAsync();

            stopwatch.Stop();
        }
 private void Execute()
 {
     try
     {
         lock (_lockObj)
         {
             _scheduled = false;
             _queueDelay.Stop();
             _lastQueueDelay = _queueDelay.Elapsed;
         }
     }
     catch (Exception exception)
     {
         _log.LogError(exception, "Exception monitoring .NET thread pool delay");
     }
 }
Exemple #4
0
        public void Dispose()
        {
            var elapsedMs = watch.Stop();

            session.Measured(key, elapsedMs);

            if (hooks != null)
            {
                for (var i = 0; i < hooks.Count; i++)
                {
                    try
                    {
                        hooks[i].Dispose();
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
        }
Exemple #5
0
        public async Task ValidateAsyncCacheNonBlocking()
        {
            AsyncCacheNonBlocking <string, string> asyncCache = new AsyncCacheNonBlocking <string, string>();
            string result = await asyncCache.GetAsync(
                "test",
                (_) => Task.FromResult("test2"),
                (_) => false);

            string cachedResults = await asyncCache.GetAsync(
                "test",
                (_) => throw new Exception("should not refresh"),
                (_) => false);

            string        oldValue   = null;
            Task <string> updateTask = asyncCache.GetAsync(
                key: "test",
                singleValueInitFunc: async(staleValue) =>
            {
                oldValue = staleValue;
                await Task.Delay(TimeSpan.FromSeconds(1));
                return("Test3");
            },
                forceRefresh: (_) => true);

            ValueStopwatch concurrentOperationStopwatch = ValueStopwatch.StartNew();
            string         concurrentUpdateTask         = await asyncCache.GetAsync(
                "test",
                (_) => throw new Exception("should not refresh"),
                (_) => false);

            Assert.AreEqual("test2", result);
            concurrentOperationStopwatch.Stop();

            Assert.IsTrue(concurrentOperationStopwatch.Elapsed.TotalMilliseconds < 500);

            result = await updateTask;
            Assert.AreEqual("Test3", result);
            Assert.AreEqual(oldValue, "test2", "The call back was not done.");
        }
        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() => {
                    ValueStopwatch stopwatch = ValueStopwatch.StartNew();
                    await timer.StartTimerAsync();
                    stopwatch.Stop();
                    return(estimatedTimeout, stopwatch.ElapsedMilliseconds);
                }));
            }

            await Task.WhenAll(tasks);
        }