0
我想使用Rally .NET REST來測量API調用返回數據所花費的時間。 API是否有辦法輸出請求和響應數據,包括響應時間?如何獲取Rally WS API調用的響應數據
我想使用Rally .NET REST來測量API調用返回數據所花費的時間。 API是否有辦法輸出請求和響應數據,包括響應時間?如何獲取Rally WS API調用的響應數據
一個快速的方法來測量時間進行調用,然後調用後保存DateTime.Now,減去第二次的第一個值,並在打印之前保存DateTime.Now值:
DateTime startTime = DateTime.Now;
storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US123");
QueryResult queryStoryResults = restApi.Query(storyRequest);
Console.WriteLine(DateTime.Now.Subtract(startTime).ToString());
也可以打印請求和響應數據。當進行通話時,會收集通話數據,但您需要通過擴展System.Diagnostics.TraceListener
來監聽。
下面是完整的代碼示例,其中時間以小數秒形式返回:ss.fffffff。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rally.RestApi;
using Rally.RestApi.Response;
using System.Diagnostics;
namespace RESTexample_FindStories
{
class Program
{
class MyListener : TraceListener
{
public MyListener(): base(){}
public override void TraceEvent(TraceEventCache cache, String source, TraceEventType eventType, Int32 eventId, String msg)
{
TraceEvent(cache, source, eventType, eventId, msg, new Object[] { });
}
public override void TraceEvent(TraceEventCache cache, String source, TraceEventType eventType, Int32 eventId, String msg, Object[] msgParams)
{
writeToEventLog(msg, msgParams, eventType);
}
public override void Write(string message)
{
WriteLine(message);
}
public override void WriteLine(string message)
{
writeToEventLog(message, new Object[] { }, TraceEventType.Information);
}
private void writeToEventLog(String msg, Object[] msgParams, TraceEventType eventType)
{
System.Console.WriteLine(msg, msgParams);
}
}
static void Main(string[] args)
{
Trace.Listeners.Add(new MyListener());
RallyRestApi restApi;
restApi = new RallyRestApi("[email protected]", "Rally2013!", "https://rally1.rallydev.com", "v2.0");
String projectRef = "/project/2222";
Request storyRequest = new Request("hierarchicalrequirement");
storyRequest.Project = projectRef;
storyRequest.Fetch = new List<string>()
{
"FormattedID"
};
DateTime startTime = DateTime.Now;
storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US123");
QueryResult queryStoryResults = restApi.Query(storyRequest);
Console.WriteLine(DateTime.Now.Subtract(startTime).ToString());
foreach (var s in queryStoryResults.Results)
{
Console.WriteLine("FormattedID: " + s["FormattedID"]);
}
}
}
}