views:

1032

answers:

5

I am conducting some throughput testing. My application has to

  1. read from JMS
  2. do some processing
  3. write to JMS

My goal here is to simulate #2, 'some processing'. That is, introduce a delay and occupy the CPU for a given time (e.g. 500ms) before forwarding the event.

The naive approach would be to Thread.sleep(500). This would introduce the right delay in execution, but would not exercise the CPU.

Calculating Fibonacci numbers is one option. Has anyone used any interesting techniques just to keep CPU(s) busy for a given time?

Ideal characteristics would be:

  • Performs a variety of instructions, rather than (for example) just spinning on a loop
  • Not something the HotSpot VM is going to optimise away to nothing
  • Has an easy way to adjust the processing period up or down (time to complete will clearly vary given the hardware)
A: 

Don't know about java but..

x=0;
for(i=0;i<1000000;i++){
 x++;
}

should to the trick:)

for(i=0;i<1000000;i++){

}

should also work.

If you really want to slow things down, then write chunks of random data to a file (the HDD is sloooow, and you also put a lot of pressure on the file system, so the kernel will get pretty busy)

Quamis
Empty loops can be easily optimized away. Writing lots of data to a file, doesn't exercise the CPU, bucause it's an I/O bound task.
Diomidis Spinellis
Also a loop like this will not run for the same amount of time on different processors
matt b
If you want to verify, simply insert similar code in C, and compile it with GCC, first with no optimization, and then -O3.
Calyth
Yeah, thats why i said to write random data to a file:)for the same reason i wrote the first loops doing incrementingdont understand why im voted down for this.also he never mentioned if he needed the loop to block for a certain time
Quamis
for a non-optimizable loop jost do x=microtime()while(microtime()-x>1000){}..now that wasnt so hard was it? still for real system stres one should rely on filesystem functions as they are mostly non-interruptible, and can easy stress the whole system.
Quamis
+3  A: 

Encrypt a string (in a loop) by calling Cipher.update(). Encryption algorithms are by definition very difficult to optimize. The only problem is that there is some non-trivial setup you need to perform. I'm marking this answer as community wiki, so that somebody who's written it recently can fill it in.

Diomidis Spinellis
A: 

One of my favorites was to create a very large List of random objects and then alternate calls to Collections.shuffle() and Collections.sort(). I used Jakara Commons Lang to quickly generate some random strings for the purposes of sorting.

cliff.meyers
+1  A: 

You could try something simple like

private static void spin(int milliseconds) {
    long sleepTime = milliseconds*1000000L; // convert to nanoseconds
    long startTime = System.nanoTime();
    while ((System.nanoTime() - startTime) < sleepTime) {}
}

Test:

public static void main(String[] args) {
    final int NUM_TESTS = 1000;
    long start = System.nanoTime();
    for (int i = 0; i < NUM_TESTS; i++) {
        spin(500);
    }
    System.out.println("Took " + (System.nanoTime()-start)/1000000 +
        "ms (expected " + (NUM_TESTS*500) + ")");
}

My output:

$ java SpinTest
Took 500023ms (expected 500000)

So the loop didn't get optimized away (and yeah, I spiked my CPU to 100% for eight minutes just to test this :)).

Michael Myers
A: 

Create a matrix and do a couple of matrix manipulations. You can tune that pretty easily by varying the size of the matrix.

Cheers,

-R

Huntrods