# Script for testing CPU values and rules

**URL:** <https://community.itrsgroup.com/t/script-for-testing-cpu-values-and-rules/272>\
**Category:** Best Practice\
**Tags:** openai\
**Created:** [18 March 2024 14:27 UTC](https://community.itrsgroup.com/t/script-for-testing-cpu-values-and-rules/272 "2024-03-18T14:27:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Lapp](https://dub1.discourse-cdn.com/flex013/user_avatar/community.itrsgroup.com/steve_lapp/32/18_2.png) [@Steve\_Lapp](https://community.itrsgroup.com/u/Steve_Lapp)\
**Post date:** [18 March 2024 14:27 UTC](https://community.itrsgroup.com/t/script-for-testing-cpu-values-and-rules/272/1 "2024-03-18T14:27:16Z")

</div>

I needed a script that would set a CPU usage value to a certain percent to test some static and forecasting alerts. All right for this to openAI. This script takes a parameter 1-100 and the script will attempt to keep the CPU value at that level.

```auto
#!/bin/bash

# Check if usage percentage is passed as an argument
if ["$#" -ne 1]; then
    echo "Usage: $0 <cpu_usage_percentage>"
    exit 1
fi

# Desired CPU usage percentage
cpu_usage=$1

# Calculate sleep duration based on desired CPU usage
sleep_duration=$(awk "BEGIN {print (1 - $cpu_usage / 100) / ($cpu_usage / 100)}")

# Function to consume CPU up to the specified percentage
limit_cpu_usage() {
    while true; do
        # Start time
        start=$(date +%s.%N)

        # Busy loop for the duration of one second
        end=$(echo "$start + 1" | bc)
        while true; do
            current=$(date +%s.%N)
            check=$(echo "$current < $end" | bc)
            if ["$check" -eq 0]; then
                break
            fi
            : # No-op
        done

        # Sleep for the calculated duration
        sleep $sleep_duration
    done
}

# Run the function in the foreground
limit_cpu_usage

# Run the function in the background
# limit_cpu_usage &

```

---

<div class="post-metadata">

**Author:** ![Steve\_Lapp](https://dub1.discourse-cdn.com/flex013/user_avatar/community.itrsgroup.com/steve_lapp/32/18_2.png) [@Steve\_Lapp](https://community.itrsgroup.com/u/Steve_Lapp)\
**Post date:** [19 March 2024 17:08 UTC](https://community.itrsgroup.com/t/script-for-testing-cpu-values-and-rules/272/2 "2024-03-19T17:08:54Z")

</div>

As an example of how I am using the script, we are running some tests on zooming in and out in our new charting package. We combine the severity timeline with the metric value.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/itrsgroup/original/1X/68ee2513ce6afbfe09013c4ad396cc74c1a44f0d.png)

---

<div class="post-metadata">

**Author:** ![pgalbavy](https://dub1.discourse-cdn.com/flex013/user_avatar/community.itrsgroup.com/pgalbavy/32/15_2.png) [@pgalbavy](https://community.itrsgroup.com/u/pgalbavy)\
**Post date:** [27 March 2024 21:15 UTC](https://community.itrsgroup.com/t/script-for-testing-cpu-values-and-rules/272/3 "2024-03-27T21:15:11Z")

</div>

Look good, but here’s your next challenge (tongue firmly in cheek):

[![](https://europe1.discourse-cdn.com/flex013/uploads/itrsgroup/original/1X/0fce31e44c6c155f8afcfc2015d1ae46d42e1fec.jpeg "Windows Task Manager Runs Doom (896 cores)") ](https://www.youtube.com/watch?v=hSoCmAoIMOU)

---

<div class="post-metadata">

**Author:** ![Steve\_Lapp](https://dub1.discourse-cdn.com/flex013/user_avatar/community.itrsgroup.com/steve_lapp/32/18_2.png) [@Steve\_Lapp](https://community.itrsgroup.com/u/Steve_Lapp)\
**Post date:** [28 March 2024 19:49 UTC](https://community.itrsgroup.com/t/script-for-testing-cpu-values-and-rules/272/4 "2024-03-28T19:49:54Z")

</div>

That looks like exactly what a 896 core machine should be used for!
