Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # NOTE: bc math library must be installed!
- # Generates load with an empty loop
- # Input parameter: Duration in seconds (Float)
- full_load()
- {
- duration=$1 # Input parameter
- st=`date +%s.%N` # Start time of the loop <epoch seconds>.<nanoseconds>
- while true; do
- true
- # check if we should finish
- now=`date +%s.%N`
- is_end=`echo "($st + $duration) <= $now" | bc`
- if [ "$is_end" = "1" ]; then
- return 1
- fi
- done
- }
- # This is the main loop that generates sinusoidal cpu load (for 1 cpu)
- start=`date +%s` # Record the script start time
- while true; do
- # As seconds pass by, the amount of sleep changes according to cosine function
- # Start with cos(0) = 1
- diff_seconds=$((`date +%s` - $start))
- i=`echo "scale=4; c($diff_seconds / 20) + 1" | bc -l` # c() is the cosine function
- # Full load for 1 second, sleep i seconds
- full_load 1
- sleep $i
- echo "$diff_seconds: Worked 1.0 s., slept $i s., expected load (%): "`echo "scale=4; (1/(1+$i)) * 100" | bc`
- echo "$diff_seconds "`echo "scale=4; (1/(1+$i)) * 100" | bc` >> data.txt
- done
Advertisement
Add Comment
Please, Sign In to add comment