예제 #1
0
        public void Log_Threads()
        {
            UT_INIT();

            Log.AddDebugLogger();

            Log.SetDomain("TEST/THREAD1", Scope.Method);
            Log.SetVerbosity(Log.DebugLogger, Verbosity.Verbose, "TEST/THREAD1");

            // if this gets commented out, the test might crash. At least the console will
            // become scrambled!
            //Log.LOX.Lock.setUnsafe( true );

            Thread thread = new Thread(new ThreadStart(testDifferentThreadMethod))
            {
                Name = "Thread2"
            };

            thread.Start();

            for (int i = 0; i < 50; i++)
            {
                Log.Info("This is the main thread ");// + i );
                ALIB.SleepMillis(4);
            }
        }
예제 #2
0
        public void testDifferentThreadMethod()
        {
            Log.SetDomain("TEST/THREAD2", Scope.Method);
            Log.SetVerbosity(Log.DebugLogger, Verbosity.Verbose, "TEST/THREAD2");

            for (int i = 0; i < 20; i++)
            {
                Log.Info("This is a different Thread. Cnt= " + i);
                ALIB.SleepMillis(3);
            }
        }
예제 #3
0
        public void ThreadLock_Threaded()
        {
            UT_INIT();

            Log.SetVerbosity(new ConsoleLogger(), Verbosity.Verbose, "/");
            Log.MapThreadName("UnitTest");
            Log.SetDomain("TestTLock", Scope.Filename);
            Log.SetVerbosity("CONSOLE", Verbosity.Verbose, "ALIB");

            ThreadLock aLock = new ThreadLock();

            Test_ThreadLock_SharedInt shared = new Test_ThreadLock_SharedInt();

            Log.Info("starting thread locked");
            aLock.Acquire();
            Test_ThreadLock_TestThreadParams tParam = new Test_ThreadLock_TestThreadParams(aLock, 10, 1, true, shared);

            Thread thread = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));

            thread.Name = "A Thread";
            thread.Start(tParam);

            Log.Info("We wait 1100 ms. This should give a warning! ");
            ALIB.SleepMillis(1100);
            aLock.Release();

            // wait until t ended
            while (thread.IsAlive)
            {
                ALIB.SleepMillis(1);
            }

            // now we do the same with a higher wait limit, no erro should come
            aLock.waitWarningTimeLimitInMillis = 5;
            aLock.Acquire();
            tParam = new Test_ThreadLock_TestThreadParams(aLock, 10, 1, true, shared);
            thread = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));
            thread.Start(tParam);
            Log.Info("We wait 1 ms. This should NOT give a warning! ");
            ALIB.SleepMillis(1);
            aLock.Release();

            // wait until t ended
            while (thread.IsAlive)
            {
                ALIB.SleepMillis(1);
            }
        }
예제 #4
0
        public void ThreadLock_HeavyLoad()
        {
            UT_INIT();
            Log.SetVerbosity(new ConsoleLogger(), Verbosity.Verbose, "/");
            Log.MapThreadName("UnitTest");
            Log.SetDomain("TestTLock", Scope.Filename);

            ThreadLock aLock = new ThreadLock();

            // uncomment this for unsafe mode
            // lock.setUnsafe( true );

            Test_ThreadLock_SharedInt shared = new Test_ThreadLock_SharedInt();
            int  holdTime = 0;
            int  repeats  = 5000;
            bool verbose  = false;

            Test_ThreadLock_TestThreadParams p1 = new Test_ThreadLock_TestThreadParams(aLock, holdTime, repeats, verbose, shared);
            Test_ThreadLock_TestThreadParams p2 = new Test_ThreadLock_TestThreadParams(aLock, holdTime, repeats, verbose, shared);
            Test_ThreadLock_TestThreadParams p3 = new Test_ThreadLock_TestThreadParams(aLock, holdTime, repeats, verbose, shared);

            Log.Info("starting three threads");

            Thread t1 = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));
            Thread t2 = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));
            Thread t3 = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));

            t1.Start(p1);
            t2.Start(p2);
            t3.Start(p3);

            // wait until all ended
            while (t1.IsAlive || t2.IsAlive || t3.IsAlive)
            {
                ALIB.SleepMillis(1);
            }

            Log.Info("All threads ended. Shared value=" + shared.val);

            UT_TRUE(shared.val == 0);
        }
