I need a loop to repeat at a specific time
For example 10 seconds :
while (Repeat every ten seconds)
{
// Code body
}
To pause the code execution into a java loop you can use Thread.sleep(Integer) method, that will stop the execution of the thread for the specified amount of milliseconds. Here there is an official example with a for loop. In your case you could do something like this:
while (true) {
/* your code here */
Thread.sleep(10000);
}
The body of the while loop will be executed once every 10 seconds.
Strictly speaking, you cannot do that. You can make a loop that does something like
while(true){
... do your sheet ...
Thread.sleep(10_000);
}
But it will not be precisely every 10 seconds.
This solution will likely be okay for you, though.
What you have to consider is the followings:
What happens if the do your sheet runs for 9 sec? Should you wait 10 sec more or only the remaining 1 sec?
What happens if the action takes more than 10sec? Should you repeat it immediately or 10 sec after the start of the action? Should the program start the action again in a different thread? How many threads should the program start?
Also, there is no guarantee that the Thread.sleep(10_000) will suspend the execution of the current thread for exactly 10 seconds. It may get back from sleep sooner, because of an interrupt/signal. Also the thread may get back from sleep later because there is no free resource to execute the thread right after the 10 sec have passed.
For a simple exercise the while loop and the sleep() are okay. If you later will need something more professional then you can read the documentation, and consider the use of Quartz.
Related
Idea is to loop requests independently, the thread will run for 20 minutes. One Sample will sample every 5 seconds. Other Samples in Simple controller will loop one by one with 10 second delay.
I don't think you will be able to run Samplers with different throughput within the bounds of one Thread Group because JMeter will always wait for the previous Sampler to complete before starting the next one.
The options are in:
Either move /api-common/rest/rmslistterms to a separate Thread Group
Or add the If Controller as a parent for the /api-common/rest/rmslistterms and configure it to run every 2nd iteration, example __jexl3() function you can use would be something like:
${__jexl3(${__jm__Loop Controller__idx} % 2 == 0,)}
Hi everyone in my Test i have this lineup:
jmeter set up
In the thread group the duration is set to 300 sec.
My goal is to make the first 3 requests in a time X sec (about 10), make those in the loop count for Y sec (180 sec, with loop count equal to 1000) and the last two for Z sec (about 10).
another question is: if the lifetime thread ends before the whole loop controller is executed, I want the last two requests to act as teardowns. How can I set them?
Can anyone give me some advice on how to act?
My goal is to make the first 3 requests in a time X sec (about 10)
and
last two for Z sec (about 10).
this is achievable by Timers or Flow Control Action sampler
make those in the loop count for Y sec (180 sec, with loop count equal to 1000)
Put "those" under the Loop Controller, mention the start time via __time() function and put an If Controller somewhere inside the Loop Controller to compare current time with the start time, if it exceeds 180 seconds - use the aforementioned Flow Control Action sampler to exit the loop
With regards to your "another question" I think you should rather start a new thread, it's quite hard to answer several "another" questions and it makes the process of searching for the answer for other people harder.
Whatever.
Currently I would recommend to use:
setUp Thread Group for preparing the test data or the system for the load test
tearDown Thread Group for cleaning up
eventually you can use __setProperty() and __P() functions combination or Inter-Thread Communication Plugin to pass the data between thread groups
If you need to implement the tearDown within the bounds of a single Thread Group - either go for the above approach with Loop Controller + If Controller + __time() function or go for Runtime Controller
I'm using spring-boot #Scheduled annotation with fixedDelay in milliseconds as documented in javadoc:
Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.
Code:
#Scheduled(fixedDelay=1000)
public void task() {
LOG.info("START: " + System.currentTimeInMillis());
....do some work here...
LOG.info("END: " + System.currentTimeInMillis());
}
And sometimes I get such output that time between previous task end and next task starts is less than 1000ms for about 2-30 milliseconds.
Is it normal due to some granularity or why is it happening? Is there any guaranties about this delta value?
There are different ways in which you can use #Scheduled annotation.
According to the documentation:
The fixedRate invokes the method every t ms but the time delay is measured from the start of the invocation. If t ms are passed and the method is still in execution then the next invocation will wait for it to finish and will invoke right after the first one. Try putting Thread.sleep(3000) in your method. I think that your method is taking about 950ms to complete.
If you want to wait after finishing the execution you can use fixedDelay.
Obviously it cannot be guaranteed since you're most likely not on a real time system. Depending on what the CPU(s) do at the moment it can vary like that. It's quite hard to do something like that actually on most PC's due to the OS scheduling calls and so on (unless you have direct access to the CPU/GPU but even then)
How #Scheduled(fixedDelay=1000) works is, it will run this void method every 1000 ms(If this task finish execution < 1000 ms or this will run asynchronously). If > 1000ms it the execution task will get into a task queue in the Executor service used. There is no connection with the end of task and the start of next task but a connection with the start of a task and start of a next task.
Right now I have two threads running in my program. One constantly tries to read input from the user, and the other watches for a timeout. A timeout occurs if the user does not send any input in a given amount of time. The two threads look like this:
User input thread
while(true){
if(in.hasNextLine()){
processLine(in.nextLine());
timeLastRecieved = System.currentTimeMillis();
}
}
Timeout thread
while(true){
//Check for a timout
if(timeLastRecieved+timeoutDuration <= System.currentTimeMillis())
timeUserOut();
else{
//Sleep until it is possible for a timeout to occur
Thread.sleep((timeLastSent+timeoutDuration) - System.currentTimeMillis());
}
}
As of now I have these thread separated, but I could combine them like this...
while(true){
if(in.hasNextLine()){
processLine(in.nextLine());
timeLastRecieved = System.currentTimeMillis();
}
//Check for a timout
if(timeLastRecieved+timeoutDuration <= System.currentTimeMillis())
timeUserOut();
}
But I really don't need to check for a timeout that frequently. So should I combine the threads and check for a timeout too often, or should I have two threads. I am not as worried about performance as I am proper coding etiquette. If it means anything the timeout duration in something like 15 minutes long.
EDIT: Just want to point out that in the version with two thread I am sleeping, but in the combined version I never sleep the thread. This obviously causes the if statement that checks for a timeout to run more then necessary.
To summarize my comments: I don't think a separate thread to check for timeouts is necessary.
Reasons:
You'd need to share information like timeLastRecieved between them, which could be more complex than wanted (e.g. AFAIK in some cases access to long values is not atomic).
From your description it seems that polling for user input and timeout (no input provided in time) are closely related, thus the polling thread could check for the timeout as well. That doesn't mean it has to handle the timeout too, just reporting it somewhere or calling some timeout handler might be better design.
It is easier to read and understand since updating timeLastRecieved and checking for a timeout is handled in the same place.
Since there is no inter-thread communication nor coordination needed (there are no threads that need to communicate) it probably is more robust as well.
A few hints on checking for the timeout:
You should calculate the timeout threshold when you update timeLastReceived and then only check agains the current time instead of calculating it in every iteration.
You might want to calculate the timeout threshold before processing the input in order not to have it depend on the processing time as well.
Finally, there are alternative approaches like using java.util.Timer. Here you could simply schedule a timeout task which is executed when the timeout should occur. That task then would check if the timeout really happened and if not it just returns.
To handle new input before the timeout occured you could use at least two approaches:
Cancel the current timeout task, remove it from the timer and schedule a new one.
If there is already a scheduled timeout task then don't schedule a new one but wait for the current one to run. The current one then checks for the timeout and if none happened it schedules a new task (or itself) for the current anticipated timeout (note that this would require some inter-thread communcation so be careful here).
You need to have two threads - one waiting for data coming in through the InputStream / Reader, and one that's watching the time to see if the time elapsed as taken too long. The only way to do it with 1 thread would be to sleep for a segment of the timeout period and then poll for data periodically. But that's less efficient than having a separate thread dedicated to reading from your InputStream/Reader.
You may want to check out Timeout as a generic option for implementing a timeout
I have one method execute(data) which takes considerable time (depending on data like 10 seconds or 20 seconds), it has timeout feature which is 30 seconds default. I want to test that method. One way of doing it is to collect enough data which lasts more than 30 seconds and then see whether I get timeout exception. Other way of doing it is to use threads. What I intend to do is to run method for some milliseconds and then put thread on wait before I get timeout exception or make it last for some seconds.Can any one please suggest how can I achieve that.
You should walk through the Java Threads Tutorial (Concurrency). Any answer on Stack Overflow would need to be really long to help you here, and the Threads/Concurrency tutorials already cover this well.
http://docs.oracle.com/javase/tutorial/essential/concurrency/
You could use
Thread.sleep( millis );
to put the thread to sleep for the required time.
Or, you could put your data processing code into a loop, so that it processes it multiple times. This would recreate the scenario of the thread actually processing data for longer than 30 seconds.
Or, you could test your code with a shorter timeout value.