using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; namespace SyntacticSugar { public class PerformanceTest { private DateTime _beginTime; private DateTime _endTime; private ParamsModel _params; private List _CharSource = new List(); /// ///设置执行次数(默认:1) /// public void SetCount(int count) { _params.RunCount = count; } /// /// 设置线程模式(默认:false) /// /// true为多线程 public void SetIsMultithread(bool isMul) { _params.IsMultithread = isMul; } /// /// 构造函数 /// public PerformanceTest() { _params = new ParamsModel() { RunCount = 1 }; } /// /// 执行函数 /// /// public void Execute(Action action, Action rollBack, string name = null) { List arr = new List(); _beginTime = DateTime.Now; for (int i = 0; i < _params.RunCount; i++) { if (_params.IsMultithread) { var thread = new Thread(new System.Threading.ThreadStart(() => { action(i); })); thread.Start(); arr.Add(thread); } else { action(i); } } if (_params.IsMultithread) { foreach (Thread t in arr) { while (t.IsAlive) { Thread.Sleep(10); } } } _CharSource.Add(new PerformanceTestChartModel() { Name = name, Time = GetTime(), CPU = GetCurrentProcessSize() }); rollBack(string.Format("总共执行时间:{0}秒", GetTime())); } private double GetTime() { _endTime = DateTime.Now; double totalTime = ((_endTime - _beginTime).TotalMilliseconds / 1000.0); return totalTime; } public List GetChartSource() { return _CharSource; } private Double GetCurrentProcessSize() { Process processes = Process.GetCurrentProcess(); var processesSize = (Double)(processes.WorkingSet64); return processesSize / (1024 * 1024); } private class ParamsModel { public int RunCount { get; set; } public bool IsMultithread { get; set; } } public class PerformanceTestChartModel { public string Name { get; set; } public double Time { get; set; } public double CPU { get; set; } } } }