Last active
September 27, 2022 09:25
-
-
Save SriramKeerthi/0f1513a62b3b09fecaeb to your computer and use it in GitHub Desktop.
Simple CPU Load Generator in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.caffinc.grex.core; | |
/** | |
* SPDX-License-Identifier: MIT | |
* | |
* 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(); | |
} | |
} | |
} | |
} |
I forked this gist and created a wrapper. Please check it out here on this.
That looks like a lot of good work done on this. I can't remember but I think I created a runnable jar version with a lot of configurations and a dashboard for this as well, but you seem to have put in a lot of work as well. Hope it's everything you want it to be.
This is awesome! Thank you
Thanks! Very helpfull
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added it, hope you get to build something cool with it.