private void SaveQueuedSessions()
        {
            do
            {
                // Suspend for a while
                //_processWait.WaitOne(ThreadSleepMilliseconds, exitContext: false);
                _processWait.WaitOne(ThreadSleepMilliseconds);

                // Upgrade to foreground thread
                Thread.CurrentThread.IsBackground = false;

                // set null the current profiling session bound to the running thread to release the memory
                ProfilingSession.SetCurrentProfilingSession(null);

                // Save all the queued sessions
                ITimingSession session;
                while (TryDequeue(out session))
                {
                    Save(session);

                    // Signal waiting threads to continue
                    _entryWait.Set();
                }

                // Downgrade to background thread while waiting
                Thread.CurrentThread.IsBackground = true;
            } while (true);
        }
Пример #2
0
        public void TestProfilingSession_SetCurrentProfilingSession()
        {
            // mock profiler
            var mockProfiler  = new Mock <IProfiler>();
            var timingSession = new TimingSession(mockProfiler.Object, "test", null);

            timingSession.AddTiming(new Timing(mockProfiler.Object, "step", timingSession.Id, "root", null));
            timingSession.AddTiming(new Timing(mockProfiler.Object, "step", timingSession.Timings.First().Id, "step2", null));
            mockProfiler.Setup(p => p.Id).Returns(timingSession.Id);
            mockProfiler.Setup(p => p.GetTimingSession()).Returns(timingSession);

            var session = new ProfilingSession(mockProfiler.Object);
            var success = false;

            // execute in async thread to avoid conflicts
            var thread = new Thread(() =>
            {
                lock (typeof(ProfilingSession))
                {
                    ProfilingSession.SetCurrentProfilingSession(session);

                    Assert.AreEqual(session, ProfilingSession.ProfilingSessionContainer.CurrentSession);
                    Assert.AreEqual(timingSession.Timings.First().Id, ProfilingSession.ProfilingSessionContainer.CurrentSessionStepId);

                    ProfilingSession.SetCurrentProfilingSession(session, timingSession.Timings.Last().Id);
                    Assert.AreEqual(timingSession.Timings.Last().Id, ProfilingSession.ProfilingSessionContainer.CurrentSessionStepId);
                }

                success = true;
            });

            thread.Start();
            thread.Join();

            Assert.IsTrue(success);
        }