从以上可以看到,接口实现里要做点儿啥,MathServicePerfCounter怎么用,你要想创建自己的计数器,比猫画虎就OK了,咱们开始创建一个Web服务,添加俩web方法
复制
保存[WebMethod]
public int Addition(int a, int b)
{
MathServicePerfCounter.Instance.RateOfAddition.Increment();
Random r = new Random();
Thread.Sleep(r.Next(500, 2000));
return a + b;
}
[WebMethod]
public int Multiplication(int a, int b)
{
MathServicePerfCounter.Instance.RateOfMultiplication.Increment();
Random r = new Random();
Thread.Sleep(r.Next(500, 2000));
return a * b;
}为了模拟真实环境,每个方法随机休眠几秒,然后右键点那个服务,在浏览器测试一下那个服务,看看能浏览不?然后在开始运行里输入perfmon,添加计数器里看看有没有咱们的MathServicePerfCounter计数器,反正我这里有。
对了,global.asax里加上如下代码,为了监控每秒请求和总请求量及并发请求量
复制
保存void Application_BeginRequest(object sender, EventArgs e)
{
MathServicePerfCounter.Instance.CountOfCurrentRequest.Increment();
MathServicePerfCounter.Instance.RateOfRequest.Increment();
MathServicePerfCounter.Instance.TotalOfRequest.Increment();
}
void Application_EndRequest(object sender, EventArgs e)
{
MathServicePerfCounter.Instance.CountOfCurrentRequest.Decrement();
}服务有了,咱得有单元测试呀,在这个Web服务上点右键,创建一个单元测试,vs会自动给你创建一个测试项目,项目的名字你可以自己指定,然后会自动给你添加一个Web引用,然后自动生成一个叫MathService.asmxTest.cs的测试类,里面给你生成两个测试方法,修改一下初始值及期望值,去除Assert.Inconclusive语句,最后如下。
复制
保存/// <summary>
///Addition (int, int) 的测试
///</summary>
[TestMethod()]
public void AdditionTest()
{
MathService target = new MathService(); // TODO: 使用 [AspNetDevelopmentServer] 和 TryUrlRedirection() 自动启动并绑定 Web 服务。
int a = 5; // TODO: 初始化为适当的值
int b = 6; // TODO: 初始化为适当的值
int expected = 11;
int actual;
actual = target.Addition(a, b);
Assert.AreEqual(expected, actual,
"MathServiceTest.localhost.MathService.Addition 未返回所需的值。");
}
/// <summary>
///Multiplication (int, int) 的测试
///</summary>
[TestMethod()]
public void MultiplicationTest()
{
MathService target = new MathService(); // TODO: 使用 [AspNetDevelopmentServer] 和 TryUrlRedirection() 自动启动并绑定 Web 服务。
int a = 5; // TODO: 初始化为适当的值
int b = 6; // TODO: 初始化为适当的值
int expected = 30;
int actual;
actual = target.Multiplication(a, b);
Assert.AreEqual(expected, actual,
"MathServiceTest.localhost.MathService.Multiplication 未返回所需的值。");
}
文章来源于领测软件测试网 https://www.ltesting.net/










