Example #1
0
        static void Main(string[] args)
        {
            //Metric.CompletelyDisableMetrics();

            Metric.Config
            .WithHttpEndpoint("http://*****:*****@"c:\temp\csv")), TimeSpan.FromSeconds(10))
                           .WithConsoleReport(TimeSpan.FromSeconds(30))
                           //.WithCSVReports(@"c:\temp\reports\", TimeSpan.FromSeconds(10))
                           //.WithTextFileReport(@"C:\temp\reports\metrics.txt", TimeSpan.FromSeconds(10))
                           );

            using (var scheduler = new ActionScheduler())
            {
                SampleMetrics.RunSomeRequests();
                scheduler.Start(TimeSpan.FromMilliseconds(500), () => SampleMetrics.RunSomeRequests());

                //Metrics.Samples.FSharp.SampleMetrics.RunSomeRequests();

                HealthChecksSample.RegisterHealthChecks();
                //Metrics.Samples.FSharp.HealthChecksSample.RegisterHealthChecks();

                Console.WriteLine("done setting things up");
                Console.ReadKey();
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            var tenSeconds = TimeSpan.FromSeconds(10);
            var scheduler  = new ActionScheduler(tenSeconds);

            scheduler.Schedule(
                TimeSpan.FromSeconds(1),
                () => WriteLine("First second"));

            scheduler.Schedule(
                TimeSpan.FromSeconds(5),
                () => WriteLine("Fifth second"));

            // this task run with delay but will be executed in correct time in the next iteration
            scheduler.Schedule(
                TimeSpan.FromSeconds(5),
                async() =>
            {
                WriteLine("Fifth second with delay");
                await Task.Delay(TimeSpan.FromSeconds(1));
            });

            scheduler.Start();
            Console.ReadKey();
            scheduler.Stop();
        }
        public void ActionScheduler_ToleratesUnlimitedFailures()
        {
            using (var scheduler = new ActionScheduler(-1))
            {
                var actionCounter = 0;
                var tcs           = new TaskCompletionSource <bool>();

                scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
                {
                    if (actionCounter < 10)
                    {
                        actionCounter++;
                        throw new Exception();
                    }
                    else
                    {
                        actionCounter++;
                        tcs.SetResult(true);
                    }
                });

                tcs.Task.Wait();
                scheduler.Stop();

                actionCounter.Should().Be(11);
            }
        }
Example #4
0
        static void Main(string[] args)
        {
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings {
                Formatting = Formatting.Indented
            };

            using (ActionScheduler scheduler = new ActionScheduler())
                using (var host = new NancyHost(new Uri("http://localhost:8090")))
                {
                    host.Start();
                    Console.WriteLine("Nancy Running at http://localhost:8090");
                    Console.WriteLine("Press any key to exit");

                    Process.Start("http://localhost:8090/metrics/");

                    SampleMetrics.RunSomeRequests();

                    scheduler.Start(TimeSpan.FromMilliseconds(500), () =>
                    {
                        SetCounterSample.RunSomeRequests();
                        SetMeterSample.RunSomeRequests();
                        UserValueHistogramSample.RunSomeRequests();
                        UserValueTimerSample.RunSomeRequests();
                        SampleMetrics.RunSomeRequests();
                    });

                    HealthChecksSample.RegisterHealthChecks();

                    Console.ReadKey();
                }
        }
Example #5
0
        static void Main(string[] args)
        {
            const string url = "http://localhost:1235/";

            using (var scheduler = new ActionScheduler())
            {
                using (WebApp.Start <Startup>(url))
                {
                    Console.WriteLine("Owin Running at {0}", url);
                    Console.WriteLine("Press any key to exit");
                    Process.Start($"{url}metrics/");

                    SampleMetrics.RunSomeRequests();

                    scheduler.Start(TimeSpan.FromMilliseconds(500), () =>
                    {
                        SetCounterSample.RunSomeRequests();
                        SetMeterSample.RunSomeRequests();
                        UserValueHistogramSample.RunSomeRequests();
                        UserValueTimerSample.RunSomeRequests();
                        SampleMetrics.RunSomeRequests();
                    });

                    HealthChecksSample.RegisterHealthChecks();

                    Console.ReadKey();
                }
            }
        }
