Two Laptop Bag*
The moose likes Performance and the fly likes Util library to help make my code run *slower*? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Util library to help make my code run *slower*?" Watch "Util library to help make my code run *slower*?" New topic
Author

Util library to help make my code run *slower*?

Jimi Svedenholm
Ranch Hand

Joined: May 19, 2001
Posts: 53
Hi,

Does anyone know of any util library that can help ones code to run slower?

Some background: I am currently working on some code that will do GET and POST http requests to web servers (within the same local network), and I would like a way to be able to control the speed that it does sends these requests, if there are a queue of requests waiting to be sent.

I know the simple way is to just make the thread sleep a few ms after each request, but I would like some more detailed control. I would like to be able to send a maximum of lets say 10 requests per second, and these 10 requests should be distributed equally (roughly) over that second (ie i don't want it to send 10 requests rapidly, and then do a pause until the second has finished and then sending 10 more requests rapidly). And preferably it should be able to do this in a multithreaded way.

I am aware that this should be fully possible do to using java.util.concurrent, but that is so low level that I would have to write most of the logic any way, and I figured that this need should already have come up in other projects, so maybe there already exists some util library that does this. Or?

Of course, if someone could give some simple example on how one could use java.util.concurrent to do this without having to write all the logic from scratch, then please tell. The main problem I'm having when I read the documentation for java.util.concurrent is that I can't figure out how to make the threads triggers happen when I want them to. Because the delays between each trigger depends on how often this and the other threads has been triggered lately.

Of course, since I use Httpclient to make the request, it would be nice to know if someone knows how to make it send its requests in this way. Ie I would then have multiple threads using httpclient, and httpclient makes sure that the requests for all these threads are sent with a maximum number per second. The only configuration I have seen is the number of concurrent connections allowed for a single host, but that is not the same thing.

Regards
/Jimi
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Jimi,

mabey Java's DelayQueue is of any help for you. Another interesting library I've found with a quick Google search seems to be this Task Queue API although I must admit that I haven't yet used neither the DelayQueue nor this API.

Anyway, I think no matter which solution or API you use there will be some work to be done on your side. You can't just simply slow down the execution of your Java code. So you have to define some kind of "steps" you want to be executed with a delay. I don't know the details of your project but I'm pretty sure that there will be no API available which can in a magical way figure out which steps of your program to slow down in a reasonable way.

Perhaps you should dissect the whole work to be done in smaller "working packages" or however you want to name it an use something like the DelayQueue to enqueue these tasks and execute them with some delay between each task.

Marco
Jimi Svedenholm
Ranch Hand

Joined: May 19, 2001
Posts: 53
Marco Ehrentreich wrote:Hi Jimi,

mabey Java's DelayQueue is of any help for you. Another interesting library I've found with a quick Google search seems to be this Task Queue API although I must admit that I haven't yet used neither the DelayQueue nor this API.


Thanks for your tips. I have already looked at the DelayQueue, and the problem with that is that I don't know what delays to use when the queue is created. The Task Queue API looks promising, but I must say that I am a bit confused of all this talk about URLs. As if they assume the API will be used in a web server context. Also it seems a bit complex, and it is still experimental.


Anyway, I think no matter which solution or API you use there will be some work to be done on your side. You can't just simply slow down the execution of your Java code. So you have to define some kind of "steps" you want to be executed with a delay. I don't know the details of your project but I'm pretty sure that there will be no API available which can in a magical way figure out which steps of your program to slow down in a reasonable way.


Well, of course I know I have to do *some* work on my side.
But for the simple case, I was thinking of using this util class in a way like this:



Ie a very simple configuration, and then two simple methods to keep track of in the job execution threads. I was actually writing on a class that would do this, but then realised that it could already exist, so then I decided to ask in this forum.


Perhaps you should dissect the whole work to be done in smaller "working packages" or however you want to name it an use something like the DelayQueue to enqueue these tasks and execute them with some delay between each task.


Well, as I said, simply sleeping a specific number of milliseconds between each task is not enough for my needs. And regarding smaller "working packages", each task is in my case to do a http get/post, and I don't think I can divide that in any smaller parts.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Jimi,

as I said it's hard to decide what would be a good solution in your specific case without knowing all details. So I can only come up with some (hopefully) useful advices...

For the size of your tasks I agree that it doesn't make much sense to make it smaller than one POST request at a time. Of course it doesn't make much sense to split one request and risk timeouts etc.

I'm still not sure what's the big deal with the delay you want to specify as late as possible. If you would use such a DelayQueue I don't see why it should be a problem to prepare a collection of task and built and populate the queue just when you know what the delay should be. You could even destroy the queue and build a new one if the delay changes in-between.

But I've found a specific ExecutorService in the JDK's concurrent util package which looks even more promising than the DelayQueue: ScheduledExecutorService
It's in general better and more high-level not to take care of the thread handling yourself but to use the ExecutorService facility for this. I just didn't know about this specific executor before. But it seems to do pretty much what you need.

There's a small example in the API doc, too. You'd basically have to encapsulate your working tasks (of sending a POST request) inside a Runnable or Future and enqueue it into the ExecutorService as soon as you know the delay between jobs.

The only other more flexible but also more complex solution for scheduling of jobs I know of, would be the Quartz framework. But for my understanding of what you're trying to achieve this would be overkill.


Hope this one is more helpful

Marco

Jimi Svedenholm
Ranch Hand

Joined: May 19, 2001
Posts: 53
Marco Ehrentreich wrote:Hi Jimi,

as I said it's hard to decide what would be a good solution in your specific case without knowing all details. So I can only come up with some (hopefully) useful advices...

For the size of your tasks I agree that it doesn't make much sense to make it smaller than one POST request at a time. Of course it doesn't make much sense to split one request and risk timeouts etc.

I'm still not sure what's the big deal with the delay you want to specify as late as possible. If you would use such a DelayQueue I don't see why it should be a problem to prepare a collection of task and built and populate the queue just when you know what the delay should be. You could even destroy the queue and build a new one if the delay changes in-between.

But I've found a specific ExecutorService in the JDK's concurrent util package which looks even more promising than the DelayQueue: ScheduledExecutorService
It's in general better and more high-level not to take care of the thread handling yourself but to use the ExecutorService facility for this. I just didn't know about this specific executor before. But it seems to do pretty much what you need.

There's a small example in the API doc, too. You'd basically have to encapsulate your working tasks (of sending a POST request) inside a Runnable or Future and enqueue it into the ExecutorService as soon as you know the delay between jobs.

The only other more flexible but also more complex solution for scheduling of jobs I know of, would be the Quartz framework. But for my understanding of what you're trying to achieve this would be overkill.


Hope this one is more helpful

Marco



I looked at the ScheduledExecutorService before, but for some reason I didn't really take notice to the method scheduleAtFixedRate. But I did so now, and after some simple tests I think I can conclude that I know how to use it to achieve my goals.

Thank you for your time and effort in helping me, Marco.

/Jimi
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

You're welcome! I'm glad to hear that my advice could be helpful at last
 
jQuery in Action, 2nd edition
 
subject: Util library to help make my code run *slower*?