Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple CPU Load Generator in Java
package com.caffinc.grex.core;
/**
* Generates Load on the CPU by keeping it busy for the given load percentage
* @author Sriram
*/
public class Load {
/**
* Starts the Load Generation
* @param args Command line arguments, ignored
*/
public static void main(String[] args) {
int numCore = 2;
int numThreadsPerCore = 2;
double load = 0.8;
final long duration = 100000;
for (int thread = 0; thread < numCore * numThreadsPerCore; thread++) {
new BusyThread("Thread" + thread, load, duration).start();
}
}
/**
* Thread that actually generates the given load
* @author Sriram
*/
private static class BusyThread extends Thread {
private double load;
private long duration;
/**
* Constructor which creates the thread
* @param name Name of this thread
* @param load Load % that this thread should generate
* @param duration Duration that this thread should generate the load for
*/
public BusyThread(String name, double load, long duration) {
super(name);
this.load = load;
this.duration = duration;
}
/**
* Generates the load when run
*/
@Override
public void run() {
long startTime = System.currentTimeMillis();
try {
// Loop for the given duration
while (System.currentTimeMillis() - startTime < duration) {
// Every 100ms, sleep for the percentage of unladen time
if (System.currentTimeMillis() % 100 == 0) {
Thread.sleep((long) Math.floor((1 - load) * 100));
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@pradykaushik

This comment has been minimized.

Copy link

commented Feb 19, 2018

I forked this gist and created a wrapper. Please check it out here on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.