15

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)
Air
  • 5,084
  • 5
  • 25
  • 19
  • Can you further explain why utilizing the CPU is necessary but simply sleeping with Thread.sleep() isn't suitable? – matt b Dec 19 '08 at 20:30
  • I'm not sure I understand what you mean by "exercise the CPU" and why exactly you would need to do that. Could you please elaborate a bit more? What exactly are you testing here? From what I can see it is either the "some processing", read/write JMS or something that sends data to this thing.Thanks. – Szymon Rozga Dec 19 '08 at 20:57
  • > sleep isn't suitable I need the JMS operations to happen under realistic CPU conditions. An idle CPU might give me falsely high JMS performance; I need the CPU to be 'realistically' busy so my JMS measurement is good. – Air Dec 19 '08 at 21:38

5 Answers5

23

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
  • 188,989
  • 46
  • 291
  • 292
6

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
  • 18,734
  • 5
  • 61
  • 83
4

Create a very large collection of random objects and then alternate calls to Collections.shuffle() and Collections.sort().

I used Jakarta Commons Lang to generate random strings for the purposes of shuffling/sorting.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
cliff.meyers
  • 17,666
  • 5
  • 51
  • 66
0

Yet another stuff you can use, maybe :

        long start  = System.currentTimeMillis();
        long count = 0l;
        for(long x=0;x<Integer.MAX_VALUE ;x++){
            count+=1;
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start +" ms");
daoway
  • 779
  • 7
  • 8
0

Create a matrix and do a couple of matrix manipulations.

You can tune that pretty easily by varying the size of the matrix.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Huntrods
  • 2,561
  • 3
  • 22
  • 29