Tag: Parallel

In the last post we discussed how to work with Specflow 2.0 parallel execution, in this post we will discuss how to run Multiple tests in parallel in multiple browser same time using Specflow + NUnit + Selenium.

Since we will be using selenium webdriver object in many Specflow step definitions and while running tests in parallel, we will encounter NullReferenceException , in order to resolve the problem, we need to use two important techniques

Context injection (dependency injection)

IOC Container

IOC Container

In software engineering, inversion of control (IoC) is a design principle in which custom-written portions of a computer program receive the flow of control from a generic framework.

A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the framework that calls into the custom, or task-specific, code.

In the last post we discussed how selenium grid works and how to configure selenium grid in greater detail. In this post we will move further and try to run multiple tests in parallel in different environments like

Different Operating systems

Different browsers

Distributed architecture

The distributed architecture we will work will look something like this

Parallel execution

Parallel execution of test can be achieved using testing framework TestNG

In TestNG there is an option called Parallel which you can see from here Read more

In this blog post I am not going to jump and start talking about Task Parallel Library (TPL), before starting that I am going to talk a little bit about threading and how to perform multiple operations using Threading via Multithreading. Later we will discuss on TPL and their relation operations.

Threading

As we all know, thread is an independent process which has its own memory and they will perform certain operation (if they are assigned to do so).

In this whole blog post, we are going to discuss on executing some query with multiple thread, so that we execute them in parallel, rather sequential.

Here is the data executed (inserted) time for both with and without thread lock result in table

As you could see for code without Thread Lock, there happened as many as 14 operation in single millisecond, whereas in code with thread lock we happened to have sporadically 11 operations in single millisecond.

Parallel Execution

I am going to do the same stuff executing them in Parallel using Task Parallel Library (TPL), but here creating and managing thread is not done by me, rather .Net and CLR will take care of it. Here is the code

1

2

3

4

5

6

7

8

publicstaticvoidWithTPL(intThreadCount)

{

//Create Parallel.For to execute the task

Parallel.For(0,ThreadCount,i=&gt;

{

ConnectDBAndExecuteQuery(i);

});

}

As you can see in the code I am using Parallel.Forto execute the ConnectDBAndExecuteQuery() method, the for loop will be iterated for the threadcount which I am passing as parameter, but here I am not creating the thread rather CLR will do instead.

Here is the output, here I have added Thread.CurrentThread.ManagedThreadId to show the ManagedThreadID

As you can see the output, most of the threads have been reused here instead of one creating newly. Here mostly thread ID with 10,11,12,13 are the threads which executed the whole operation 20 times, now the database table has these many records as shown

AS you can see, the record inserted time frame is very close to the Thread without lock we saw previous in our discussion. But the important point to note here is, the whole operation is performed by JUST 4 THREADS.

Using Parallel Programming we don’t have to care about managing threads and reusing them, since everything is taken care by .Net for us.

Limiting Parallel Threads with ParallelOptions

We can also limit the parallel threads from 4 to 2 or we can increase the same from 2 to 10 using ParallelOptions class

Here is the self-explanatory code

1

2

ParallelOptions options=newParallelOptions();

options.MaxDegreeOfParallelism=2;

Now let’s change our code to have ParallelOptions in our Parallel.For method

1

2

3

4

5

6

7

8

9

10

publicstaticvoidWithTPLAndParallelOptions(intThreadCount)

{

ParallelOptions options=newParallelOptions();

options.MaxDegreeOfParallelism=2;

//Create Parallel.For to execute the task

Parallel.For(0,ThreadCount,options,i=&gt;

{

ConnectDBAndExecuteQuery(i);

});

}

Now the output looks like this

As you can see, only 2 threads are executing the method in parallel, which is 9 and 10.

I hope this post has given an idea on how to call multiple methods in parallel using TPL and Threads.