jMeter thread sequence - java

I have a JMeter test plan with following http request samplers.
Login
Call some functionality which needs a logged in user
Logout
When I execute the test plan with 5 parallel threads, I see that the sampler 2 is called before calling sampler 1 for some threads, which then fails the response assertions.
Is there any way to specify a sequence of samplers to be executed ?

This should ensure that they are executed sequentially :
So let's start with thread group.
Number of Threads(users) is 5.
So assuming you have the logic work out for your login sampler. Just add additional sampler to it. So right click on that sample Add > Post Processors > BSF PostProcessor, inside this post processor big script space write ${__setProperty(ThreadValue,${__threadNum},)}.
This will save the thread number to your property called ThreadValue. Make sure you select your language as beanshell in the dropdown list.
Then after the login sampler add the if controller. Add this to the condition field (${JMeterThread.last_sample_ok}==true) && (${__property(ThreadValue,,)} == ${__threadNum})
What this means is that -> do only logged in stuff while the actual login is successful and if the login thread matches the thread you're currently in.
That's it you do your login stuff only inside the if controller. If you want to be sure that you logout the right user place additional if controller arround it.
Cheers

What you need to consider is that each thread is a separate entity, like a real user, and that it has it's own session, but that JMeter is designed to execute these threads asynchronously, ie. in parallel. The View Results Tree listener displays all activity, from all threads as it happens regardless of the logical thread sequence. If you would rather have 1 thread make 5 iterations then simply change the thread count to 1 and use a loop of 5 and this will preserve the sequence (although this defeats the point of using a load testing tool like JMeter!).
If you do change the thread count to 1 then you can see the true, logical sequence of execution in the results tree. This will show each sampler being executed, in sequence, top down.
Now, in your case, I suspect you have a problem not because things are being run out of sequence but rather because the server is losing the session context between requests. This is very common and more often than not you can address the problem using a HTTP Cookie Manager or with the use of a Regular Expression Extractor.

Related

JMeter run LoginThread then all threads concurrently

I need to run one thread group first (login), then after the login is ok, run all other thread groups concurrently.
How do I configure JMeter to do this?
Use setUp Thread Group to perform your "Login" operation. The setUp Thread Group is being executed before other Thread Groups so you can use it for some pre-requisites operations.
To check whether login is successful or not you can use If Controller like:
Add If Controller after the Login request
Use !${JMeterThread.last_sample_ok} as the condition. This is a pre-defined JMeter Variable holding previous sampler execution result
Add Test Action Sampler as a child of the If Controller and configure it to Stop Test

Multithreading operation in JSP

I am trying to build something, which requires multithreading functionality. The desired work was not done using javascript i.e. Web-Workers.
So I changed focus from javascript to JSP. Now I want to call one method which will execute series of some queries, and at the same time I want to show the affected table rows on other hand. And when first process done with it's execution, I want to stop other process also. My work is done but statically. Now I want to share some resource between this two threads. So that when first thread done with it's execution, I will set some value to that resource(variable,flag), and check that resource in another thread. Is it possible to access variable of one thread in another while it running.
Thanks
JavaScript in a browser is per design not capable of multithreading. You can simulate it a little bit when using setTimeout or setInterval methods.
But, as with the introduction of HTML5, there are now so called WebWorkers available. They run completely separate, spawn a real OS thread, do not have access the DOM but can interact with your UI application e.g. via events.

Stopping a previous search without using multithreading Java?

I have a webpage that allows the user to enter a search criteria. Upon submission of the search form using the get method, a controller class reads in the search parameters, sets them as request attributes, then directs back to the page, which then calls a java class that has database connections and sql queries. The question I have is this: when the user decides to perform a new search while the current search is still not finished, is there a way to terminate the current search on the server before starting the new one.
You really didn't gave many information here. Especially you didn't write how do you connects to database. If you are using JDBC, you can cancel executing query with Statement.cancel(). Also you should hold information about current running query somewhere in session.
You need to interrupt() the thread that is doing the search and -admitted that the search Thread is doing some Thread.sleep() in between of performing the operations (eventually you should add those) - the thread will be interrupted by an InterruptedException that you should handle to break the search processing.
Still, you need to save the Thread.currentThread() somewhere (i.e. in the session) in order to retrieve a reference by the second one.
It's not trivial, and it's not very clean; since you need to put some interruption points in your search logic (i.e. the Thread.sleep() I mentioned before) and handle the InterruptedException properly.
For sure, the easier way is to ignore that the previous thread is still running and perform a new search.

applicability of mutli threading to a specific scenario in a java program

