Riain McAtamney
Riain McAtamney

Reputation: 6432

Load testing a Webservice

I have been asked to load test an external webservice hosted by our client.

Basically to send multiple web requests to the service and time how long it takes for the service to process them all. To try and simulate multiple different users hitting the web service from different machines. The test will be to check that the webservice can handle this an to basically give the client some metrics and examples....e.g how long its takes 50 requests to respond.

I am aware that this could be achieved by using a tool like SoapUI/LoadUI.

What I want to know is there anyway I can write a load test using C#?

Currently I have something along the lines of the following:

    [Test]
    public void TestService50Requests()
    {
        List<WebRequest> requests = CreateMultipleRequests(50); //Creates web request objects

        string timeSpan = TimeMultipleRequests(requests);

        Console.WriteLine("50 Requests runtime: " + timeSpan);
    }

    private void TimeMultipleRequests(List<WebRequest> requests)
    {
       var stopWatch = new Stopwatch();
       stopWatch.Start();

       foreach (var request in requests)
       {
            request.GetResponse();
       } 

       stopWatch.Stop();
       var timeSpan = stopWatch.Elapsed;
       return String.Format("{0:00} hours, {1:00} minutes, {2:00} seconds, {3:00} milliseconds", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);           
    }

I assume there is a better way to do this? Any suggestions for improvements or is this completely wrong?

Upvotes: 2

Views: 5977

Answers (3)

fahad
fahad

Reputation: 389

I will explain how to create a Load test for a Web Service in visual studio Creating a Load Test Project First step of creating a load test Project. To-do so perform following steps Steps:

Open Visual Studio Instance

Create a New Project By clicking New > Project….

Click “Test” and then select “Web Performance and Load Test Project” with C#, Provide any Suitable name and click “OK”.

After Creating Project Creating a Performance Test Using Recorder Second Step is to create a performance test it can be created using a recorder or directly calling HTTP requests here we will record HTTP requests using recorder. Steps: To Add a Performance Test Left Click your project and open Add > Web Performance Test.

Note: A Web Test will be automatically added and you can also rename that if desired If recorder opens close it otherwise simply left click your test and click “Add Web Service Request” .

Change local host post method to user Web service address from properties at left pane

In Body Properties Select Content Type as Text /XML and in String Body provide your XML . E.g. Please note use SOAP 1.2 Request as we are using .net frame work 4.0 or 4.5 Click ok

Parameterize script As we know in load Test will simulate multiple Users therefore in some cases all user need a unique data set therefore it’s very important to Parameterize Request. To this following steps are required to be done
We require a data source to bind our parameter with a data in Excel or SQL Server Table. To do this Create a data source by click highlighted Icon.

If you want to fetch data for parameters from a CSV file select CS else select Data base.

In case of SQL Server create a connection with D.B by performing required steps and at last selecting table. Now open the web service body XML and for parameterizing a parameter type in its tag .Name of CSV file .Field Name}} e.g.

{{DataSource1.test#csv.ID}}

This way parameterize all required parameters of web service.

Add load test Now after creating a performance test you need to create a load test against it. To do so perform following steps. Steps: Left click project and open

A window will open click next

Name your scenario , Select Think time and click Next

Select Users count as per your requirement and test machine specifications.

Select Test Model

Add your Performance Test By clicking add which would be included in Load test

Select Your test

In this way configure your load test and continue

Executing load test. After Doing All configurations simply start Load test

When load test will start following graphs will be shown

Generating Report There are different ways of generating a load test report one is to automatically creating report using excel and second one is preparing report your self-having only required data what I normally require is response time and its graphs I simply make report like the one mentioned below in email form but it depends on need .

you can find a basic turtorial on this link

Upvotes: 0

Davin Tryon
Davin Tryon

Reputation: 67326

It really depends on what you are trying to test. Load testing can be used to stress (how many concurrent users it takes to break the system) or soak (medium load for an extended time period) the system. Your code does not send any concurrent requests to the system so it cannot be considered a good test that would mimic the real behavior of users.

There are good tools to accomplish these, but they can be costly. If you have Visual Studio Ultimate at your disposal, the load testing tools included are pretty good. You can also look at WAPT, which is a tool that will do a similar thing.

So, I would go back to the person that gave you the requirement and ask what specifically needs to be tested. Is it number of concurrent users the system can handle, no memory leaks, system availability, or performance?

Upvotes: 3

NikolaiDante
NikolaiDante

Reputation: 18649

To present another alternative to SoapUI - I would use JMeter rather than trying to do it in C#.

From the front page:

Apache JMeter features include:

  • Can load and performance test many different server types:

    • SOAP

Upvotes: 1

Related Questions