Guest User

ercan

a guest
Mar 18th, 2009
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.26 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # NOTE: bc math library must be installed!
  4.  
  5. # Generates load with an empty loop
  6. # Input parameter: Duration in seconds (Float)
  7. full_load()
  8. {
  9.         duration=$1  # Input parameter
  10.         st=`date +%s.%N`  # Start time of the loop <epoch seconds>.<nanoseconds>
  11.         while true; do
  12.                 true
  13.                 # check if we should finish
  14.                 now=`date +%s.%N`
  15.                 is_end=`echo "($st + $duration) <= $now" | bc`
  16.                 if [ "$is_end" = "1" ]; then
  17.                         return 1
  18.                 fi
  19.         done
  20. }
  21.  
  22. # This is the main loop that generates sinusoidal cpu load (for 1 cpu)
  23. start=`date +%s`  # Record the script start time
  24. while true; do
  25.         # As seconds pass by, the amount of sleep changes according to cosine function
  26.         # Start with cos(0) = 1
  27.         diff_seconds=$((`date +%s` - $start))
  28.         i=`echo "scale=4; c($diff_seconds / 20) + 1" | bc -l`  # c() is the cosine function
  29.  
  30.         # Full load for 1 second, sleep i seconds
  31.         full_load 1
  32.         sleep $i
  33.         echo "$diff_seconds: Worked 1.0 s., slept $i s., expected load (%): "`echo "scale=4; (1/(1+$i)) * 100" | bc`
  34.         echo "$diff_seconds "`echo "scale=4; (1/(1+$i)) * 100" | bc` >> data.txt
  35. done
  36.  
Advertisement
Add Comment
Please, Sign In to add comment