private string GetModeString(ProfilerTypes profilerType)
 {
     string profilerTypeName = profilerType.ToString();
     int indexOfProfiler = profilerTypeName.IndexOf("Profiler");
     string modeString = profilerTypeName.Substring(0, profilerTypeName.Length - indexOfProfiler - 1);
     return modeString;
 }
        private string GetModeString(ProfilerTypes profilerType)
        {
            string profilerTypeName = profilerType.ToString();
            int    indexOfProfiler  = profilerTypeName.IndexOf("Profiler");
            string modeString       = profilerTypeName.Substring(0, profilerTypeName.Length - indexOfProfiler - 1);

            return(modeString);
        }
        public void Profile(ProfilerTypes profiler, string processPath)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo {
                FileName = processPath
            };

            CriterionSwitchViewModel[] criterionSwitchVMs;

            if (profiler == ProfilerTypes.TracingProfiler)
            {
                var profilerAccess = new TracingProfilerAccess(
                    processStartInfo,
                    TimeSpan.FromMilliseconds(1000),
                    OnUpdateCallback);
                _closeProfileeProcessAction = profilerAccess.CloseProfileeProcess;
                profilerAccess.StartProfiler();
                _uiLogic.ActiveCriterion = TracingCriteriaContext.CallCountCriterion;

                criterionSwitchVMs = new[] {
                    new CriterionSwitchViewModel(TracingCriteriaContext.CallCountCriterion)
                    {
                        IsActive = true
                    },
                    new CriterionSwitchViewModel(TracingCriteriaContext.TimeActiveCriterion),
                    new CriterionSwitchViewModel(TracingCriteriaContext.TimeWallClockCriterion)
                };
            }
            else
            {
                var profilerAccess = new SamplingProfilerAccess(
                    processStartInfo,
                    TimeSpan.FromMilliseconds(1000),
                    OnUpdateCallback);
                _closeProfileeProcessAction = profilerAccess.CloseProfileeProcess;
                profilerAccess.StartProfiler();
                _uiLogic.ActiveCriterion = SamplingCriteriaContext.TopStackOccurrenceCriterion;

                criterionSwitchVMs = new[] {
                    new CriterionSwitchViewModel(SamplingCriteriaContext.TopStackOccurrenceCriterion)
                    {
                        IsActive = true
                    },
                    new CriterionSwitchViewModel(SamplingCriteriaContext.DurationCriterion)
                };
            }

            foreach (var switchVM in criterionSwitchVMs)
            {
                switchVM.CriterionChanged += _uiLogic.ActivateCriterion;
            }

            criteriaSwitch.DataContext  = criterionSwitchVMs;
            _uiLogic.CriterionSwitchVMs = criterionSwitchVMs;
        }
        private void StartProfiler(ProfilerTypes profilerType)
        {
            try
            {
                bool buildSuccedded = StartBuild();
                if (!buildSuccedded)
                {
                    throw new Exception("Could not build the solution.");
                }

                Project startUpProject = GetStartUpProject();
                if (startUpProject == null)
                {
                    throw new Exception("Could not locate a startUp project.");
                }

                string assemblyPath = GetOutputAssemblyPath(startUpProject);
                if (string.IsNullOrEmpty(assemblyPath))
                {
                    throw new Exception("Could not locate the output assembly.");
                }
                if (Path.GetExtension(assemblyPath) != ".exe")
                {
                    throw new Exception("The output of the startUp project is not an executable.");
                }
                if (!File.Exists(assemblyPath))
                {
                    throw new Exception("The output executable file could not be found.");
                }

                _window = this.FindToolWindow(typeof(VisualProfilerToolWindow), 0, true) as VisualProfilerToolWindow;
                if ((null == _window) || (null == _window.Frame))
                {
                    throw new NotSupportedException("Cannot create a window.");
                }

                UILogic uiLogic = _window.VisualProfilerUIView.UILogic;
                _window.VisualProfilerUIView.UILogic.MethodClick += mvm => OnMethodClick(uiLogic, mvm);
                _window.VisualProfilerUIView.Profile(profilerType, assemblyPath);
                _window.VisualProfilerUIView.DataUpdate += ContainingUnitView.UpdateDataOfContainingUnits;
                _window.Caption = string.Format("Visual Profiler - {0} Mode", GetModeString(profilerType));
                IVsWindowFrame windowFrame = (IVsWindowFrame)_window.Frame;
                ErrorHandler.ThrowOnFailure(windowFrame.Show());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Profiler initialization failed.", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        public ProfilerCommunicator(
            ProfilerTypes profilerType,
            ICallTreeFactory <TCallTree> callTreeFactory,
            MetadataDeserializer metadataDeserializer,
            MetadataCache <MethodMetadata> methodCache,
            EventHandler <ProfilingDataUpdateEventArgs <TCallTree> > updateCallback)
        {
            Contract.Requires(callTreeFactory != null);
            Contract.Requires(updateCallback != null);

            _callTreeFactory      = callTreeFactory;
            _metadataDeserializer = metadataDeserializer;
            _methodCache          = methodCache;
            _updateCallback       = updateCallback;
            _profilerType         = profilerType;
        }
        public ProfilerAccess(
            ProcessStartInfo profileeProcessStartInfo,
            ProfilerTypes profilerType,
            TimeSpan profilingDataUpdatePeriod,
            ProfilerCommunicator <TCallTree> profilerCommunicator)
        {
            Contract.Requires(profileeProcessStartInfo != null);
            Contract.Requires(profilerCommunicator != null);

            _profilerCommunicator    = profilerCommunicator;
            ProfilerType             = profilerType;
            ProfileeProcessStartInfo = profileeProcessStartInfo;
            ProfilerDataUpdatePeriod = profilingDataUpdatePeriod;
            ProfilerStarted          = false;
            _namePipeName            = "VisualProfilerAccessPipe" + new Random(DateTime.Now.Millisecond).Next(int.MaxValue);
        }
        public void Profile(ProfilerTypes profiler, string processPath)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = processPath };
            CriterionSwitchViewModel[] criterionSwitchVMs;

            if (profiler == ProfilerTypes.TracingProfiler)
            {
                var profilerAccess = new TracingProfilerAccess(
                    processStartInfo,
                    TimeSpan.FromMilliseconds(1000),
                    OnUpdateCallback);
                _closeProfileeProcessAction = profilerAccess.CloseProfileeProcess;
                profilerAccess.StartProfiler();
                _uiLogic.ActiveCriterion = TracingCriteriaContext.CallCountCriterion;

                criterionSwitchVMs = new[] {
                new CriterionSwitchViewModel(TracingCriteriaContext.CallCountCriterion){IsActive = true},
                new CriterionSwitchViewModel(TracingCriteriaContext.TimeActiveCriterion),
                new CriterionSwitchViewModel(TracingCriteriaContext.TimeWallClockCriterion)};
            }
            else
            {
                var profilerAccess = new SamplingProfilerAccess(
                    processStartInfo,
                    TimeSpan.FromMilliseconds(1000),
                    OnUpdateCallback);
                _closeProfileeProcessAction = profilerAccess.CloseProfileeProcess;
                profilerAccess.StartProfiler();
                _uiLogic.ActiveCriterion = SamplingCriteriaContext.TopStackOccurrenceCriterion;

                criterionSwitchVMs = new[] {
                new CriterionSwitchViewModel(SamplingCriteriaContext.TopStackOccurrenceCriterion){IsActive = true},
                new CriterionSwitchViewModel(SamplingCriteriaContext.DurationCriterion)};
            }

            foreach (var switchVM in criterionSwitchVMs)
            {
                switchVM.CriterionChanged += _uiLogic.ActivateCriterion;
            }

            criteriaSwitch.DataContext = criterionSwitchVMs;
            _uiLogic.CriterionSwitchVMs = criterionSwitchVMs;
        }
        private void StartProfiler(ProfilerTypes profilerType)
        {
            try
            {
                bool buildSuccedded = StartBuild();
                if (!buildSuccedded) throw new Exception("Could not build the solution.");

                Project startUpProject = GetStartUpProject();
                if (startUpProject == null) throw new Exception("Could not locate a startUp project.");

                string assemblyPath = GetOutputAssemblyPath(startUpProject);
                if (string.IsNullOrEmpty(assemblyPath)) throw new Exception("Could not locate the output assembly.");
                if (Path.GetExtension(assemblyPath) != ".exe") throw new Exception("The output of the startUp project is not an executable.");
                if (!File.Exists(assemblyPath)) throw new Exception("The output executable file could not be found.");

                _window = this.FindToolWindow(typeof(VisualProfilerToolWindow), 0, true) as VisualProfilerToolWindow;
                if ((null == _window) || (null == _window.Frame)) throw new NotSupportedException("Cannot create a window.");

                UILogic uiLogic = _window.VisualProfilerUIView.UILogic;
                _window.VisualProfilerUIView.UILogic.MethodClick += mvm => OnMethodClick(uiLogic, mvm);
                _window.VisualProfilerUIView.Profile(profilerType, assemblyPath);
                _window.VisualProfilerUIView.DataUpdate += ContainingUnitView.UpdateDataOfContainingUnits;
                _window.Caption = string.Format("Visual Profiler - {0} Mode", GetModeString(profilerType));
                IVsWindowFrame windowFrame = (IVsWindowFrame)_window.Frame;
                ErrorHandler.ThrowOnFailure(windowFrame.Show());

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Profiler initialization failed.", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }