help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Time and analyze execution of command


From: Seth David Schoen
Subject: Re: Time and analyze execution of command
Date: Mon, 5 Jul 2021 14:52:48 -0700

Julius Hamilton writes:

> Hey,
> 
> I would like to see a print out of everything the computer did on some
> pretty low level, but not too low, next to a time, to see how long each
> step of a certain command executing took.
> For example, when I run the tool googler.
> 
> Is there a recommended way of doing this?

Are you thinking of the individual commands executed by a shell script?

"set -x" in bash will cause each command to be displayed as it's run --
but you don't get timestamps with that.

You could try

trap DEBUG 'date +%s.%N'
set -x

at the top of a bash script (if you're on a system with GNU date).  The
annoying part of this is that the date command will get printed by set
-x, too.

You could also manually put $TIME at the beginning of each command in
the script, and then if you want the timings, you could do

TIME=time

at the top of the script (perhaps with "set -x" in order to see the
commands).  Or maybe put TIME (without the $) at the beginning of each
command, and then at the top of the script do

TIME() { $@; }

if you don't want the timings, and

TIME() { echo $@ >&2; time $@; }

if you do.

If you're looking for more low-level information than this, look at
strace and ltrace.  The former gives information about individual system
calls (into the OS kernel), while the latter gives information about
individual library calls (into functions in the standard library or
other C libraries).  On other Unixes you might have truss or dtruss
instead.  This is possibly much lower-level than you're thinking of,
but you can potentially still limit it down to events that you care more
about.

In general, this kind of information about a program in any programming
language is generated by a tool called a "profiler", and is called
"profiling" the program's execution (not to be confused with the
.bash_profile and .profile files, which are used to set up a shell's
environment automatically).  There are often built-in and third-party
profiling tools for every programming language environment, of varying
levels of complexity and customizability.  Your request roughly
corresponds to the fairly common question of how to profile a shell
script's execution, and if you search for that you can find dozens of
mostly ad-hoc but useful answers people have provided in the past.

https://stackoverflow.com/questions/5014823/how-to-profile-a-bash-shell-script-slow-startup
https://unix.stackexchange.com/questions/39644/how-can-i-profile-a-shell-script

(Some of these use more recent bash features to add nicer functionality.)

-- 
Seth David Schoen <schoen@loyalty.org>      |  Qué empresa fácil no pensar
     http://www.loyalty.org/~schoen/        |  en un tigre, reflexioné.
                                            |        -- Borges, "El Zahir"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]