Example #6
0
        static void Main(string[] args)
        {
            //Metric.CompletelyDisableMetrics();
            Metric.Config
            .WithHttpEndpoint("http://*****:*****@"c:\temp\reports\", TimeSpan.FromSeconds(10))
                           //.WithTextFileReport(@"C:\temp\reports\metrics.txt", TimeSpan.FromSeconds(10))
                           //.WithGraphite(new Uri("net.udp://localhost:2003"), TimeSpan.FromSeconds(1))
                           //.WithInfluxDb("192.168.1.8", 8086, "admin", "admin", "metrics", TimeSpan.FromSeconds(1))
                           .WithElasticSearch(new ElasticSearch.ElasticReportsConfig()
            {
                Host = "192.168.1.8", Port = 9200, Index = "metrics", RollingIndexType = ElasticSearch.RollingIndexType.Daily, ReportingApplication = ApplicationInfo.Default
            }, TimeSpan.FromSeconds(5))
                           );

            using (var scheduler = new ActionScheduler())
            {
                SampleMetrics.RunSomeRequests();

                scheduler.Start(TimeSpan.FromMilliseconds(500), () =>
                {
                    SetCounterSample.RunSomeRequests();
                    SetMeterSample.RunSomeRequests();
                    UserValueHistogramSample.RunSomeRequests();
                    UserValueTimerSample.RunSomeRequests();
                    SampleMetrics.RunSomeRequests();
                });

                Metric.Gauge("Errors", () => 1, Unit.None);
                Metric.Gauge("% Percent/Gauge|test", () => 1, Unit.None);
                Metric.Gauge("& AmpGauge", () => 1, Unit.None);
                Metric.Gauge("()[]{} ParantesisGauge", () => 1, Unit.None);
                Metric.Gauge("Gauge With No Value", () => double.NaN, Unit.None);

                //Metric.Gauge("Gauge Resulting in division by zero", () => 5 / 0.0, Unit.None);

                ////Metrics.Samples.FSharp.SampleMetrics.RunSomeRequests();

                HealthChecksSample.RegisterHealthChecks();
                //Metrics.Samples.FSharp.HealthChecksSample.RegisterHealthChecks();

                Console.WriteLine("done setting things up");
                Console.ReadKey();
            }
        }
        private static void Test_NoTolerance(ActionScheduler scheduler)
        {
            var actionCounter = 0;

            scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
            {
                actionCounter++;
                throw new Exception();
            });

            Thread.Sleep(200);
            scheduler.Stop();

            actionCounter.Should().Be(1);
        }
Example #8
0
        public void Scheduled_Tasks_Runs_Exactly_In_Time()
        {
            var task1 = GetFakeTimeInvoker();
            var task2 = GetFakeTimeInvoker();

            var sampleInSeconds = 5;
            var firstDelay      = 2;
            var secondDelay     = 3;

            var            sampling = TimeSpan.FromSeconds(sampleInSeconds);
            DateTimeOffset startTime;
            var            testScheduler = new TestScheduler();

            using (var scheduler = new ActionScheduler(sampling, testScheduler))
            {
                scheduler.Schedule(TimeSpan.FromSeconds(firstDelay), () => task1.Object.Invoke(testScheduler.Now));
                scheduler.Schedule(TimeSpan.FromSeconds(secondDelay), () => task2.Object.Invoke(testScheduler.Now));

                startTime = testScheduler.Now;
                scheduler.Start();

                testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks);
            }

            var correction = startTime.AddSeconds(-(startTime.Second % sampleInSeconds));

            var firstTime = correction.AddSeconds(firstDelay);

            if (startTime > firstTime)
            {
                firstTime = firstTime.Add(sampling);
            }

            var secondTime = correction.AddSeconds(secondDelay);

            if (startTime > secondTime)
            {
                secondTime = secondTime.Add(sampling);
            }

            task1.Verify(x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, firstTime))), Times.Exactly(1));
            task1.Verify(x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, firstTime.Add(sampling)))), Times.Exactly(1));

            task2.Verify(x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, secondTime))), Times.Exactly(1));
            task2.Verify(
                x => x.Invoke(It.Is <DateTimeOffset>(date => DateEqual(date, secondTime.Add(sampling)))),
                Times.Exactly(1));
        }
Example #9
0
        public void Can_Schedule_Task(TimeSpan startTime, bool inclusive, TimeSpan sampling, TimeSpan delay, TimeSpan workDuration, int occurs)
        {
            var invoker       = GetFakeInvoker();
            var testScheduler = new TestScheduler();

            using (var scheduler = new ActionScheduler(sampling, testScheduler))
            {
                scheduler.Schedule(delay, () => invoker.Object.Invoke(), inclusive);

                testScheduler.AdvanceBy(startTime.Ticks);
                scheduler.Start();

                testScheduler.AdvanceBy(workDuration.Ticks - startTime.Ticks);
            }

            invoker.Verify(x => x.Invoke(), Times.Exactly(occurs));
        }
        public void ActionScheduler_ExecutesScheduledActionWithToken()
        {
            using (ActionScheduler scheduler = new ActionScheduler())
            {
                int data = 0;
                var tcs  = new TaskCompletionSource <bool>();

                scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
                {
                    data++;
                    tcs.SetResult(true);
                });

                tcs.Task.Wait();
                scheduler.Stop();
                data.Should().Be(1);
            }
        }
