Java Handler Post Delayed task is not accurate timer - java

I have created two Handlers, which each have Runnable Tasks which run in a loop through:
// do it again 1 hundeth of a sec later
QuestionHandler.postDelayed(QuestionHandlerTask, 10);
And through:
// and do it again 1 sec later
GameHandler.postDelayed(GameHandlerTask, 1000);
Both the tasks start at 10 and count downwards and their results are displayed in labels. The reason i have used 2 handler's is because i want 1 timer to count down by milleseconds and 1 to count down by seconds.
However, the GameHandlerTask that runs once per second runs significantly faster than the other. By the time it reaches 1 the QuestionHandlerTask is still at 4 seconds:
Is this a commonly known problem or have i done something seriously wrong? Is there some way i can make it so my timers run at the same speed? Is there other methods that i can use that would be better in this circumstance (where i want precise timing)?

This is because the screen cannot refresh 100 times per second, which is what is happening with the 10 millisecond timer. So the screen tries to show every single value that you told it to show, but can only do so at its maximum refresh rate.
It is not the timer that is slow, it is the screen that can't keep up with the timer.
To solve this, try to calculate how much does the number need to go down by in 40 milliseconds, and you start a timer of 40ms. This will update the screen 25 times a second, which the screen should be able to handle.

Related

How to implement scheduling inside a running thread group?

I am testing a certain "functionality" that happens after log in.
The test case is 500 users exercising that functionality within 5 minutes.
I can add a synchronising timer after the log in, to ensure all 500 threads have logged in but then it will do all 500 "functionality" tasks at once, rather than 5 minutes, which will crash the app (it thinks there's a DDoS attack and shuts down).
Right now, I am handling this by giving some think time after login, to slow down login to a stable figure that I can predict and then start "functionality" at each thread's turn, as scheduled by: the main scheduler + the the log in response time + the think time...
But that's a bit fuzzy.
Is there a way to "ramp up" tasks once already running?
I can think in two options.
The first one is two use random times. You would use the range from 0 seconds to 300 - 1 that is [0-300) or using millis [0-300000). Then sleep the thread basesd on this ramdon time.
This approach can be a little more realist, because for instance, in a specific second of the given interval you don't have any threads starting and in other particular second you have 2-3. This still should be well balanced in general, since you won't make all petitions at start.
The second one is to start the threads uniformly. During your configuration time (login and before firing the threads) you can use something like an AtomicInteger, initializing it with new AtomicInteger(0) and calling getAndIncrement() to assign the possition of the thread, in the range [0-500) and then when you fire the threads sleep 300.0 * id / 500.0 milliseconds to execute the task/petition.
By default JMeter executes requests as fast as it can, you can "throttle" the execution to desired throughput (request per minute) rate using Constant Throughput Timer.
Example Test Plan would look like:
Thread Group
Login
Synchronizing Timer
Functionality
Constant Throughput Timer
Constant Throughput Timer follows JMeter Scoping Rules so you can apply it either to single sampler or to a group of samplers.

Make a thread generate fixed number of requests per second

I am doing calculations in milliseconds and I really do not want my thread to spend more time doing time calculations rather the job it is assigned to do. However I want to implement something that:
1- It should not generate more than n requests per second
2- If it has generated less, it should start at zero for the next second(obviously :D)
I am trying to do some performance benchmarking where my goal is to give all cpu to only processing and not time computations after every request. Roughly, I am processing
08:36 - 171299
08:37 - 170970
08:38 - 163763
I want to make sure I do not make more than 160000 requests per minute here. How to acheive that is the problem.
Thanks in advance!
You can combine ScheduledExecutorService to run some code every second and this answer to set timeout on that code. In the end, your runnable that should have 1-second timeout should generate up to n requests, and if it times out, it will start in next second with new context.

java 8 adjustable time scheduler

I need to implement the following functionality:
I have something like a messenger. So, user sends messages, and system should wait 3 sec after last message. So, for example user sends 'Hello!',and system should start countdown, and if 2 seconds passed and user sends 'hey!', it should start counting down again. If 3 secs has passed, special action should fire. Are there any special classes and technologies for this in java and java frameworks?
UPDATE:
Basically I need something ready to implement a time barrier with ability to change waiting time. So, I need to wait 3 secs after LAST message came. So, when API gets message 'Hello', it should start waiting for 3 secs before perform some action. If 2 secs passed and API gets next message 'Hey', it should init it's time counter and start counting down seconds again. So, totally 5 secs are passed. And If there's no message to API for 3 secs after last message, action should run.
If I do Thread.sleep or future.get I specify fixed number of secs to delay, but I want to have ability to change this number online and system should take it. For example:
int counter=5;//any shared seconds quantity holder
while(true){
counter--;
Thread.sleep(1000);
}
....
setCounter(5);//I call somewhere, and loop will be looping again.
But I am looking for something stable and tested, not to invent a bicycle.

Mimic spinning wheel in code?

I've been searching a lot but I just can see to find any examples of what I'm looking for.
Basically what I want to do is mimic the functionality of spinning a wheel like on wheel of fortune but with no graphics, just code. So what would happen is I'd start the function and then it'd output 1 number per second, then after a certain amount of time it'll start spitting out 1 number every 3 seconds, then every 5 seconds, etc.. until it finally stops and picks 1 number. Kind of like how when you spin a bottle it gradually comes to a stop.
At first I thought I could achieve this using ScheduledExecutors but its really tricky seeing as I have to calculated when to stop the previous task before I start the next one.
Does anyone have a more simple solution?

Jmeter - simulating more complex load scenarios?

Been experimenting with Jmeter, and I'd like to know the best way to accomplish:
20 users logging onto an application, over 20 minutes, and performing some actions for another 20 minutes, before logging off over a period of 20 minutes. I.e. have 200 users logging on, and then once ALL of them are logged on, begin 20 minute timer. Once 20 minutes are up, start to log the ones who logged on earliest off.
I realise this MAY or MAY NOT BE a realistic testing scenario, but i'd like to see if it's possible.
At the moment I have a test plan whereby a user logs on, performs some actions, and then logs off. I can't see how I can ramp up and ramp down.
There's an option in Test Plan "Run thread groups consecutively". Set it to checked.
Then add 3 thread groups to your test plan. I'd suggest using Thread Group for first (20 threads, loop count 1, ramp up time 1), Ultimate Thread Group (20 threads starting immediately and holding load for 20min) for second and Thread Group again for third (20 threads, loop count 1, ramp up time 1).
Place appropriate samplers inside each TG - first just logs in, second does actions, third logs off.
That's it. If you have any troubles - let me know.
You'll need several thread groups in JMeter starting off and running at different intervals, in that way you could ensure that the users who start first will end first.
Also see a related question on this.
You can have no of users=20, ramp up time=1200 sec (1 per min), difference of time between test start and test end time=20 min to achieve that.
I think I had a similar problem in the past
Here Want I've did:
First set your thread group to have 20 thread with a rampup period of 60 seconds
After the login put a "test action" (in the sampler menu)
target = current thread, with the action pause and 20 minutes (1 200 000 ms) or more if you want to be safe.
After this test action, put all your navigating request.
Once your navigation is done, put another "test action" with the same setting has the previous one
(target = current thread, with the action pause and 20 minutes (1 200 000 ms))
put the logouf request after the sampler.
This should cover you case.
Take note that the sampler just pause your thread so the first thread that start should be the first thread that end.
If you want to scale it to 200 you just need to change your thread group rampup period to 6 or 5 seconds.
hope it's help.

Categories