I am conducting some throughput testing. My application has to
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:
A JVM may max out on CPU usage because of the incoming workload. The server capacity may not be sized sufficiently to handle the rate of requests coming in and in such a situation, the Java application may be doing work, trying to keep up with the workload.
So Java is CPU-intensive compared to the same program written in C. Java is not interpreted at runtime, at least not as most people would think of interpretation as being. In any competent JVM, Java bytecode is JITed into native code and then executed. Yes, this is slower than running native code.
SQL Server Management Studio Once you connect to your SQL Server or Azure SQL instance, you can select Reports > Performance Dashboard and see the current and historical values of CPU usage. Here you can find the query texts of the top resource consumers and identify the queries that are causing the CPU issues.
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 :)).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With