3 使用示例
下面是后面部分示例中会用到的一段程序。
//被测试程序 private UInt16 _mediumFactor, _maxFactor; public UInt16 MediumFactor { get {return _mediumFactor;} set {_mediumFactor = value;} } public UInt16 MaxFactor { get {return _maxFactor;} set {_maxFactor = value;} }
public void FastMethod(Int32 NumberIterations) { int j = 0; for (int i = 0;i < NumberIterations;++i) { j += i; } }
public void MediumMethod(Int32 NumberIterations) { int j = 0; for (int i = 0;i < NumberIterations;++i) { for (int k = 0; k < _mediumFactor; ++k) j += k; } }
public void SlowMethod(Int32 NumberIterations) { int j = 0; for (int i = 0;i < NumberIterations;++i) { for (int k = 0; k < _maxFactor; ++k) j += k; } } |
□ 正向测试
首先来看C/S程序的正向测试,
public DotNetPerformance.StraightTest.TestResult[] RunSTest() { const int numberIterations = 50000000; DotNetPerformance.StraightTest.TestRunner tr = new DotNetPerformance.StraightTest.TestRunner(); tr.RunTests(); FastMethod(numberIterations); tr.PhaseEnd("Phase1"); MediumMethod(numberIterations); tr.PhaseEnd("Phase2"); SlowMethod(numberIterations); tr.End("最后一个阶段");
return tr.GetTestResult(); }
private void btnST_Click(object sender, System.EventArgs e) { DotNetPerformance.ResultOutput.Output.DisplayResults(RunSTest(), DotNetPerformance.ResultOutput.DisplayOption.Chart,null); } |
图 3-1 正向测试结果-图形输出
再来看B/S程序单个页面里的正向测试(这里用System.Threading.Thread.Sleep来模拟一段程序的执行),在结果输出时要传入页面对象用于注册脚本,从而显示输出测试结果。
m_Tester = new DotNetPerformance.StraightTest.TestRunner(); m_Tester.RunTests(); System.Threading.Thread.Sleep(1000); m_Tester.PhaseEnd("第一阶段"); System.Threading.Thread.Sleep(100); m_Tester.PhaseEnd("第二阶段"); System.Threading.Thread.Sleep(2000); m_Tester.PhaseEnd("第三阶段"); System.Threading.Thread.Sleep(50); m_Tester.PhaseEnd("第四阶段"); System.Threading.Thread.Sleep(50); m_Tester.End("第五阶段");
WebDisplayOption _wdo = UIHelper.ToWebDisplayOption(this.ddlOutputMode.SelectedValue); if(_wdo == WebDisplayOption.WebAlert) { DotNetPerformance.ResultOutput.Output.DisplayResults(m_Tester.GetTestResult(),_wdo,new object[]{this.Page}); } else if(_wdo == WebDisplayOption.WebTable) { DotNetPerformance.ResultOutput.Output.DisplayResults(m_Tester.GetTestResult(),_wdo,new object[]{"",this.Page}); } else if(_wdo == WebDisplayOption.WebXml) { DotNetPerformance.ResultOutput.Output.DisplayResults(m_Tester.GetTestResult(),_wdo,new object[]{"",this.Page}); } else { // } |
图 3-2正向测试结果-Web提示输出
对于B/S多个页面间的测试,需要用GlobalStraightTest。如果要显示测试进度还要传入一个URL(前面已解释)。
DotNetPerformance.StraightTest.GlobalStraightTest.WebContainerPage = this.Page; DotNetPerformance.StraightTest.GlobalStraightTest.RunTests("http://localhost/LPTDemo/WaitPage.aspx"); System.Threading.Thread.Sleep(1000); DotNetPerformance.StraightTest.GlobalStraightTest.PhaseEnd("第一个页面阶段1"); System.Threading.Thread.Sleep(100); DotNetPerformance.StraightTest.GlobalStraightTest.PhaseEnd("第一个页面阶段2"); |
图 3-3 正向全局测试-进度条1
下面转到第二个页面,
Response.Redirect("MidWebForm.aspx"); |
DotNetPerformance.StraightTest.GlobalStraightTest.WebContainerPage = this.Page; System.Threading.Thread.Sleep(50); DotNetPerformance.StraightTest.GlobalStraightTest.PhaseEnd("第二个页面测试结束"); Response.Redirect("EndWebForm.aspx"); |
图 3-4 正向全局测试-进度条2
第三个页面,
System.Threading.Thread.Sleep(150); DotNetPerformance.StraightTest.GlobalStraightTest.End("第三个页面测试结束");
WebDisplayOption _wdo = UIHelper.ToWebDisplayOption(this.ddlOutputMode.SelectedValue); if(_wdo == WebDisplayOption.WebAlert) { DotNetPerformance.ResultOutput.Output.DisplayResults( DotNetPerformance.StraightTest.GlobalStraightTest.GetTestResult(),_wdo,new object[]{this.Page}); } else if(_wdo == WebDisplayOption.WebTable) { ???? DotNetPerformance.ResultOutput.Output.DisplayResults( DotNetPerformance.StraightTest.GlobalStraightTest.GetTestResult(),_wdo,new object[]{"",this.Page}); } else if(_wdo == WebDisplayOption.WebXml) { DotNetPerformance.ResultOutput.Output.DisplayResults( DotNetPerformance.StraightTest.GlobalStraightTest.GetTestResult(),_wdo,new object[]{"",this.Page}); } else { // }
|
图 3-5 正向全局测试结果-XML输出
图 3-6 正向测试结果-Web表格输出
图 3-7 正向测试结果-Web图形输出