How to implement real concurrency testing in white-box testing (Java)

Source: Internet
Author: User

Before we start this topic, let's figure out why we're doing concurrency testing.

General concurrency testing is the simulation of concurrent access to test whether multiple users concurrently access the same application, module, and data to create hidden concurrency problems, such as memory leaks, thread locks, resource contention issues.

At the point of view of the performance test, the concurrency test is not to obtain performance metrics, but to discover problems caused by concurrency.

So what is the implementation of the concurrent corresponding technology?

In short, concurrency means that multiple processes or threads process a specified operation at a time, a bit similar to the concept of a collection point in a performance test, with a particular emphasis on parallelism.

To get here, the next step is to discuss technology implementation:

Recently found in the project some developers do dynamic test simulation 500 concurrency, the implementation code is as follows:

Code Snippet 1:

 Public Static voidMain (string[] args) {//TODO auto-generated method stub         for(intI=0;i< -; i++) {NewMyThread (). run (); }    }Static  class MyThread implements Runnable{@Override Public voidRun () {//TODO auto-generated method stub            //Define a business logic implementation that is responsible for each thread}    }

Because of the use of some shared objects, to avoid the phenomenon of multi-threaded chaos, I propose to add a synchronous lock, then the developer rewritten the code, the following:

Code Snippet 2:

 Public Static voidMain (string[] args) {//TODO auto-generated method stub        //define thread pool, simulate 500 concurrent requestsThreadpoolexecutor executor =NewThreadpoolexecutor ( -, -, +, Timeunit.milliseconds,NewLinkedblockingqueue<runnable> ( -),NewThreadpoolexecutor.discardoldestpolicy ()); for(intI=0;i< -; i++) {Executor.execute (NewMyThread ());    } executor.shutdown (); }Static  class MyThread implements Runnable{@Override Public voidRun () {//TODO auto-generated method stub            //Define a business logic implementation that is responsible for each threadSynchronized ( This) {            }        }    }

In fact, the above-mentioned way in the CPU level is the FIFO strategy (first-out), the thread is to get the lock resources for processing, can not achieve the same time, so I decided to do the white box test, using the following 2 ways to achieve: Cyclicbarrier and Countdownlatch.

Code Snippet 3:

 Public Static voidMain (string[] args) {//TODO auto-generated method stubCountdownlatch CDL =NewCountdownlatch ( -); for(inti =0; I < -; i++) {NewMyThread (CDL). Run (); }Try{cdl.await ();//The thread that calls the await () method is suspended, and it waits until the count value is 0 to continue execution}Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); }    }Static  class MyThread implements Runnable {        PrivateCountdownlatch CDL; PublicMyThread (Countdownlatch CDL) { This. CDL = CDL; } @Override Public voidRun () {//TODO auto-generated method stubCdl.countdown ();//Subtract the Count value by 1            //Define a business logic implementation that is responsible for each thread}    }

Each of the above threads shares a counter, minus 1 calls the await () method to hang until count is reduced to 0 o'clock to continue execution together;

Code Snippet 4:

 Public Static voidMain (string[] args) {//TODO auto-generated method stubCyclicbarrier cb =NewCyclicbarrier ( -); Executorservice es = Executors.newfixedthreadpool ( -); for(inti =0; I < -; i++) {Es.execute (NewMyThread (CB));    } es.shutdown (); }Static  class MyThread implements Runnable {        PrivateCyclicbarrier CB; PublicMyThread (Cyclicbarrier CB) { This. cb = CB; } @Override Public voidRun () {//TODO auto-generated method stub            Try{//wait for all tasks to be readyCb.await ();//Define a business logic implementation that is responsible for each thread}Catch(Exception e)            {E.printstacktrace (); }        }    }

After all threads have been created, call the await () method to suspend until all threads have reached the barrier state and execute concurrently;

Disclaimer: Here, Cyclicbarrier barrier State is reusable, and Countdownlatch is not reusable, personal or recommended Cyclicbarrier, because the performance of the loss of less!

This is the sum of the day.

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

How to implement real concurrency testing in white-box testing (Java)

Related Article

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.