I am looking into JMeter and trying to understand the concepts. Especially confusing to me is the Threads vs Controllers.
I understand that a Thread represents a User and a Controller is a container for Samplers and determines their execution.
But what is the relation of a Thread and a Controller? Does a thread execute all the controllers that are children of the Thread Group?
So a thread (to simplify) calls each controller which in turn fires the samples?
But for example what is the difference between specifying a loop count of 20 in my Thread Group and using a While Controller to fire requests for 20 times?
Any help clear this out?
You understand well, a Thread represents a User and a Controller is a container for Samplers and determines their execution.
Yes it does execute or not (if controller is inside IfController) all controllers that are children.
Yes a thread (to simplify) calls each controller which in turn fires the samples.
But for example what is the difference between specifying a loop count of 20 in my Thread Group and using a While Controller to fire requests for 20 times?
It is very simular, many people add a WhileController while just setting loop count is enough.
You usually need a While controller if you want to repeat a set of samples inside main iteration.
Read JMeter Component Reference and Elements of a Test Plan
Read also Scoping Rules to understand how config elements apply.
Its simply like in Java. Thread means an execution thread, controllers are standing for control structures. So if you want to decide how much stress do you put on your tested application the main thing is the number of threads. Each thread executes its children in order. If you want to test the same thing more than once, for controller is your mate, if you want to do this parallel, you need threads (threadgroup with a given number of threads).
One main difference, which you need to understand, comes in picture when you want to parametrize your test.
So for example you have a web application and you have 100 demo users. Each user can log in only once. This case you need something, that tells to your test threads, which user should they use. This is usually a CVS Data Set, which can be used in a way, that threads can fetch one row for themselves, and they use that. You can say as well, that when there is no more row in the CSV, your test should stop. The same concept (giving one data for each iteration in a for loop) is harder to implement with a for loop, or other controller.
Related
I have a custom controller type which runs it's own specific test fragments. The important thing to note is that these fragments contain Transaction Controllers, which contain gaussian timers simulating wait times of up to 5 minutes.
The tests I am running are data driven, and should be runnable for a varying length of time. To specify the runtime of a test I have been using the "Duration" option on the Thread Group scheduler.
In the event were a test has ran beyond its duration, I've noticed that when these timer fragments are in use, the test is delayed and cannot end until the transaction (or at least the timer) has been complete. The other timings and samplers recorded seem to be unaffected, however the runtime of the test is impacted.
I'd like to solve this issue without having to rely on the user manually killing a test when it has reached it's duration. Is there any option within JMeter to kill or interrupt any type of running thread when a duration has been reached?
As per my understanding with Jmeter, there is no element which can stop the running test on reaching specific duration.
However, an element named 'Test Action' can be used to Pause/Stop/Stop Now actions on your test during run time and this element can be used under 'If Controller' element so that you can set the condition in this element to stop the thread.
Although JMeter provides various head-on elements to handle different conditions but in rare cases where existing elements could not provide direct solution to the problem then JMeter experts in any software testing company uses multiple elements with child-parent hierarchy to handle the condition [as used above with Test Action & If Controller elements]
I believe this has to do with stop test vs shutdown. When a test reaches its duration, it will issue a stop test, at which point any timer will finish, the request will happen, then the thread stops. This is why manually shutting it down works- shutdown doesn't respect timers, etc.
I don't think there's a way to set duration to use shutdown rather than stop. One thing you might try is multiple, smaller timers, and see if it still waits for all of them.
Suppose I need to execute N tasks in the same thread. The tasks may sometimes need some values from an external storage. I have no idea in advance which task may need such a value and when. It is much faster to fetch M values in one go rather than the same M values in M queries to the external storage.
Note that I cannot expect cooperation from tasks themselves, they can be concidered as nothing more than java.lang.Runnable objects.
Now, the ideal procedure, as I see it, would look like
Execute all tasks in a loop. If a task requests an external value, remember this, suspend the task and switch to the next one.
Fetch the values requested at the previous step, all at once.
Remove all completed task (suspended ones don't count as completed).
If there are still tasks left, go to step 1, but instead of executing a task, continue its execution from the suspended state.
As far as I see, the only way to "suspend" and "resume" something would be to remove its related frames from JVM stack, store them somewhere, and later push them back onto the stack and let JVM continue.
Is there any standard (not involving hacking at lower level than JVM bytecode) way to do this?
Or can you maybe suggest another possible way to achieve this (other than starting N threads or making tasks cooperate in some way)?
It's possible using something like quasar that does stack-slicing via an agent. Some degree of cooperation from the tasks is helpful, but it is possible to use AOP to insert suspension points from outside.
(IMO it's better to be explicit about what's going on (using e.g. Future and ForkJoinPool). If some plain code runs on one thread for a while and is then "magically" suspended and jumps to another thread, this can be very confusing to debug or reason about. With modern languages and libraries the overhead of being explicit about the asynchronicity boundaries should not be overwhelming. If your tasks are written in terms of generic types then it's fairly easy to pass-through something like scalaz Future. But that wouldn't meet your requirements as given).
As mentioned, Quasar does exactly that (it usually schedules N fibers on M threads, but you can set M to 1), using bytecode transformations. It even gives each task (AKA "fiber") its own stack trace, so you can dump it and get a complete stack trace without any interference from any other task sharing the thread.
Well you could try this
you need
A mechanism to save the current state of the task because when the task returns its frame would be popped from the call stack. Based on the return value or something like that you can determine weather it completed or not since you would need to re-execute it from the point where it left thus u need to preserve the state information.
Create a Request Data structure for each task. When ever a task wants to request something it logs it there , The data structure should support all the possible request a task can make.
Store these DS in a Map. At the end of the loop you can query this DS to determine the kind of resource required by each task.
get the resource put it in the DS . Start the task from the state when it returned.
The task queries the DS gets the resource.
The task should use this DS when ever it wants to use an external resource.
you would need to design the method in which resource is requested with special consideration since when you will re-execute the task again you would need to call this method yourself so that the task can execute from where it left.
*DS -> Data Structure
hope it helps.
I designed a java application. A friend suggested using multi-threading, he claims that running my application as several threads will decrease the run time significantly.
In my main class, I carry several operations that are out of our scope to fill global static variables and hash maps to be used across the whole life time of the process. Then I run the core of the application on the entries of an array list.
for(int customerID : customers){
ConsumerPrinter consumerPrinter = new ConsumerPrinter();
consumerPrinter.runPE(docsPath,outputPath,customerID);
System.out.println("Customer with CustomerID:"+customerID+" Done");
}
for each iteration of this loop XMLs of the given customer is fetched from the machine, parsed and calculations are taken on the parsed data. Later, processed results are written in a text file (Fetched and written data can reach up to several Giga bytes at most and 50 MBs on average). More than one iteration can write on the same file.
Should I make this piece of code multi-threaded so each group of customers are taken in an independent thread?
How can I know the most optimal number of threads to run?
What are the best practices to take into consideration when implementing multi-threading?
Should I make this piece of code multi-threaded so each group of customers are taken
in an independent thread?
Yes multi-threading will save your processing time. While iterating on your list you can spawn new thread each iteration and do customer processing in it. But you need to do proper synchronization meaning if two customers processing requires operation on same resource you must synchronize that operation to avoid possible race condition or memory inconsistency issues.
How can I know the most optimal number of threads to run?
You cannot really without actually analyzing the processing time for n customers with different number of threads. It will depend on number of cores your processor has, and what is the actually processing that is taking place for each customer.
What are the best practices to take into consideration when implementing multi-threading?
First and foremost criteria is you must have multiple cores and your OS must support multi-threading. Almost every system does that in present times but is a good criteria to look into. Secondly you must analyze all the possible scenarios that may led to race condition. All the resource that you know will be shared among multiple threads must be thread-safe. Also you must also look out for possible chances of memory inconsistency issues(declare your variable as volatile). Finally there are something that you cannot predict or analyze until you actually run test cases like deadlocks(Need to analyze Thread dump) or memory leaks(Need to analyze Heap dump).
The idea of multi thread is to make some heavy process into another, lets say..., "block of memory".
Any UI updates have to be done on the main/default thread, like print messenges or inflate a view for example. You can ask the app to draw a bitmap, donwload images from the internet or a heavy validation/loop block to run them on a separate thread, imagine that you are creating a second short life app to handle those tasks for you.
Remember, you can ask the app to download/draw a image on another thread, but you have to print this image on the screen on the main thread.
This is common used to load a large bitmap on a separated thread, make math calculations to resize this large image and then, on the main thread, inflate/print/paint/show the smaller version of that image to te user.
In your case, I don't know how heavy runPE() method is, I don't know what it does, you could try to create another thread for him, but the rest should be on the main thread, it is the main process of your UI.
You could optmize your loop by placing the "ConsumerPrinter consumerPrinter = new ConsumerPrinter();" before the "for(...)", since it does not change dinamically, you can remove it inside the loop to avoid the creating of the same object each time the loop restarts : )
While straight java multi-threading can be used (java.util.concurrent) as other answers have discussed, consider also alternate programming approaches to multi-threading, such as the actor model. The actor model still uses threads underneath, but much complexity is handled by the actor framework rather than directly by you the programmer. In addition, there is less (or no) need to reason about synchronizing on shared state between threads because of the way programs using the actor model are created.
See Which Actor model library/framework for Java? for a discussion of popular actor model libraries.
I'm making a little java game in which I would have two threads (well as the FIRST step towards multithreading...), one for the logic and one for the drawing.
So my question is: How can I make those two communicating which each other?
Requirements:
accessing variables and object from a another thread
syncing them so they each complete a same number of "loops" in the same time.
(the logic calculates and then the another one draws the results and the loop begins again...)
So how is this achievable in java?
Thanks in advance!
1. Create a Class with logic and drawing methods.
Whose object is accessible by both the threads.
2. Now please do synchronize the atomic statements or methods.
3. So its like an object is shared between 2 threads.
Methods are methods, within a thread or not. Just create an object that is visible to all of your Threads, and they'll both be able to access it.
One easy structure to use to communicate between threads is the BlockingQueue.
I often find if you use a BlockingQueue it will focus you on making the threads work together correctly. For example, they will not provide the facilities you are asking for because actually those facilities are not what you want.
I have a component that I wish to write and it's the kind of thing that feels like a common pattern. I was hoping to find the common name for the pattern if there is one, and examples of how to go about implementing it.
I have a service that queues requests and processes them one at a time. I have a number of client threads which make the requests. The key is that the calling threads must block until their own particular request is serviced.
E.g. if there are 10 threads, all making a request, then the 10th thread will block for longest while it waits for its request to make it to the front of the queue, and to be processed. In brief pseodocode, a call would be as simple as:
service.processMessage(myMessage); /* block whilst it enqueues, waits, */
/* processes and returns */
I know what you're thinking - why bother having threads at all? Let's just say there are design constraints well outside my control.
Also, this should run on JavaME, which means an infuriating subset of real Java, and no swanky external libraries.
If you do not have any requirements on the total ordering of handling requests (i.e., you don't mind arbitrarily mixing requests from different threads independent of the order they "arrive" in), you could simply make processMessage() synchronized, I guess.