tags:

views:

53

answers:

2

In order to test how a program reacts when system resources become scarce (mainly the CPU but I'm interested in disk I/O too), I'd like to put an arbitrary load on the system.

Currently I'm doing something like this:

#!/bin/bash

while true
do
    echo "a" >> a.txt
    md5 a.txt
done

I could also start mp3-encoding audio files, or whatever.

What would be an easy and small Bash script that could be used to simulate an arbitrary load, ideally configurable using parameter(s)?

+1  A: 

If you are looking for only CPU load, you may simply execute an empty loop:

while :; do : ; done

for disk I/O you can use a sequence of file copy od disk dump (see "dd" command)

Regards

Giuseppe Guerrini
@Giuseppe Guerrini: +1 busy looping is indeed great I just tried it but it shall only make one core work (I know, I know, I can simply launch 'x' of them where 'x' is the number of cores the machine has ;)
Webinator
On many unix-like systems there isa the command "taskset". You can use it to launch a prosess on a specific CPU. I think it can help you to have a better control on the CPU load. Bye!
Giuseppe Guerrini
@Giuseppe Guerrini: yup right I forgot about cpu-affinity :)
Webinator
A: 

Well, you can test CPU load by doing an empty loop as stated above.

For disk I/O, it depends how sophisticated you need. If you just want something that can test I/O speed at constant full blast, try something like this:

yes "a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long string (longer than this example)" | dd if=/dev/stdin of=file

Which will give you a nice speed reading when you kill the command.

If you want to tune the exact amount of load, it gets a bit tricker.

However, why are you rolling your own benchmarks? There are plenty already existing, and you can get a bunch of good ones with the Phoronix Test Suite.

Max E.
He doesn't want benchmarks, but rather how a program behaves with severely limited resources.
Justin Smith
@Max E.: what Justin Smith said: I don't need to benchmark the CPU, I want to test how a program behaves when it's run on system(s) that have a high CPU and/or I/O load :)
Webinator
@WizardOfOdds and Justin, thanks. I only skimmed the question. :)
Max E.