I am confused about the applicability of multi threading in general...
I am creating an application which executes some code which has been saved in xml format. The work is to use apache http client and retrieve some data from websites...More than 1 website can be visited by one block of code in xml...
Now I want that if 2 users have created their own respective codes and saved them in XML, then each user's 'job' (ie block of code in xml format) runs in a separate thread.
I have with me code to execute one user's code...Now I want that multiple persons' code can be run in parallel. But I have some doubts--
(1) The Apache HTTP client provides a way of multithreaded communication, currently I am simply using the default HTTP client- this same client can be made to visit multiple websites, one after the other- as per code block in xml. Am I correct in thinking that I do not need to change my code so that it uses the recommended multithreaded communication?
(2) I am thinking of creating a servlet that when invoked, executes one block of xml code. So to execute 2 blocks of code as given by 2 different users, I will have to invoke this servlet twice. I am going to deploy this application using Amazon Elastic Beanstalk, so what I am confused about is, do I need to use multi threading at all in my program? Can I not simply invoke the existing code (which is used to execute one block of code at a time) from the servlet? And I do want to keep processing of the different blocks of XML code separate from each other, so I dont think I should use multi threading here.. Am I correct in my assumption?
Running it one after the other as per your 1st option will not be considered 'concurrent' .
Coming to the servlet method , the way you describe it will work concurrently , but you also need to think about how many users concurrently ? Since for each user , there would be a separate request , there would be some network latency involved for multiple calls. You need to think about all these factors before going ahead with this option
Since you have the code for one user's job , you can define a thread class which has userid as an attribute. In the run() method call the code for a particular user's job.
Now create two threads and set the appropriate userid for each thread and spawn them off.
If the number of users are more , you can look at using Java's Thread Pool Executor .
Since you are going to use a servlet container then it's going to manage multithreading for you. Every servlet request will be executed in a different thread. In that scenario one servlet call would execute on block of code from provided XML in a single threaded manner. If there are several sites declared per block of code they would be visited serially. Other user in the same time may call the same server with other block of code running in parallel with the first one.

Controlling Java and Lotusscript agents

I created 2 agents, one made of Java and another made of Lotusscript. The java agent is scheduled to run every 5 minutes, while the lotusscript agent is scheduled to run every 15 minutes. Therefor there will come a time that they will simultaneously run. When that happens, the java agent must pause/wait until the lotusscript agent finished. I tried to simulate locking using Profile DOcuments and Environment Variables but to no avail. Is there a way that I can simulate locking between this two different agents? Please help. Thanks a lot!
Edit: I forgot to say that the 2 agents resides in TWO DIFFERENT databases, to complicate things more :(
Why not writing a third Agent (maybe in an extra Database), which runs periodically every five Minutes, which starts the other two Agents:
The Lotus Script Agent every time
The Java Agent every third run
... then you are also in control of the run order, without any complicated lock mechanisms.
This is a near foolproof way I have found that works for controlling the execution order of independent agents. I use a real notes document as a psuedo-lock document.
The way I have done this before is to keep a Notes document that represents a "lock". Don't use a database profile document as it's prone to replication/save conflict issues and you can't view it in a view.
The "lock" document can have a flag on it which tells the java agent whether it is allowed to run now. The java agent simply has code in it similar to this
Session s = NotesFactory.createSession();
Database db = s.getDatabase("This Server", "This database");
View vw = db.getView("(lockView)");
Document docControl = vw.getFirstDocument();
String sRunStatus = docControl.getItemValueString("runStatus");
boolean bContinue = false;
if (sRunStatus =="Go"){
bContinue = true;
}
if(bContinue){
//do agent code here....
// reset the status to "wait". The lotusscript agent should then set it to "Go"
// the other agent will execute on "wait" and then update the status to "Go" on
// completion to prevent simulatenous execution. Can also use different state names
// instead of go/wait, like run0, run1, run2 etc
docControl.replaceItemValue("runStatus", "wait");
docControl.save(true);
}
Note that you use the agents to set "Go"/"wait" values in the "runStatus" field on a control document. You only need 1 document so you then only need to get the first document out of the view.
The equivalent logic should be even simpler to add in the LotusScript agent as well. The only downside I can find is that the java agent may not execute the code because the control document is not yet set to "go" and the "IF" test fails without running the logic, so it's not a pause as such, but prevents the Java agent from executing out of it's desired order with the lotusscript agent. But it would then fire on the next scheduled instance if the LotusScript agent has released it.
You can also extend this idea to manage a suite of agents and even chain multiple agents as well by using specific values like "RunAgent1", "RunAgent2", another benefit is that you can also capture execution start times, errors as well, or anything you require....
Enabling document locking in the database could work. If you can enable document locking in the database itself you can have the agents lock a specific document and check if the document is locked before/during it runs the code.
If enabling document locking in that database is not an option you can consider creating a separate database do store the document.
Why can't these agents run simultaneously? Maybe it is possible to achieve the same result while allowing the agents to run simultaneously. Trying to control agents this way will usually lead to other problems. If the database has replicas the solution might break.
You said that it is two databases, but really by far the simplest way to stop agents from running simultaneously is to put them in the same database. I will very often create a special database that only contains agents and log documents generated by the agents. The agents can open any database, so it really doesn't matter where they are.
I also led a project once in which we built our own control mechanism for agents which was a combination of giulio and spookycoder's ideas. Only one 'master' agent was scheduled, and it read the control document to decide which agent should run next. Let's say we have agents A, B and C. The master runs A, which immediately updates the control document to say "I am running", then it updates fields with its progress information as it goes along, and finally when it is done it updates the control document with either "B",The next time the master runs, it looks at the control document. If the progress information shows that A has finished, the master will see that it is B's turn to run. Of course, A might realize that B has no work to do, so it might have written "C" instead, in which case the master will run C. The master also has the option to re-run A if the progress information shows that it did not finish the job.

Categories