9

Is there a way to benchmark a bash script's performance? the script downloads a remote file, and then makes calls to multiple commandline programs to manipulate. I would like to know (or as much as possible):

  • Total time
    • Time spent downloading
    • Time spent on each command called
    • -=[ I think these could be wrapped in "time" calls right? ]=-
  • Average download speed
    • uses wget
  • Total Memory used
  • Total CPU usage
    • CPU usage per command called

I'm able to make edits to the bash script to insert any benchmark commands needed at specific points (ie, between app calls). Not sure if some "top" ninja-ry could solve this or not. Not able to find anything useful (at least to limited understanding) in man file.

Will be running the benchmarks on OSX Terminal as well as Ubuntu (if either matter).

3
  • this sort of thing gets discussed occasionally with some reasonable answers (there is nothing automatic, except for vendor pkgs, or consulting hours ;-). Try looking for messages with tag=benchmark/ing and tag=testing. Good luck.
    – shellter
    Nov 6, 2011 at 22:11
  • You are right about wrapping the total time in time calls, as for the average download speed, get the size of the file, and then put that over time spent downloading for one general result. Memory and cpu usage can be retrieved through various calls as well. Here is something on using /proc/stat for usage. colby.id.au/node/39 Nov 6, 2011 at 22:17
  • Since bash benchmark command redirects here. I think it is worth mentionning hyperfine
    – Ulysse BN
    Oct 20, 2022 at 15:28

2 Answers 2

19
strace -o trace -c -Ttt ./scrip
  1. -c is to trace the time spent by cpu on specific call.
  2. -Ttt will tell you time in microseconds at time of each system call running.
  3. -o will save output in file "trace".
2
  • Does this include time spend on networking? In my case I'm trying to get stats on "git checkout" and this seems to report less than a tenth of a second total time when the command clearly is busy for a much longer amount of time (~3m) Apr 14, 2014 at 20:13
  • 1
    not sure, but you can get that idea by the system calls it made.
    – Farhan
    Apr 14, 2014 at 20:29
4

You should be able to achieve this a number of ways. One way is to use time built-in function for each command of interest and capture the results. You may have to be careful about any pipes and redirects;

You may also consider trapping SIGCHLD, DEBUG, RETURN, ERR and EXIT signals and putting timing information in there, but you may not get some results.

This concept of CPU usage of each command won't give you any thing useful, all commands use 100% of cpu. Memory usage is something you can pull out but you should look at

If you want to get deep process statistics then you would want to use strace... See strace(1) man page for details. I doubt that -Ttt as it is suggest elsewhere is useful all that tells you are system call times and you want other process trace info.

You may also want to see ltrace and dstat tools.

A similar question is answered here Linux benchmarking tools

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.