Beispiel #1
0
        public void Ages()
        {
            UT_INIT();

            Log.SetVerbosity(new ConsoleLogger(), Verbosity.Verbose, "/");
            Log.MapThreadName("UnitTest");
            Log.SetDomain("TickWatch", Scope.Method);

            TickWatch tt = new TickWatch();
            // minimum time measuring
            {
                tt.Start();
                tt.Sample();
                tt.Reset(); // we need to do this once before, otherwise C# might be
                // very slow. Obviously the optimizer...
                tt.Start();
                tt.Sample();

                long ttAverageInNanos  = tt.GetAverage().InNanos();
                long ttAverageInMicros = tt.GetAverage().InMicros();
                long ttAverageInMillis = tt.GetAverage().InMillis();

                Log.Info("TickWatch minimum measurement nanos:    " + ttAverageInNanos);
                Log.Info("TickWatch minimum measurement micros:   " + ttAverageInMicros);
                Log.Info("TickWatch minimum measurement millis:   " + ttAverageInMillis);
                UT_TRUE(ttAverageInNanos < 5000);
                UT_TRUE(ttAverageInMicros <= 5);
                UT_TRUE(ttAverageInMillis == 0);
            }

            // minimum sleep time measuring
            {
                tt.Reset();
                for (int i = 0; i < 100; i++)
                {
                    ALIB.SleepNanos(1);
                    tt.Sample();
                }
                Ticks avg = tt.GetAverage();
                Log.Info("100 probes of 1 ns of sleep leads to average sleep time of " + avg.InNanos() + " ns");
            }


            // sleep two times 20 ms and probe it to an average
            {
                tt.Reset();
                tt.Start();
                ALIB.SleepMillis(20);
                tt.Sample();


                ALIB.SleepMillis(80);

                tt.Start();
                ALIB.SleepMillis(20);
                tt.Sample();

                long   cnt   = tt.GetSampleCnt();
                long   avg   = tt.GetAverage().InMillis();
                double hertz = tt.GetAverage().InHertz(1);
                Log.Info("TickWatch sum is " + tt.GetCumulated().InMillis() + " after " + cnt + " times 20 ms sleep");
                Log.Info("  average is: " + avg + " ms");
                Log.Info("  in Hertz:   " + hertz);
                UT_TRUE(hertz < 53);
                UT_TRUE(hertz > 30);   // should work even on heavily loaded machines
                UT_EQ(2, cnt);
                UT_TRUE(avg >= 18);
                UT_TRUE(avg < 45);   // should work even on heavily loaded machines
            }

            // Ticks Since
            {
                Ticks tt1 = new Ticks(); tt1.FromSeconds(1000);
                Ticks tt2 = new Ticks(); tt2.FromSeconds(1001);

                UT_TRUE(tt2.Since(tt1).InMillis() == 1000L);
                UT_TRUE(tt2.Since(tt1).InMicros() == 1000L * 1000L);
                UT_TRUE(tt2.Since(tt1).InNanos() == 1000L * 1000L * 1000L);
            }
        }