Example #1
0
        public void Basic()
        {
            var tps = new PerSecondCounter();

            while (DateTime.Now.Millisecond > 100)
            {
                Thread.Sleep(1);
            }                                                            // just wait to get more of a second

            tps.Increment();
            tps.Increment(2);

            Assert.Equal(0, tps.Value);
            Assert.Equal(0.0, tps.ValuePerSecond);

            Thread.Sleep(1000);

            Assert.Equal(3, tps.Value);
            Assert.Equal(3.0, tps.ValuePerSecond);

            Thread.Sleep(1000);

            Assert.Equal(0, tps.Value);
            Assert.Equal(0.0, tps.ValuePerSecond);
        }
Example #2
0
        public static void Run()
        {
            Terminal.Clear();
            Terminal.WriteLine(" Terminal ", ConsoleColor.Yellow, ConsoleColor.DarkGray);
            Terminal.WriteLine();

            using var counter = new PerSecondCounter();
            counter.Tick      = delegate {
                Console.WriteLine($"TPS: {counter.ValuePerSecond} /s");
            };

            var limiter = new PerSecondLimiter(13);

            while (true)
            {
                foreach (var key in Terminal.ReadAvailableKeys())
                {
                    switch (key)
                    {
                    case ConsoleKey.OemPlus: limiter.PerSecondRate += 1; break;

                    case ConsoleKey.OemMinus:
                        if (limiter.PerSecondRate > 1)
                        {
                            limiter.PerSecondRate -= 1;
                        }
                        break;

                    case ConsoleKey.Escape: return;
                    }
                }

                limiter.Wait();
                counter.Increment();
            }
        }