예제 #5
0
        public void Test_ThreadLock_Test_run(Object o)
        {
            Test_ThreadLock_TestThreadParams p = o as Test_ThreadLock_TestThreadParams;

            for (int i = 0; i < p.repeats; i++)
            {
                if (p.verbose)
                {
                    Log.Info("Thread: " + Thread.CurrentThread.Name + " acuiring lock...");
                }
                p.aLock.Acquire();
                if (p.verbose)
                {
                    Log.Info("Thread: " + Thread.CurrentThread.Name + " has lock.");
                }

                int sVal = ++p.shared.val;

                ALIB.SleepMillis(p.holdTime);

                p.shared.val = sVal - 1;

                if (p.verbose)
                {
                    Log.Info("Thread: " + Thread.CurrentThread.Name + " releasing lock.");
                }
                p.aLock.Release();
                if (p.verbose)
                {
                    Log.Info("Thread: " + Thread.CurrentThread.Name + " released lock.");
                }
            }

            p.result = 0;
            Log.Info("Thread: " + Thread.CurrentThread.Name + " terminates.");
        }
        public void Lox_ScopeDomains()
        {
            UT_INIT();

            // we have tell alox to include more directories in the scope path
            Log.ClearSourcePathTrimRules(Inclusion.Include, false);
            Log.SetSourcePathTrimRule("*/alox/src.cs/", Inclusion.Exclude);

            Lox          lox           = new Lox("ReleaseLox");
            TextLogger   consoleLogger = Lox.CreateConsoleLogger();
            MemoryLogger ml            = new MemoryLogger();

            ml.MetaInfo.Format._()._("@%D#");
            lox.SetVerbosity(ml, Verbosity.Verbose);

            lox.SetVerbosity(consoleLogger, Verbosity.Verbose);
            lox.SetVerbosity(consoleLogger, Verbosity.Verbose, ALox.InternalDomains);

            // scope global
            lox.SetDomain("REPLACE", Scope.Global);  lox.Info("", ""); SDCHECK_RL("@/REPLACE#", ml);
            lox.SetDomain("GLOBAL", Scope.Global);  lox.Info("", ""); SDCHECK_RL("@/GLOBAL#", ml);

            lox.SetDomain(null, Scope.Global);  lox.Info("", ""); SDCHECK_RL("@/#", ml);

            // scope source
            lox.SetDomain("REPLACE", Scope.Filename);  lox.Info("", ""); CICHECK_RL("@/REPLACE#", ml);
            lox.SetDomain("FILE", Scope.Filename);  lox.Info("", ""); CICHECK_RL("@/FILE#", ml);

            // scope method
            lox.SetDomain("REPLACE", Scope.Method);  lox.Info("", ""); CICHECK_RL("@/FILE/REPLACE#", ml);
            lox.SetDomain("Method", Scope.Method);  lox.Info("", ""); CICHECK_RL("@/FILE/METHOD#", ml);
            lox.SetDomain("/ABS", Scope.Method);  lox.Info("", ""); CICHECK_RL("@/ABS#", ml);

            // unset method with null
            lox.SetDomain(null, Scope.Method);  lox.Info("", ""); CICHECK_RL("@/FILE#", ml);

            // unset method with ""
            lox.SetDomain("/METHOD", Scope.Method);  lox.Info("", ""); CICHECK_RL("@/METHOD#", ml);
            lox.SetDomain("", Scope.Method);  lox.Info("", ""); CICHECK_RL("@/FILE#", ml);
            lox.SetDomain("Method", Scope.Method);  lox.Info("", ""); CICHECK_RL("@/FILE/METHOD#", ml);

            // source path
            lox.SetDomain("REPLACE", Scope.Path);  lox.Info("", ""); CICHECK_RL("@/REPLACE/FILE/METHOD#", ml);
            lox.SetDomain("PATH", Scope.Path);  lox.Info("", ""); CICHECK_RL("@/PATH/FILE/METHOD#", ml);
            lox.SetDomain("REPLACE", Scope.Path, 1);  lox.Info("", ""); CICHECK_RL("@/REPLACE/PATH/FILE/METHOD#", ml);
            lox.SetDomain("PO1", Scope.Path, 1);  lox.Info("", ""); CICHECK_RL("@/PO1/PATH/FILE/METHOD#", ml);
            lox.SetDomain("REPLACE", Scope.Path, 2);  lox.Info("", ""); CICHECK_RL("@/REPLACE/PO1/PATH/FILE/METHOD#", ml);
            lox.SetDomain("PO2", Scope.Path, 2);  lox.Info("", ""); CICHECK_RL("@/PO2/PO1/PATH/FILE/METHOD#", ml);

            lox.SetDomain("GLOBAL", Scope.Global);  lox.Info("", ""); CICHECK_RL("@/GLOBAL/PO2/PO1/PATH/FILE/METHOD#", ml);


            // remove all previous scope domains
            lox.SetDomain("", Scope.Global);
            lox.SetDomain("", Scope.Path);
            lox.SetDomain("", Scope.Path, 1);
            lox.SetDomain("", Scope.Path, 2);
            lox.SetDomain("", Scope.Filename);
            lox.SetDomain("", Scope.Method);  lox.Info("LOC", ""); SDCHECK_RL("@/LOC#", ml);

            // Thread-related
            lox.SetDomain("T_O", Scope.ThreadOuter);  lox.Info("", ""); SDCHECK_RL("@/T_O#", ml);
            lox.SetDomain("GL", Scope.Global);  lox.Info("", ""); SDCHECK_RL("@/GL/T_O#", ml);
            lox.SetDomain("MET", Scope.Method);  lox.Info("", ""); CICHECK_RL("@/GL/T_O/MET#", ml);
            lox.Info("LOC", ""); CICHECK_RL("@/GL/T_O/MET/LOC#", ml);
            lox.SetDomain("T_I", Scope.ThreadInner);  lox.Info("", ""); CICHECK_RL("@/GL/T_O/MET/T_I#", ml);
            lox.Info("LOC", ""); CICHECK_RL("@/GL/T_O/MET/LOC/T_I#", ml);
            lox.SetDomain("T_O2", Scope.ThreadOuter);  lox.Info("", ""); CICHECK_RL("@/GL/T_O/T_O2/MET/T_I#", ml);
            lox.SetDomain("T_I2", Scope.ThreadInner);  lox.Info("", ""); CICHECK_RL("@/GL/T_O/T_O2/MET/T_I/T_I2#", ml);

            lox.SetDomain("/T_O3", Scope.ThreadOuter);  lox.Info("", ""); CICHECK_RL("@/T_O3/MET/T_I/T_I2#", ml);
            lox.SetDomain("/T_I3", Scope.ThreadInner);  lox.Info("", ""); SDCHECK_RL("@/T_I3#", ml);
            lox.SetDomain("", Scope.Method);  lox.Info("", ""); SDCHECK_RL("@/T_I3#", ml);

            lox.SetDomain("", Scope.ThreadInner);  lox.Info("", ""); SDCHECK_RL("@/T_O3/T_I/T_I2#", ml);
            lox.RemoveThreadDomain("T_IXX", Scope.ThreadInner);  lox.Info("", ""); SDCHECK_RL("@/T_O3/T_I/T_I2#", ml);
            lox.RemoveThreadDomain("T_I", Scope.ThreadInner);  lox.Info("", ""); SDCHECK_RL("@/T_O3/T_I2#", ml);
            lox.RemoveThreadDomain("", Scope.ThreadInner);  lox.Info("", ""); SDCHECK_RL("@/T_O3/T_I2#", ml);
            lox.SetDomain("", Scope.ThreadInner);  lox.Info("", ""); SDCHECK_RL("@/T_O3#", ml);
            lox.SetDomain("", Scope.ThreadOuter);  lox.Info("", ""); SDCHECK_RL("@/GL/T_O/T_O2#", ml);
            lox.SetDomain("T_O3", Scope.ThreadOuter);  lox.Info("", ""); SDCHECK_RL("@/GL/T_O/T_O2/T_O3#", ml);
            lox.RemoveThreadDomain("T_O2", Scope.ThreadOuter);  lox.Info("", ""); SDCHECK_RL("@/GL/T_O/T_O3#", ml);
            lox.RemoveThreadDomain("T_O", Scope.ThreadOuter);  lox.Info("", ""); SDCHECK_RL("@/GL/T_O3#", ml);
            lox.SetDomain("", Scope.ThreadOuter);  lox.Info("", ""); SDCHECK_RL("@/GL#", ml);
            lox.SetDomain("", Scope.Global);  lox.Info("", ""); SDCHECK_RL("@/#", ml);


            // second thread
            Thread thread = new Thread(new ParameterizedThreadStart(DomainTestThreadRun_RL));

            lox.SetDomain("THIS_THREAD", Scope.ThreadOuter);
            lox.SetDomain("OTHER_THREAD", Scope.ThreadOuter, thread);
            thread.Start(lox);
            while (thread.IsAlive)
            {
                ALIB.SleepMillis(1);
            }
            UT_EQ("@/OTHER_THREAD/DTT#", ml.MemoryLog);  ml.MemoryLog._(); ml.AutoSizes.Reset();
            lox.Info("ME", "");  UT_EQ("@/THIS_THREAD/ME#", ml.MemoryLog);  ml.MemoryLog._(); ml.AutoSizes.Reset();

            // cleanup
            lox.RemoveLogger(consoleLogger);
            lox.RemoveLogger(ml);
        }
        public void Log_ScopeDomains()
        {
            UT_INIT();

            // we have tell alox to include more directories in the scope path
            Log.ClearSourcePathTrimRules(Inclusion.Include, false);
            Log.SetSourcePathTrimRule("*/alox/src.cs/", Inclusion.Exclude);

            Log.AddDebugLogger();
            MemoryLogger ml = new MemoryLogger();

            ml.MetaInfo.Format._()._("@%D#");
            Log.SetVerbosity(ml, Verbosity.Verbose);
            Log.SetVerbosity(Log.DebugLogger, Verbosity.Verbose, ALox.InternalDomains);
            Log.SetDomain("REPLACE", Scope.Global);  Log.Info(""); SDCHECK("@/REPLACE#", ml);
            Log.SetDomain("GLOBAL", Scope.Global);  Log.Info(""); SDCHECK("@/GLOBAL#", ml);

            Log.SetDomain(null, Scope.Global);  Log.Info(""); SDCHECK("@/#", ml);

            // scope source
            Log.SetDomain("REPLACE", Scope.Filename);  Log.Info(""); SDCHECK("@/REPLACE#", ml);
            Log.SetDomain("FILE", Scope.Filename);  Log.Info(""); SDCHECK("@/FILE#", ml);

            // scope method
            Log.SetDomain("REPLACE", Scope.Method);  Log.Info(""); SDCHECK("@/FILE/REPLACE#", ml);
            Log.SetDomain("METHOD", Scope.Method);  Log.Info(""); SDCHECK("@/FILE/METHOD#", ml);
            Log.SetDomain("/ABS", Scope.Method);  Log.Info(""); SDCHECK("@/ABS#", ml);

            // unset method with null
            Log.SetDomain(null, Scope.Method);  Log.Info(""); SDCHECK("@/FILE#", ml);

            // unset method with ""
            Log.SetDomain("/METHOD", Scope.Method);  Log.Info(""); SDCHECK("@/METHOD#", ml);
            Log.SetDomain("", Scope.Method);  Log.Info(""); SDCHECK("@/FILE#", ml);
            Log.SetDomain("METHOD", Scope.Method);  Log.Info(""); SDCHECK("@/FILE/METHOD#", ml);

            // source path
            Log.SetDomain("REPLACE", Scope.Path);  Log.Info(""); SDCHECK("@/REPLACE/FILE/METHOD#", ml);
            Log.SetDomain("PATH", Scope.Path);  Log.Info(""); SDCHECK("@/PATH/FILE/METHOD#", ml);
            Log.SetDomain("REPLACE", Scope.Path, 1);  Log.Info(""); SDCHECK("@/REPLACE/PATH/FILE/METHOD#", ml);
            Log.SetDomain("PO1", Scope.Path, 1);  Log.Info(""); SDCHECK("@/PO1/PATH/FILE/METHOD#", ml);
            Log.SetDomain("REPLACE", Scope.Path, 2);  Log.Info(""); SDCHECK("@/REPLACE/PO1/PATH/FILE/METHOD#", ml);
            Log.SetDomain("PO2", Scope.Path, 2);  Log.Info(""); SDCHECK("@/PO2/PO1/PATH/FILE/METHOD#", ml);
            Log.SetDomain("PO50", Scope.Path, 50);  Log.Info(""); SDCHECK("@/PO50/PO2/PO1/PATH/FILE/METHOD#", ml);

            Log.SetDomain("GLOBAL", Scope.Global);  Log.Info("");          SDCHECK("@/GLOBAL/PO50/PO2/PO1/PATH/FILE/METHOD#", ml);
            CS_ALox_domains_helper.help(); SDCHECK("@/GLOBAL/PO50/PO2/PO1/PATH/HFILE/HMETHOD#", ml);
            Log.Info("");        SDCHECK("@/GLOBAL/PO50/PO2/PO1/PATH/FILE/METHOD#", ml);

            //Log.LogConfig( "", Verbosity.Info, "Configuration now is:" ); ml.MemoryLog._(); ml.AutoSizes.Reset();

            // remove all previous scope domains
            Log.SetDomain("", Scope.Global);
            Log.SetDomain("", Scope.Path);
            Log.SetDomain("", Scope.Path, 1);
            Log.SetDomain("", Scope.Path, 2);
            Log.SetDomain("", Scope.Path, 45);      // same as 50 above! (test)
            Log.SetDomain("", Scope.Filename);
            Log.SetDomain("", Scope.Method);  Log.Info("LOC", ""); SDCHECK("@/LOC#", ml);

            // Thread-related
            Log.SetDomain("T_O", Scope.ThreadOuter);  Log.Info("", ""); SDCHECK("@/T_O#", ml);
            Log.SetDomain("GL", Scope.Global);  Log.Info("", ""); SDCHECK("@/GL/T_O#", ml);
            Log.SetDomain("MET", Scope.Method);  Log.Info("", ""); SDCHECK("@/GL/T_O/MET#", ml);
            Log.SetDomain("MET", Scope.Method);  Log.Info("LOC", ""); SDCHECK("@/GL/T_O/MET/LOC#", ml);
            Log.SetDomain("T_I", Scope.ThreadInner);  Log.Info("", ""); SDCHECK("@/GL/T_O/MET/T_I#", ml);

            Log.Info("LOC", ""); SDCHECK("@/GL/T_O/MET/LOC/T_I#", ml);
            Log.SetDomain("T_O2", Scope.ThreadOuter);  Log.Info("", ""); SDCHECK("@/GL/T_O/T_O2/MET/T_I#", ml);
            Log.SetDomain("T_I2", Scope.ThreadInner);  Log.Info("", ""); SDCHECK("@/GL/T_O/T_O2/MET/T_I/T_I2#", ml);

            Log.SetDomain("/T_O3", Scope.ThreadOuter);  Log.Info("", ""); SDCHECK("@/T_O3/MET/T_I/T_I2#", ml);
            Log.SetDomain("/T_I3", Scope.ThreadInner);  Log.Info("", ""); SDCHECK("@/T_I3#", ml);

            Log.SetDomain("", Scope.ThreadInner);  Log.Info("", ""); SDCHECK("@/T_O3/MET/T_I/T_I2#", ml);
            Log.RemoveThreadDomain("T_IXX", Scope.ThreadInner);  Log.Info("", ""); SDCHECK("@/T_O3/MET/T_I/T_I2#", ml);
            Log.RemoveThreadDomain("T_I", Scope.ThreadInner);  Log.Info("", ""); SDCHECK("@/T_O3/MET/T_I2#", ml);
            Log.RemoveThreadDomain("", Scope.ThreadInner);  Log.Info("", ""); SDCHECK("@/T_O3/MET/T_I2#", ml);
            Log.SetDomain("", Scope.ThreadInner);  Log.Info("", ""); SDCHECK("@/T_O3/MET#", ml);
            Log.SetDomain("", Scope.ThreadOuter);  Log.Info("", ""); SDCHECK("@/GL/T_O/T_O2/MET#", ml);
            Log.SetDomain("T_O3", Scope.ThreadOuter);  Log.Info("", ""); SDCHECK("@/GL/T_O/T_O2/T_O3/MET#", ml);
            Log.RemoveThreadDomain("T_O2", Scope.ThreadOuter);  Log.Info("", ""); SDCHECK("@/GL/T_O/T_O3/MET#", ml);
            Log.RemoveThreadDomain("T_O", Scope.ThreadOuter);  Log.Info("", ""); SDCHECK("@/GL/T_O3/MET#", ml);
            Log.SetDomain("", Scope.ThreadOuter);  Log.Info("", ""); SDCHECK("@/GL/MET#", ml);
            Log.SetDomain("", Scope.Global);  Log.Info("", ""); SDCHECK("@/MET#", ml);
            Log.SetDomain("", Scope.Method);  Log.Info("", ""); SDCHECK("@/#", ml);

            // second thread
            Thread thread = new Thread(new ThreadStart(DomainTestThreadRun));

            Log.SetDomain("THIS_THREAD", Scope.ThreadOuter);
            Log.SetDomain("OTHER_THREAD", Scope.ThreadOuter, thread);
            thread.Start();
            while (thread.IsAlive)
            {
                ALIB.SleepMillis(1);
            }
            UT_EQ("@/OTHER_THREAD/DTT#", ml.MemoryLog);  ml.MemoryLog._(); ml.AutoSizes.Reset();
            Log.Info("ME", "");  UT_EQ("@/THIS_THREAD/ME#", ml.MemoryLog);  ml.MemoryLog._(); ml.AutoSizes.Reset();
        }
