C # Interface Performance test-Calculate execution time

Source: Internet
Author: User

When you do a program, you will definitely encounter places that provide interfaces to others or use other people's interfaces. For a developer, whether it's an interface to others or an interface that you provide to others. will have to be tested.

For many tests, it is necessary to record the time range of the interface in detail, such as where the performance of the interface is 10ms 100ms.

Since we want to understand the performance of each interface, the test is not to test the correctness of the interface, but to record the execution time of the interface while the test is correct.

recently, because of the development of a new project, and the new project will use the old system interface. Therefore, it is necessary to test the interface provided by the old system, and then decide whether the interface needs to be reconstructed (optimized).

Now, let's talk about some of the techniques of interface performance testing.

1, create a new test program, write false data, call the interface to test.

2, run the old system directly, on all the interfaces to be tested for processing. That is: The time is recorded before the interface is called and the time is recorded after the interface.

Regardless of which method is used to test, can meet the task requirements.

Here is the test I implemented in C # in the first way.

Time TimeSpan startTime = new TimeSpan (DateTime.Now.Ticks) before the call interface is recorded;// Call the external interface QueryAllFaultBll.Instance.QueryAllFaultBll ();//Record the time timespan after the calling interface EndTime = new TimeSpan (DateTime.Now.Ticks) ; TimeSpan ts = endtime.subtract (startTime). Duration ();//Calculate the time interval to find out the time required to invoke the interface string spantime = ts. Hours.tostring () + "hours" + TS. Minutes.tostring () + "min" + TS. Seconds.tostring () + "Seconds" +ts.milliseconds.tostring ();//print Time Label1. Text = Spantime;


In the above way, the interface can be tested, but one thing is disgusting, is to test the interface with parameters, but also need to fake data.

To say, the pros and cons of both of these approaches:

The first way, the fake data is actually also a very troublesome is. Unless you know a lot about the business. If not, the fake data will be very disgusting.

In the second way, since it is the test interface, the method of running the source program is used for testing. To do this, you must find the upper-level method that invokes the interface. That is, the trigger event for the method is found. This is also a more troublesome thing.

two ways, each with its advantages and disadvantages. Or choose according to your own situation. Knowledge of the business, easy to fake data, you can choose the first. If you are a novice, be honest with the second method.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # Interface Performance test-Calculate execution time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.