0

I would like to measure time it takes an API call return data using Rally .NET REST. Does API have a way to output request and response data, including the response time?

4

1 回答 1

0

A quick way to measure time is to save DateTime.Now value before making the call, and then saving a DateTime.Now after the call, subtracting the first value from the second and printing it:

    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());

It is also possible to print out request and response data. When a call is made, a call data is collected, but you need a listener to catch it by extending System.Diagnostics.TraceListener.

Here is a full code example where the time is returned in fractional seconds: 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("nmusaelian@rallydev.com", "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"]);

            }

        }
    }
}
于 2013-08-28T20:43:52.323 回答