예제 #8
0
        public void Basics()
        {
            UT_INIT();

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

            Log.Info("\n### TicksBasics ###");

            // check times
            {
                Ticks t = new Ticks();
                t.FromNanos(42);      // beyond resolution in C#:  UT_EQ( t.InNanos(),        42L);
                UT_EQ(t.InMicros(), 0L);
                UT_EQ(t.InMillis(), 0L);
                UT_EQ(t.InSeconds(), 0L);
                t.FromMicros(42);    UT_EQ(t.InNanos(), 42000L);
                UT_EQ(t.InMicros(), 42L);
                UT_EQ(t.InMillis(), 0L);
                UT_EQ(t.InSeconds(), 0L);
                t.FromMillis(42);    UT_EQ(t.InNanos(), 42000000L);
                UT_EQ(t.InMicros(), 42000L);
                UT_EQ(t.InMillis(), 42L);
                UT_EQ(t.InSeconds(), 0L);
                t.FromSeconds(42);    UT_EQ(t.InNanos(), 42000000000L);
                UT_EQ(t.InMicros(), 42000000L);
                UT_EQ(t.InMillis(), 42000L);
                UT_EQ(t.InSeconds(), 42L);
                Ticks diff = new Ticks();
                diff.FromMillis(100);
                t.Add(diff);            UT_EQ(t.InNanos(), 42100000000L);
                UT_EQ(t.InMicros(), 42100000L);
                UT_EQ(t.InMillis(), 42100L);
                UT_EQ(t.InSeconds(), 42L);
                t.Sub(diff);            UT_EQ(t.InNanos(), 42000000000L);
                UT_EQ(t.InMicros(), 42000000L);
                UT_EQ(t.InMillis(), 42000L);
                UT_EQ(t.InSeconds(), 42L);

                t.FromMillis(100); UT_EQ(t.InHertz(), 10.0);
                t.FromMillis(300); UT_EQ(t.InHertz(0), 3.0);
                UT_EQ(t.InHertz(1), 3.3);
                UT_EQ(t.InHertz(2), 3.33);
                UT_EQ(t.InHertz(5), 3.33333);
            }

            // check internal frequency
            {
                double freq = Ticks.InternalFrequency;
                Log.Info("TickWatch InternalFrequency:   " + freq);
                UT_TRUE(freq >= 1000000.0);
            }

            // check TickWatch creation time
            {
                Ticks creationTimeDiff = new Ticks();
                creationTimeDiff.Sub(Ticks.CreationTime);
                Log.Info("TickWatch library creation was: " + creationTimeDiff.InNanos() + "ns ago");
                Log.Info("TickWatch library creation was: " + creationTimeDiff.InMicros() + "µs ago");
                Log.Info("TickWatch library creation was: " + creationTimeDiff.InMillis() + "ms ago");
                Log.Info("TickWatch library creation was: " + creationTimeDiff.InSeconds() + "s ago");
                UT_TRUE(creationTimeDiff.InNanos() > 100);       // It should really take 100 nanoseconds to get here!
                UT_TRUE(creationTimeDiff.InSeconds() < 3600);    // these test will probably not last an hour
            }

            // check if we could sleep for 100ms
            {
                Ticks start = new Ticks();
                ALIB.SleepMillis(30);
                Ticks sleepTime = new Ticks();
                sleepTime.Sub(start);
                Log.Info("TickWatch diff after 100ms sleep: " + sleepTime.InMillis() + " ms");
                UT_TRUE(sleepTime.InMillis() > 28);
                UT_TRUE(sleepTime.InMillis() < 150);    // should work even on heavily loaded machines
            }
        }
예제 #9
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);
            }
        }