Beispiel #1
0
        public StopwatchWrapper StartNew()
        {
            var stopwatch = new StopwatchWrapper(this);

            stopwatch.Start();
            return(stopwatch);
        }
Beispiel #2
0
        public void AddStopwatch(int id)
        {
            var stopwatch = new StopwatchWrapper(id);

            _stopwatches.Add(stopwatch);
            _stopwatches[_stopwatches.Count - 1].Start();
        }
Beispiel #3
0
 private MiniProfiler()
 {
     IsActive    = true;
     SqlProfiler = new SqlProfiler(this);
     _sw         = StopwatchWrapper.StartNew();
     Id          = Guid.NewGuid();
     Root        = new Timing(this, null, "empty");
 }
Beispiel #4
0
        public void Step_WithParallelThreads_RealTime()
        {
            var profiler = MiniProfiler.Start("root");

            // Need real wall-time here - hard to simulate in a fake
            profiler.Stopwatch = StopwatchWrapper.StartNew();

            // Add 100ms to root just to offset the starting point
            Thread.Sleep(100);

            // Run up to 3 threads at a time (system and scheduler dependent),
            // each waiting 10 * 50 ms = 500 ms
            Parallel.For(0, 3, i =>
            {
                using (profiler.Step($"thread[{i}]"))
                {
                    foreach (int j in Enumerable.Range(0, 5))
                    {
                        using (profiler.Step($"work[{i}/{j}]"))
                        {
                            Thread.Sleep(50);
                        }
                    }
                }
            });

            MiniProfiler.Stop();

            // Assert
            //Console.WriteLine(profiler.RenderPlainText());

            // The total run time is non-deterministic and depends
            // on the system and the scheduler, so we can only assert
            // each thread's duration
            var hierarchy = profiler.GetTimingHierarchy().ToList();

            foreach (var timing in hierarchy)
            {
                if (timing.Name.StartsWith("thread", StringComparison.Ordinal))
                {
                    // 3 work items, 50 ms each
                    AssertNear(250, timing.DurationMilliseconds, 100);
                }
                else if (timing.Name.StartsWith("work", StringComparison.Ordinal))
                {
                    // 50 ms each work item
                    AssertNear(50, timing.DurationMilliseconds, 20);
                }
            }
        }
Beispiel #5
0
    // Start is called before the first frame update
    void Start()
    {
        this.GetComponent <TargetLogic>().sw_click = new StopwatchWrapper();
        this.GetComponent <TargetLogic>().sw_gaze  = new StopwatchWrapper();
        this.GetComponent <TargetLogic>().tobii    = new TobiiHelper();
        this.transform.position = new Vector2(Random.Range(-5, 5), Random.Range(-5, 5));

        this.GetComponent <TargetLogic>().cam = Camera.main;
        Vector3 original = new Vector3(this.transform.position.x, this.transform.position.y, 0);
        Vector3 viewPort = cam.WorldToViewportPoint(original);

        this.GetComponent <TargetLogic>().x    = viewPort.x;
        this.GetComponent <TargetLogic>().y    = viewPort.y;
        this.GetComponent <Renderer>().enabled = false;
    }
        public static void DemoOutVsReturn()
        {
            Benchmarker.Benchmark(() =>
            {
                StopwatchWrapper.Time(() =>
                {
                }, out var sw1);
            }, "Out parameter", _count);

            Benchmarker.Benchmark(() =>
            {
                var sw2 = StopwatchWrapper.Time(() =>
                {
                });
            }, "Return", _count);

            Console.WriteLine();
        }
Beispiel #7
0
 public TimeTarget()
 {
     _stopWatch = StopwatchWrapper.StartNew();
 }
Beispiel #8
0
        public async Task Step_WithParallelTasks_RealTime()
        {
            var profiler = MiniProfiler.Start("root");

            profiler.Stopwatch = StopwatchWrapper.StartNew();

            Timing timing10 = null,
                   timing11 = null,
                   timing20 = null,
                   timing21 = null,
                   timing30 = null,
                   timing31 = null;

            // Act

            // Add 100ms to root
            await Task.Delay(100).ConfigureAwait(false);

            // Start tasks in parallel
            var whenAllTask = Task.WhenAll(
                Task.Run(async() =>
            {
                // timing10: 100 + 100 = 200 ms
                using (timing10 = profiler.Step("step1.0 (Task.Run)"))
                {
                    await Task.Delay(100).ConfigureAwait(false);
                    await Task.Run(async() =>
                    {
                        using (timing11 = profiler.Step("step1.1 (Task.Run)"))
                        {
                            await Task.Delay(100).ConfigureAwait(false);
                        }
                    }).ConfigureAwait(false);
                }
            }),
                Task.Factory.StartNew(async() =>
            {
                // timing20: 200 + 100 = 300 ms
                using (timing20 = profiler.Step("step2.0 (Task.Factory.StartNew)"))
                {
                    await Task.Delay(200).ConfigureAwait(false);
                    await Task.Run(async() =>
                    {
                        using (timing21 = profiler.Step("step2.1 (Task.Run)"))
                        {
                            await Task.Delay(100).ConfigureAwait(false);
                        }
                    }).ConfigureAwait(false);
                }
                // Important to Unwrap() when using the not-for-mortals StartNew()
            }).Unwrap(),
                Task.Factory.StartNew(async() =>
            {
                // timing30: 300 + 100 = 400 ms
                using (timing30 = profiler.Step("step3.0 (Task.Factory.StartNew:LongRunning)"))
                {
                    await Task.Delay(300).ConfigureAwait(false);
                    await Task.Run(async() =>
                    {
                        using (timing31 = profiler.Step("step3.1 (Task.Run)"))
                        {
                            await Task.Delay(100).ConfigureAwait(false);
                        }
                    }).ConfigureAwait(false);
                }
                // Important to Unwrap() when using the not-for-mortals StartNew()
            }, TaskCreationOptions.LongRunning).Unwrap()
                );

            await whenAllTask;

            MiniProfiler.Stop();

            // Assert
            //Console.WriteLine(profiler.RenderPlainText());

            // 100ms + longest running task (step3.0 with 300 + 100 ms) = 500ms
            AssertNear(500, profiler.DurationMilliseconds, 50);

            // Parent durations are sum of itself and children
            AssertNear(200, timing10.DurationMilliseconds, 50);
            AssertNear(100, timing11.DurationMilliseconds, 50);

            AssertNear(300, timing20.DurationMilliseconds, 50);
            AssertNear(100, timing21.DurationMilliseconds, 50);

            AssertNear(400, timing30.DurationMilliseconds, 50);
            AssertNear(100, timing31.DurationMilliseconds, 50);
        }
 public StopwatchWrapperTests()
 {
     _stopwatch = Stopwatch.StartNew();
     _subject   = new StopwatchWrapper(_stopwatch);
 }