Skip to content

Instantly share code, notes, and snippets.

@pradykaushik
Forked from SriramKeerthi/Load.java
Created October 23, 2017 21:00
Show Gist options
  • Save pradykaushik/3fd144d37f9283637186710fdc5b97cc to your computer and use it in GitHub Desktop.
Save pradykaushik/3fd144d37f9283637186710fdc5b97cc to your computer and use it in GitHub Desktop.
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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment