Sunday, November 30, 2014

Performance & Load testing - Part 2

Performance & Load testing a WCF service using Visual Studio (Part 2)

In the previous blog, I explained how to create a web test for your service and parametrize the request xml. Next step is to use this web test in our performance and load testing.

In the same project where your web test is present, right click and select load test. A load test wizard will open. 
















Click Next and enter the name of the scenario and select whether you want think times between execution or not.


Click Next and enter the load under wish you wish to test your endpoint. It could be constant load or increasing load















Click Next and select the model of your test. When I say model I mean, how you wish to run your test as shown below:-















Click Next and add your web test for execution.















Click Next and select the network type for you test. If you are not sure, leave it with LAN. 


Click next and select the browser you wish to run this test in. Since we are just testing our service endpoint. Browser really doesn't make any difference.















Click next and select the computer which you wish to monitor during test execution. The system will be monitored for CPU & memory etc















Click next and finally enter the load test duration and test iterations you wish for your performance test and click Finish















Now your load test is configured for execution and right click and click run load test. You should see screen as below with the current status of the test. I took the screen shot when it was finished so don't be afraid if it looks a bit different :)





















Once the test is finished, you will have all the stats with you clearly defining the response time, response at 90th percentile and 95th percentile etc.

Hope you enjoyed doing some performance testing :)

Saturday, November 29, 2014

Performance & Load testing - Part 1

Performance & Load testing a WCF service using Visual Studio (Part 1)

In this blog, we will be discussing how we can performance & load test WCF service using Visual Studio. 

Performance of any service is a critical factor and if a service is not responding within specified time then the response is as good as none. Hence it is absolutely imperative to not only make sure the service is responding but responding quickly.

There are couple of ways to performance test a service. Since we work on Visual Studio for development, that is the most handy and available tool for this. Below are the pre-requisites:-
  1. Visual Studio Ultimate
  2. One service endpoint to test
  3. Credentials if the service is secured via some security
So let's start, we will achieve this in two parts. First, we are going to write a web test which will call our endpoint. Second, we will use this test in our load test to add the desired load we want for our performance test.

1. Writing a web performance test:-

Open your WCF project in Visual Studio and right click on the solution and click add new project. On new project dialogue, click on test on the left hand side and select web performance and Load Test project. Please note, this option will only be available in Visual Studio Ultimate edition. 




Once the project is created, right click and add new web performance test. As soon as you click that, the browser will open for recording and just click Stop Recording. Now you have an empty web test. Right click on the webtest and click add Web Service Request. This will add a service call in your webtest with http://localhost/ url. Right click on the web service test and click properties and in the properties window, update the URL of the test with the one you wish to test.



Now the url is pointing to your service, it is time to add the payload. I will give you a tip here, if you are dealing with services, I would recommend to use SoapUI for testing them. It is a fantastic tool to add your service and it auto creates the request object for you as well which you can fill and test the service. 

I do have my service configured in SoapUI and I am going to use the same xml in request. If you are not using SoapUI then I would suggest to manually generate the xml request and add to the test. For this, right click on String body under web service test and click properties. Under String body property, add the request xml. Mine is shown below:-



Everything else in above xml is usual apart from Authorization header. My service is secured and the only way to successfully call it is either passing a basic Auth or SAML token in the request header. I am using Basic Auth above.

Now since the test is now loaded with request xml as well there is only one thing left to do. Specify the method of the endpoint. You might have noticed, we haven't mentioned which method of the endpoint to call. This information will go in header. Right click on the web service test and click add header. This will add a new header in the test.
Right click on the header and click properties. In Name add "SOAPAction" and in value add the fully qualified method name. 



This is important as without the SOAPAction header, your request will be lost. Now your test is ready to run. Right click on webtest1 and select Run Test. This will run you test and display the results.  

You can run this test as many number of times you want and it will give you the same results. Same results??? Yes, because currently our request is only invoked with the same request parameter. Check the username in the request screen shot above, "TestUser1". This is a hard coded value and will always be the same. Obviously when we wish to run our performance test, we would prefer to use different values in our requests so as to cover all testing scenarios and for this we would have to parametrize the request xml. 

Parametrization of web test:-

In order to parametrize a web test, you need to add a data source to it. Data source could be of three formats. Database, CSV file or an XML file. For our demonstration, I am going to use the simplest one. A CSV file.

First, let's create a file with our data. Create a CSV file on your desktop and in the first row, put the names of your parameters separated by comma and from the second row, the value of those parameters again separated by comma. Since in my request there is only one parameter, the content of my CSV file are given below:-

Usernames
TestUser1
TestUser2
TestUser3

Usernames is the name of the column and rest three are the values which my test will be using while running.

Now, let's add this CSV file as a data source in our web test. Right click on web test and click add data source. A dialogue will open showing three options of data source. Click on the CSV file and click next, now select the CSV file you just created. If the format of your CSV file is correct then the dialogue will display the contents of the CSV file. Press Finish.

The CSV file is now added in your web test as data source. All you have to do is to remove the hard coded value in the request and use it from data source. Below is the parametrized version of our request. 




Naming the parameter is simple. Name of your data source then name of your CSV file and name of the column to pick the value from. Now save the test and run again. You should see the results of the test using the new parametrized values.

This is it, we have successfully created a web test for our service and parametrized the request as well. In the next part, we will add this test in our load test configuration and will add load as per our requirements.

Stay tuned !!!

Thursday, November 13, 2014

Microsoft Decode 2014

Just sharing few of the really exciting announcements made in Microsoft Decode Tech day 2014 yesterday. But first, I can’t really resist without bragging how good the event was and some of the best sessions from few great people like Brian Cox etc. People missed who registered with me but didn’t attend. Their loss!
Right, anyways, back to the real world. It was announced in the morning itself that there will be few very important announcements from Microsoft in the evening so please don’t leave J  and turned out to be worth waiting for.  (Stretching a little bit right?)

Ok…so first announcement:-

 Microsoft has open-sourced the full server side .net core stack for developer community. This means, people can contribute to it now and the open source will be expanded to non-Microsoft operating systems as well like Linux & Mac. Cool right.. .net code running on Linux & Mac J

Second one, (this is going to please expense managers of projects as few less licenses)

A full fletched version of Visual Studio called Visual Studio Community 2013 has been launched for free. There is a huge difference between Visual Studio Express and this community version. It is basically a full version of Visual Studio with no restriction or a very few I may add. Well Microsoft never gives anything for free but the idea behind this move is they want to make Visual Studio as a gateway to other products and technologies of Microsoft. Good move but right now I am only looking at free version of Visual Studio J

Third one…..although this was bit of a surprise!

A preview version of Visual Studio 2015 and .Net has been launched. Well you might wonder the same thing I am doing which is what happened to Visual Studio 2014 beta which was released few months back. No idea…may be it is another thing like Windows 10 after 8 :D

And last but not the least…..

Visual Studio online Release management has been launched as a service (although currently in preview). Not sure how many of you are aware of this tool but this tool was launched by Microsoft after acquiring one company called InRelease. This is one of the products from Microsoft for managing your different release environments and automating the entire process with pre-defined workflows.
Although like any other release management tool, it wasn’t easy to configure. I was following the configuration manual and in the end realized that the current version of VSRM can’t connect to VSO but only TFS server. Disappointments!!!
But not for long, now this feature is available as a service which means no hassle, they will manage everything for you and you can only pay the subscription. That always reminds me of my postpaid connection J Easy
.
That was all for announcements. I also attended the DevOps technical sessions completely. (Before you think, let me clear this out that I am still into Development and has no plans to move into operations team but has attended this as this is the area we need to improve on for the client I am currently working for). Learned some great deal of information there and will try to share that soon as well. But as a tip, learn Powershell if you haven’t started yet J

That is for now….hopefully I will get some time soon to share more with you.





Sunday, August 17, 2014

Calling secured WCF from VBA

Using Azure ACS secured WCF in VBA


Another day and another weird requirement of using one of our secured WCF service from VBA. Now, I did write a lot of VBA code before but never a WCF service and that too which is secured.

Right, so the requirement is we have a service which is secured via Azure Access Control service and can only be called when detects a proper SWT token or BasicAuth credentials in request header. And we wish to use this service in excel.

There are couple of way by which you can call a WCF service in VBA but it is not straight forward and when you have to add secured header to your request, believe me it is not at all easy. So instead of testing the limits of VBA, I decided to keep my logic in something I know like C# and we know already that you can use C# Dlls in VBA.

