[Adapdev-commits] Adapdev/src/Adapdev/Diagnostics CPUMeter.cs,1.2,1.3 HiPerfTimer.cs,1.2,1.3 IPerfTi
Status: Beta
Brought to you by:
intesar66
From: Sean M. <int...@us...> - 2005-11-16 05:33:39
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev/Diagnostics In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19977/src/Adapdev/Diagnostics Added Files: CPUMeter.cs HiPerfTimer.cs IPerfTimer.cs MillisecondsPerfTimer.cs MinutesPerfTimer.cs PerfTimerFactory.cs SecondsPerfTimer.cs TicksPerfTimer.cs Log Message: Reposting to the repository after it got hosed --- NEW FILE: SecondsPerfTimer.cs --- #region Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion namespace Adapdev.Diagnostics { using System; /// <summary> /// Summary description for DateTimePerfTimer. /// </summary> public class SecondsPerfTimer : IPerfTimer { private DateTime start; private TimeSpan stop; public SecondsPerfTimer() { } #region IPerfTimer Members public void Start() { start = DateTime.Now; } public void Stop() { stop = DateTime.Now.Subtract(start); } public double Duration { get { return stop.TotalSeconds; } } #endregion } } --- NEW FILE: IPerfTimer.cs --- #region Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion namespace Adapdev.Diagnostics { /// <summary> /// Summary description for PerfTimer. /// </summary> public interface IPerfTimer { void Start(); void Stop(); double Duration { get; } } } --- NEW FILE: MillisecondsPerfTimer.cs --- #region Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion namespace Adapdev.Diagnostics { using System; /// <summary> /// Summary description for DateTimePerfTimer. /// </summary> public class MillisecondsPerfTimer : IPerfTimer { private DateTime start; private TimeSpan stop; public MillisecondsPerfTimer() { } #region IPerfTimer Members public void Start() { start = DateTime.Now; } public void Stop() { stop = DateTime.Now.Subtract(start); } public double Duration { get { return stop.TotalMilliseconds; } } #endregion } } --- NEW FILE: MinutesPerfTimer.cs --- #region Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion namespace Adapdev.Diagnostics { using System; /// <summary> /// Summary description for DateTimePerfTimer. /// </summary> public class MinutesPerfTimer : IPerfTimer { private DateTime start; private TimeSpan stop; public MinutesPerfTimer() { } #region IPerfTimer Members public void Start() { start = DateTime.Now; } public void Stop() { stop = DateTime.Now.Subtract(start); } public double Duration { get { return stop.TotalMinutes; } } #endregion } } --- NEW FILE: TicksPerfTimer.cs --- #region Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion namespace Adapdev.Diagnostics { using System; /// <summary> /// Summary description for TicksPerfTimer. /// </summary> public class TicksPerfTimer : IPerfTimer { private int start; private int stop; public TicksPerfTimer() { // // TODO: Add constructor logic here // } #region IPerfTimer Members public void Start() { start = Environment.TickCount; } public void Stop() { stop = Environment.TickCount - start; } public double Duration { get { return stop; } } #endregion } } --- NEW FILE: PerfTimerFactory.cs --- #region Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion namespace Adapdev.Diagnostics { /// <summary> /// Summary description for PerfTimerFactory. /// </summary> public class PerfTimerFactory { public PerfTimerFactory() { // // TODO: Add constructor logic here // } public static IPerfTimer GetPerfTimer(PerfTimerType type) { switch (type) { case PerfTimerType.MILLISECONDS: return new MillisecondsPerfTimer(); case PerfTimerType.HIRESSECONDS: return new HiPerfTimer(); case PerfTimerType.SECONDS: return new SecondsPerfTimer(); case PerfTimerType.MINUTES: return new MinutesPerfTimer(); case PerfTimerType.TICKS: return new TicksPerfTimer(); default: return new MillisecondsPerfTimer(); } } } public enum PerfTimerType { TICKS, MILLISECONDS, HIRESSECONDS, SECONDS, MINUTES } } --- NEW FILE: HiPerfTimer.cs --- // Original Copyright 2002 Daniel Strigl. http://www.codeproject.com/csharp/highperformancetimercshar.asp?target=timer #region Modified Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion namespace Adapdev.Diagnostics { using System.ComponentModel; using System.Runtime.InteropServices; using System.Threading; internal class HiPerfTimer : IPerfTimer { [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceCounter( out long lpPerformanceCount); [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceFrequency( out long lpFrequency); private long startTime, stopTime; private long freq; // Constructor public HiPerfTimer() { startTime = 0; stopTime = 0; if (QueryPerformanceFrequency(out freq) == false) { // high-performance counter not supported throw new Win32Exception(); } } // Start the timer public void Start() { // lets do the waiting threads there work Thread.Sleep(0); QueryPerformanceCounter(out startTime); } // Stop the timer public void Stop() { QueryPerformanceCounter(out stopTime); } // Returns the duration of the timer (in seconds) public double Duration { get { double d = (stopTime - startTime); return d/freq; } } } } --- NEW FILE: CPUMeter.cs --- // Original Copyright (c) 2004, Ingo Rammer http://www.thinktecture.com/staff/ingo/weblog/archives/001403.html /** * Usage: * * static void Main(string[] args) * { * CPUMeter mtr = new CPUMeter(); * // do some heavy stuff * double result = 0; * for (int i = 0;i<100000000; i++) * { * result = result+Math.Sin(i); * } * double usage = mtr.GetCpuUtilization(); * Console.WriteLine("Done. CPU Usage {0:#00.00} %", usage); * Console.ReadLine(); * } * */ #region Modified Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion namespace Adapdev.Diagnostics { using System; using System.Diagnostics; public class CPUMeter : IDisposable { private CounterSample _startSample; private PerformanceCounter _cnt; /// Creates a per-process CPU meter instance tied to the current process. public CPUMeter() { String instancename = GetCurrentProcessInstanceName(); _cnt = new PerformanceCounter("Process", "% Processor Time", instancename, true); ResetCounter(); } /// Creates a per-process CPU meter instance tied to a specific process. public CPUMeter(int pid) { String instancename = GetProcessInstanceName(pid); _cnt = new PerformanceCounter("Process", "% Processor Time", instancename, true); ResetCounter(); } /// Resets the internal counter. All subsequent calls to GetCpuUtilization() will /// be relative to the point in time when you called ResetCounter(). This /// method can be call as often as necessary to get a new baseline for /// CPU utilization measurements. public void ResetCounter() { _startSample = _cnt.NextSample(); } /// Returns this process's CPU utilization since the last call to ResetCounter(). public double GetCpuUtilization() { CounterSample curr = _cnt.NextSample(); double diffValue = curr.RawValue - _startSample.RawValue; double diffTimestamp = curr.TimeStamp100nSec - _startSample.TimeStamp100nSec; double usage = (diffValue/diffTimestamp)*100; return usage; } private static string GetCurrentProcessInstanceName() { Process proc = Process.GetCurrentProcess(); int pid = proc.Id; return GetProcessInstanceName(pid); } private static string GetProcessInstanceName(int pid) { PerformanceCounterCategory cat = new PerformanceCounterCategory("Process"); string[] instances = cat.GetInstanceNames(); foreach (string instance in instances) { using (PerformanceCounter cnt = new PerformanceCounter("Process", "ID Process", instance, true)) { int val = (int) cnt.RawValue; if (val == pid) { return instance; } } } throw new Exception("Could not find performance counter " + "instance name for current process. This is truly strange ..."); } public void Dispose() { if (_cnt != null) _cnt.Dispose(); } } } |