Example #11
0
        public void Can_Schedule_Multiple_Tasks()
        {
            var task1         = GetFakeInvoker();
            var task2         = GetFakeInvoker();
            var testScheduler = new TestScheduler();

            using (var scheduler = new ActionScheduler(TimeSpan.FromSeconds(5), testScheduler))
            {
                scheduler.Schedule(TimeSpan.FromSeconds(1), () => task1.Object.Invoke());
                scheduler.Schedule(TimeSpan.FromSeconds(2), () => task2.Object.Invoke());

                scheduler.Start();

                testScheduler.AdvanceBy(TimeSpan.FromSeconds(10).Ticks);
            }

            task1.Verify(x => x.Invoke(), Times.Exactly(2));
            task2.Verify(x => x.Invoke(), Times.Exactly(2));
        }
Example #12
0
        static void Main(string[] args)
        {
            const string url = "http://localhost:1234/";

            using (var scheduler = new ActionScheduler())
            {
                using (WebApp.Start <Startup>(url))
                {
                    Console.WriteLine("Owin Running at {0}", url);
                    Console.WriteLine("Press any key to exit");
                    Process.Start(string.Format("{0}metrics", url));

                    SampleMetrics.RunSomeRequests();

                    scheduler.Start(TimeSpan.FromMilliseconds(500), () => SampleMetrics.RunSomeRequests());

                    Console.ReadKey();
                }
            }
        }
        public void ActionScheduler_ExecutesScheduledFunction()
        {
            using (ActionScheduler scheduler = new ActionScheduler())
            {
                var tcs  = new TaskCompletionSource <bool>();
                int data = 0;

                Func <CancellationToken, Task> function = (t) => Task.Factory.StartNew(() =>
                {
                    data++;
                    tcs.SetResult(true);
                });

                scheduler.Start(TimeSpan.FromMilliseconds(10), function);
                tcs.Task.Wait();
                scheduler.Stop();

                data.Should().Be(1);
            }
        }
Example #14
0
        static void Main(string[] args)
        {
            //Metric.CompletelyDisableMetrics();
            Metric.Config
            .WithHttpEndpoint("http://localhost:1234/metrics/", config => config
                              .WithEndpointReport("/test", (d, h, c) => new MetricsEndpointResponse("test", "text/plain")))
            .WithAllCounters()
            .WithInternalMetrics()
            .WithReporting(config => config
                           .WithConsoleReport(TimeSpan.FromSeconds(30)));

            using (var scheduler = new ActionScheduler())
            {
                SampleMetrics.RunSomeRequests();

                scheduler.Start(TimeSpan.FromMilliseconds(500), () =>
                {
                    SetCounterSample.RunSomeRequests();
                    SetMeterSample.RunSomeRequests();
                    UserValueHistogramSample.RunSomeRequests();
                    UserValueTimerSample.RunSomeRequests();
                    SampleMetrics.RunSomeRequests();
                });

                Metric.Gauge("Errors", () => 1, Unit.None);
                Metric.Gauge("% Percent/Gauge|test", () => 1, Unit.None);
                Metric.Gauge("& AmpGauge", () => 1, Unit.None);
                Metric.Gauge("()[]{} ParantesisGauge", () => 1, Unit.None);
                Metric.Gauge("Gauge With No Value", () => double.NaN, Unit.None);

                //Metric.Gauge("Gauge Resulting in division by zero", () => 5 / 0.0, Unit.None);

                ////Metrics.Samples.FSharp.SampleMetrics.RunSomeRequests();

                HealthChecksSample.RegisterHealthChecks();
                //Metrics.Samples.FSharp.HealthChecksSample.RegisterHealthChecks();

                Console.WriteLine("done setting things up");
                Console.ReadKey();
            }
        }
        public void ActionScheduler_ReportsErrorInCaseOfToleratedFailures()
        {
            using (var scheduler = new ActionScheduler(3))
            {
                var actionCounter = 0;
                var tcs           = new TaskCompletionSource <bool>();
                var errorCounter  = 0;

                Metric.Config.WithErrorHandler(e =>
                {
                    if (e.Message == "reports_error")
                    {
                        errorCounter++;
                    }
                });

                scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
                {
                    if (actionCounter < 3)
                    {
                        actionCounter++;
                        throw new Exception("reports_error");
                    }
                    else
                    {
                        actionCounter++;
                        tcs.SetResult(true);
                    }
                });

                tcs.Task.Wait();
                scheduler.Stop();

                actionCounter.Should().Be(4);
                errorCounter.Should().Be(3);
            }
        }
        public void ActionScheduler_ReportsExceptionWithGlobalMetricHandler()
        {
            Exception x   = null;
            var       tcs = new TaskCompletionSource <bool>();

            Metric.Config.WithErrorHandler(e =>
            {
                x = e;
                tcs.SetResult(true);
            });

            using (ActionScheduler scheduler = new ActionScheduler())
            {
                scheduler.Start(TimeSpan.FromMilliseconds(10), t =>
                {
                    throw new InvalidOperationException("boom");
                });

                tcs.Task.Wait(1000);
                scheduler.Stop();
            }

            x.Should().NotBeNull();
        }