Skip to content

Instantly share code, notes, and snippets.

@SriramKeerthi
Last active September 27, 2022 09:25
Show Gist options
  • Select an option

  • Save SriramKeerthi/0f1513a62b3b09fecaeb to your computer and use it in GitHub Desktop.

Select an option

Save SriramKeerthi/0f1513a62b3b09fecaeb to your computer and use it in GitHub Desktop.

Revisions

  1. SriramKeerthi revised this gist Oct 18, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Load.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    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
    */
  2. Sriram Keerthi M K revised this gist Mar 9, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Load.java
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ public void run() {
    try {
    // Loop for the given duration
    while (System.currentTimeMillis() - startTime < duration) {
    // Every 100ms, sleep for the percentage of "unloaded" time
    // Every 100ms, sleep for the percentage of unladen time
    if (System.currentTimeMillis() % 100 == 0) {
    Thread.sleep((long) Math.floor((1 - load) * 100));
    }
  3. Sriram Keerthi M K created this gist Mar 9, 2016.
    61 changes: 61 additions & 0 deletions Load.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    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 "unloaded" time
    if (System.currentTimeMillis() % 100 == 0) {
    Thread.sleep((long) Math.floor((1 - load) * 100));
    }
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }