Ejemplo n.º 1
0
        public void TestStaticClock()
        {
            System.Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

            //There must be a static clock
            if (staticClock == null)
            {
                throw new System.InvalidOperationException("There is no static Clock.");
            }

            //The static clock also has JIT overhead

            System.Console.WriteLine("Static SystemClockResolution: " + staticClock.SystemClockResolution);

            System.Console.WriteLine("Static TicksPerUpdate: " + staticClock.TicksPerUpdate);

            System.Console.WriteLine("Static OperationsPerClockUpdate: " + staticClock.InstructionsPerClockUpdate);

            System.Console.WriteLine("Static AverageOperationsPerTick: " + staticClock.AverageOperationsPerTick);

            //Probably not very accurate but WILL NEVER reflect a frequency the CPU is not capable of running at (Turbo mode)
            System.Console.WriteLine("CPU Estimated Frequency:" + staticClock.InstructionsPerClockUpdate / 1000 + "hZ");

            //Perform the same logic using a reference to the clock (The values are the same)

            //Make a reference to the clock which should not dispose
            using (Media.Concepts.Classes.Clock staticClockReference = staticClock)
            {
                System.Console.WriteLine("Static SystemClockResolution: " + staticClockReference.SystemClockResolution);

                System.Console.WriteLine("Static TicksPerUpdate: " + staticClockReference.TicksPerUpdate);

                System.Console.WriteLine("Static OperationsPerClockUpdate: " + staticClockReference.InstructionsPerClockUpdate);

                System.Console.WriteLine("Static AverageOperationsPerTick: " + staticClockReference.AverageOperationsPerTick);

                System.Console.WriteLine("CPU Estimated Frequency:" + staticClockReference.InstructionsPerClockUpdate / 1000 + "hZ");
            }

            //The staticClock must not dispose
            if (staticClock.IsDisposed != staticClock.ShouldDispose)
            {
                throw new System.InvalidOperationException("staticClock cannot be Disposed.");
            }
        }
Ejemplo n.º 2
0
        public void TestClock()
        {
            System.Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

            //There must be a static clock
            if (staticClock == null)
            {
                throw new System.InvalidOperationException("There is no static Clock.");
            }

            //Call Dispose on the staticClock, just to test it
            staticClock.Dispose();

            //The staticClock must not dispose
            if (staticClock.IsDisposed != staticClock.ShouldDispose)
            {
                throw new System.InvalidOperationException("staticClock cannot be Disposed.");
            }

            //Make a new clock
            using (var clock = new Media.Concepts.Classes.Clock())
            {
                System.Console.WriteLine("SystemClockResolution: " + clock.SystemClockResolution);

                System.Console.WriteLine("TicksPerUpdate: " + clock.TicksPerUpdate);

                System.Console.WriteLine("OperationsPerClockUpdate: " + clock.InstructionsPerClockUpdate);

                System.Console.WriteLine("AverageOperationsPerTick: " + clock.AverageOperationsPerTick);

                //Convert from ticks to hZ
                System.Console.WriteLine("CPU Estimated Frequency:" + clock.InstructionsPerClockUpdate / 1000 + "hZ");

                //These values may or may not have been obtained based on a system time update which recently occured

                //For as many times are as indicated in the Ticks per update from the staticClock perform a test
                for (int i = 0; i < staticClock.TicksPerUpdate; ++i)
                {
                    using (var clockTest = new Media.Concepts.Classes.Clock())
                    {
                        System.Console.WriteLine("SystemClockResolution: " + clockTest.SystemClockResolution);

                        System.Console.WriteLine("TicksPerUpdate: " + clockTest.TicksPerUpdate);

                        System.Console.WriteLine("OperationsPerClockUpdate: " + clockTest.InstructionsPerClockUpdate);

                        System.Console.WriteLine("AverageOperationsPerTick: " + clockTest.AverageOperationsPerTick);

                        //Convert from ticks to hZ
                        System.Console.WriteLine("CPU Estimated Frequency:" + clock.InstructionsPerClockUpdate / 1000 + "hZ");

                        //
                        System.Console.WriteLine("CPU 1 Tick = " + clock.EstimateOperations(Common.Extensions.TimeSpan.TimeSpanExtensions.OneTick) + " ops");

                        System.Console.WriteLine("CPU 1000000 Ops in " + clock.EstimateTime(1000000));

                        var a = clock.UtcNow;

                        System.Console.WriteLine("Now: " + clock.UtcNow);

                        clock.NanoSleep(1000);

                        var b = clock.UtcNow;

                        System.Console.WriteLine("Now: " + clock.UtcNow);

                        System.Console.WriteLine("Taken: " + (b - a));
                    }
                }

                #region Notes on results

                //Even with a RTOS
                //http://en.wikipedia.org/wiki/Real-time_operating_system

                //Out of the above runs you should see close to the same values as produced by the initial clock and somewhat different that that of the static clock
                //If you run this test multiple times you will see seemingly random results.
                //This is due to CPU cache access which allows the code to sometimes run in between updates of the system clock if the code is in cache.
                //Ironically this may make the CPU seem slower but what is occuring is that the System.Environment.TickCount changes in a smaller amount of physical units in the time domain.
                //In a RTOS there will be slightly less variation in between all results

                #endregion
            }
        }