8,497
社区成员
发帖
与我相关
我的任务
分享
private class TestCaseType
{
public TestCaseDelegate proc; //测试程序
public string desc; //测试程序的标题(摘要说明)
}
private delegate void TestCaseDelegate();
private static IEnumerable<TestCaseType> GetTestCases(this Assembly[] assembly)
{
。。。。
}
private static void StressTest(this Assembly[] assembly, int 次数)
{
var tps = GetTestCases(assembly);
if (tps.Count() == 0)
{
System.Console.WriteLine("******** 没有测试用例 ********");
return;
}
var map = tps.AsParallel().SelectMany(tp => Clone(tp, 次数)).Select(tp => Test(tp));
var reduce = from t in map
group t by t.proc into g
orderby g.Average(p => p.mi) descending
select new { proc = g.Key, desc = g.First().desc, mi = (long)g.Average(p => p.mi) };
int testCnd = 0;
reduce.ForEach(r => { System.Console.WriteLine("{0} 平均{1}毫秒\t{2}", ++testCnd, r.mi, r.desc.Description); });
}
//将测试用例重复多次
private static IEnumerable<TestCaseType> Clone(TestCaseType t, int 倍数)
{
var c = new List<TestCaseType>();
for (var i = 0; i < 倍数; i++)
c.Add(new TestCaseType { desc = t.desc, proc = t.proc });
return c;
}
//单独执行一个测试用例并记录耗时。如果可能,Test应该在一个Master机辅助下分布到不同的Worker机上执行。
private static TestCaseTestType Test(TestCaseType p)
{
var wt = new Stopwatch();
wt.Start();
p.proc();
wt.Stop();
return new TestCaseTestType { proc = p.proc, desc = p.desc, mi = wt.ElapsedMilliseconds };
}
static public void ForEach<T>(this IEnumerable<T> array, Action<T> proc)
{
foreach (T obj in array)
proc(obj);
}