Step 1: Create a C# Dll which has the logic to call the service and return the response.

In Visual Studio, Create a new class library project and name it as you want the name of your DLL.














You have to take care of below things in order to use this DLL properly in VBA:-
  1. If you have custom request and response objects for your service then expose those in this DLL so that they can be used in VBA
  2. WCF settings in configuration file
  3. Expose project for COM interop
Now, first one is easy, make your request/response as public so they can be used in VBA. The problem is second one. Your endpoint and behaviour configuration for a WCF usually goes either in Web.config or app.config and you can't have any of them in VBA. So, you would have to hard code the binding settings and endpoint in the class library itself unfortunately.

Below is what I did in order to call the service.

            request = new ProviderApi.SubmitInvoiceRequest() { Invoice = new Invoice() };

            var username = "userName";
            var password = "passWord";
            var headerBytes = Encoding.ASCII.GetBytes(username + ":" + password);
            var headerCredentials = "Basic " + Convert.ToBase64String(headerBytes);

            var header = new MessageHeader<string>(headerCredentials);
            var untyped = header.GetUntypedHeader("Authorization", "http://schemas.bupa.com/ukmu/security/claims/authorization");

            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security = new BasicHttpSecurity();
            binding.Security.Mode = BasicHttpSecurityMode.Transport;

            var invoiceClient = new ProviderApi.InvoiceServiceClient(binding, new EndpointAddress("https://providerapitest.bhwcloud.com/InvoiceService.svc"));
            using (var contextScope = new OperationContextScope(invoiceClient.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(untyped);

                var response = invoiceClient.SubmitInvoice(request);
                return response.InvoiceSubmissionReference;
            }

In short, green is where I created a header value and blue is hard coding the binding and endpoint in the client call itself. This takes away the need of having the configuration in web or app config file and when this code will be called from VBA, it will not give you behaviour or endpoint not found exception. 

And finally, changing the properties of your project to make it COM interop. Below are the two places where you have to do this. Right click on project and click properties and under build, check the box which says Register for Com interop as below:-

















and then click Application and the under Assembly Information, click check box Make Assembly Com visible as show below:-












And save the project and rebuild. Go to bin folder and you shall now see for different format of files there as show below:-








A C# DLL, one config file with same project name, one visual studio pdb file and the last one which we need to use is .tlb file. We are going to reference this .tlb file in VBA and use all the code we have written in the class library project.

Step 2: Use the .tlb in VBA and call our service

In VBA, reference the .tlb as show below:-













Click Tools -> References and in the dialog opened, browse and select the .tlb file. Make sure before pressing the OK button, your DLL is checked. 

Now you have the reference added, create the required objects like request object and the object of the class where you written the method and call the method with the request object. Mine is shown below:-

Sub Button3_Click()
 Dim iService As Bupa_ProviderApi_Caller.Bupa_ProviderApi_Caller_InvoiceService
 Set iService = New Bupa_ProviderApi_Caller_InvoiceService <Method class object>

 Dim request As Bupa_ProviderApi_Caller.SubmitInvoiceRequest 
 Set request = New Bupa_ProviderApi_Caller.SubmitInvoiceRequest <Request object>
 Set request.Invoice = New Bupa_ProviderApi_Caller.Invoice

 response = iService.CreateInvoice(request) <Calling the method with request>
 MsgBox (response)

End Sub

And there you go, got the response from the service in VBA without writing anything unique which I didn't know already. The only downside of this would be, you need separate DLLs for calling test and production environment as the binding and endpoint settings are hardcoded in DLL. If you find anyway to config out that value then do let me know as well. Have fun !!!

Friday, August 15, 2014

Automating CodedUI with Visual Studio, TFS and Selenium - Part 2

Selenium, CodedUI & Visual Studio – Part 2

Right, so in Part 1, we did the configuration for recording the Selenium web tests using FireFox Selenium extension and NUnits. In this part, we are going to record a web test without using the CodedUI functionality of Visual Studio. So let's start by recording a test first. I am going to take a simple example of web test by recording my log-in in Gmail and then run the test again to verify it is working.

Open FireFox, go to tools and click Selenium IDE. In Selenium IDE, click on Action on menu and make sure Record button is ticked. Now, follow the below steps:-

1. Navigate to Firefox address bar and type - https://gmail.com and press enter.
2. Gmail page will show up, click on Sign In link
3. Enter the username and password in the fields and click Sign In
4. On successful sign in, go back to Selenium IDE, click Actions and Click Record. Make sure, it is unchecked now.

Now, you have recorded your web test. In order to test this, sign out from Gmail but don't close the browser. Post sign out, go back to Selenium IDE and click on play button and that will run your sign-in process again. If you are happy with your test, we are ready to export this test and integrate with Visual Studio.

Click on File -> Export Test Case As as shown below:-
















Now, we are going to export this test in C# but we can either do it as WebDriver or Remote Control. WebDriver is basically now Selenium 2 or a newer and better version of Selenium since RC. There are couple of issues of annoying features about RC which are fixed in WebDriver. So export this test as C# NUnit WebDriver and save it on your machine.

We have a recorded test in C# and now we want this test to run in Visual Studio so that it can be integrated with our normal automated builds. Here how we are going to do it:-

1. Open Visual Studio, create any test project in C# and add unit test project to it. (Create unit test project and not codedUI project as we wish to demonstrate that this process doesn't require Premium version of VS.)
2. Right click on test project and Add ->Existing Item and select the exported file from Selenium
3. Rebuild the solution. You might end up with thousand errors as it is missing import DLLs and assemblies.
4. Now, let's get the required assemblies. Right click on the project and click "Manage Nuget" and search for Selenium Webdriver in search. Install the first search result.
5. And the final piece of the JigSaw, NUnit test adapter Nuget package.

Rebuild the project again and now you should have no errors. It is not full proof and depending on your recording, you might have to fix few more things but would just be resolving DLLs.

You have a working test now. Go to test explorer, right click on your test and click Run. Visual Studio will playback your test. Enjoy!!

Thursday, August 14, 2014

Automating CodedUI with Visual Studio, TFS and Selenium - Part 1

Selenium, CodedUI & Visual Studio – Part 1

CodedUI, a brilliant way to test the behaviour of your application on browser. It is not that dissimilar from unit tests in your application but they work at browser level and make sure that the buttons, content, links, text all are loading at the right place with the right user actions.

Now there are couple of ways by which you can record CodedUI tests for your application but we are majorly going to discuss two approaches only. First is Visual Studio. It gives you an option to record coded UI tests and integrates them with your automated CI and CD builds but is a paid functionality in Visual Studio which comes only with Premium edition. If you do not require any other features of VS premiums then buying them only for CodedUI will be a hit on the pocket.

The other option for CodedUI is the one which we are going to discuss in detail is Selenium and NUnit framework. So we are going to do this in two parts.  First, we will configure the machine with Selenium and in second, will record one test and export that to Visual Studio.

Part 1: Configuring Selenium:

So in order to record CodedUI via Selenium, you need Selenium IDE, Selenium server to run tests and IE Driver if you wish to record/run your tests in IE as well. Let’s start with the Selenium IDE first. Navigate to Selenium website and click downloads as shown below:-






















Download the latest IDE from the link and save it. Now, this IDE is basically an extension of Firefox and hence you have to add this extension in Firefox with below steps:-
  1. Open Firefox, Click Tools -> Add-Ons
  2. Click on Settings next to Search Add-Ons and Install Add-On from File
  3. Select the extension you have just downloaded and it will automatically install it.
Now, go to Tools and you should have Selenium IDE option visible there as shown below:-









Now, next step is only important if you wish to use Selenium RC for running your tests. If you wish to use the new Selenium WebDriver then you don't have to do this step.

Download the Selenium Server from the same Selenium HQ download page. This download is basically a jar file which runs as an standalone http server. In order to run this, you will need the Java JDK which can be downloaded from here. Once, this is installed, copy the jar file in some folder in C drive. Create a bat file in the same folder and copy the below command in it. 

java -jar NAME-OF-JAR-FILE -port 4444 -interactive

Run the bat file and your http server is up and running. Now the final piece is NUnit framework. Download the latest NUnit installer msi from here and install it. Although you might not need this if you are planning to run your tests from Visual Studio but you can surely test your recorded tests straight with this before exporting them to VS.

So this concludes the set-up for Selenium web tests on your machine. In all three components above, you can choose which one to install as per your requirements.

In Part 2, we will discuss how to record CodedUI in FireFox Selenium extension and export them into Visual Studio.

Friday, August 8, 2014

Extend your reach with .Net Extension methods

Got a legacy code to fix/enhance and worried of breaking something existing - Extension methods are your life savers


Recently heard of something like this but at first I actually thought of it more of another standard practice for maintainability and standard but when seen in action? ouuhhh...that will be cool !!

So extension methods enable you to add new functionality to existing types without compiling or modifying existing code. They are written as static methods but called as instance types. Sounds weird right but as complicated they sound, they are far easier to implement. So let's not smug more about it and see them in action. I will come back again on this for what can or cannot be done with Extension methods.

Consider I have an existing DLL which read and write your message on Azure Storage. The storage manager implements an interface IStorageManager which exposes two methods, GetMessage and PutMessage as shown below:-

//Your old code Interface
namespace ExtensionMethodInterface
{
    using System;
    public interface IStorageManager
    {
        void GetMessage();
        void PutMessage();
    }
}

Now there is no method exposed by the interface for Deleting a message from Storage Queue. You might not have the access to the source code of the DLL and may not wish to change it but you still need the delete functionality. You got the right ingredients for .net Extension methods.

So lets create a static class Extension and write our delete message functionality there as shown below:-

/Define an extension
namespace Extensions
{
    using System;
    using ExtensionMethodInterface;

    public static class Extension
    {
        //New extended method for deleting the message from storage
        public static void DeleteMessage(this IStorageManager legacyInterface, int messageId)
        {
            Console.WriteLine("Deleting the message using message id");
        }

        //New extended method for deleting the message from storage
        public static void DeleteMessage(this IStorageManager legacyInterface, string messageCode)
        {
            Console.WriteLine("Deleting the message using message code");
        }

        //This method will never get called becuase each class implementing IStorageManager will have its own implementation
        public static void GetMessage(this IStorageManager legacyInterface)
        {
            Console.WriteLine("Get message from storage");
        }

        //This method will never get called becuase each class implementing IStorageManager will have its own implementation
        public static void PutMessage(this IStorageManager legacyInterface)
        {
            Console.WriteLine("Put message from storage");
        }
    }
}

The above class expose another method DeleteMessage via either Id or Code. Pay attention to the signature of the method. The first argument is the interface type. Now we do have both Get and Put message implementation in this class but these methods will never be called as the original implementation of IStorageManager will have the implementation of these methods. Lets see the original implementation of IStorageManager as below:-

namespace InterfaceImplementation
{   
    using Extensions;
    using ExtensionMethodInterface;

    public class StorageManager : IStorageManager
    {
        //Must implement the GetMessage method
        public void GetMessage()
        {
            Console.WriteLine("Returns the message from Storage");
        }

        public void PutMessage()
        {
            Console.WriteLine("Writes the message on Storage");
        }
    }
}

Typical, implementing the Interface methods for read and write message on Storage Queue. No sign of DeleteMessage method as not exposed by the interface. Now, lets see the class in action:-

namespace ExtensionUsage
{  
    using Extensions;
    using InterfaceImplementation;

    class Usage
    {
        public void UseMyExtension()
        {
            StorageManager manager = new StorageManager();

            //This will be resolved from StorageManager class with the implementation
            manager.PutMessage();
            manager.GetMessage();

            manager.DeleteMessage(100);
            manager.DeleteMessage("code");
        }
    }
}

Now there are couple of things to notice above. We created an instance of the implementation class of IStorageManager interface. We already have the Get and Put message methods implementation in Storage manager class but DeleteMessage is neither present in IStorageManager interface and nor in its implementation in StorageManager. So this call will be resolved from the DeleteMessage method in Extension class.  Great right!!
You never changed your original interface and neither its implementation but still the call resolved from instance of interface implementation and that too as an instance method call and not like static. 

Now, its not exactly a down side but Extension method are never created to override the original code or functionality and hence the extension methods are never called if the have the same signature as the original method in the interface. Also, When using an extension method to extend a type whose source code you cannot change, you run the risk that a change in the implementation of the type will cause your extension method to break.

So choose wisely when you have to use Extension method but if you have to then they are pretty simple to implement. So have fun !!