Generating CPU load for tests


Sometimes you need to generate a heavy CPU load in your app for testing purposes.

One of the possible approaches may be to employ hashing algorithms.

For example, Spring has Pbkdf2PasswordEncoder, so we can run multithreaded password hashing:

public void runHeavyLoad() {
    String pepper = "pepper"; // secret key used by password encoding
    int iterations = 200000;  // number of hash iteration
    int hashWidth = 256;      // hash with in bits
    int threadCount = 1000;   // simultaneous thread count to be run

    for (int i = 0; i < threadCount; i++) {
        final int finalI = i;
        new Thread(() -> {
            while(true) {
                Pbkdf2PasswordEncoder pbkdf2PasswordEncoder = new Pbkdf2PasswordEncoder(pepper, iterations, hashWidth);
                pbkdf2PasswordEncoder.encode(UUID.randomUUID().toString());
            }
        }).start();
    }
}