bash script execution time in milliseconds on mac OSX - bash

I'm trying to find execution time of a script in milliseconds.
I'm running a bunch of queries which are basically placed inside a for loop. Each query here is a actually a script. I want to find the time taken for each query.
Can someone help me to find the execution time in milliseconds please.
I have tried a bunch approaches with the 'time' and 'date' commands but couldn't land on a precise solution.
Thanks.

You could try:
time -v script_name
/usr/bin/time -v script_name # if mac delivers the program
https://coderwall.com/p/er_zca
the equivalent of strace for macos which should allow you to see the time of system calls between each of them.

Related

How do I use AppleScript to time program execution

I would like to use AppleScript to batch run a bunch of programs and get the time that each one ran.
Example: From the command line I would type time ls like this:
$ time ls
Applications
bin
net
Library
... etc
real 0m0.013s
user 0m0.002s
sys 0m0.006s
but when I try to use apple script and put the results into a variable, it completely ignores the time output.
Example:
set result to do shell script "time ls"
returns
Applications
bin
net
Library
... etc
with no mention of the time execution time anywhere.
Even making this more simple:
set result to do shell script "time"
returns
""
How do I get the time of execution back from AppleScript?
p.s. I'm not actually trying to time ls. I want to time a bunch of custom created programs, but ls makes simple example.
Use the exec 2>&1 command to redirect the STDERR to STDOUT, like this:
do shell script "exec 2>&1; time ls"

Get execution time information for large number of bash scripts

Given a project that consists of large number of bash scripts that are being launched from crontab periodically how can one track execution time of each script?
There is straightforward approach to edit each of those file by adding date
But what I really want is some kind of daemon that could track execution time and submit results to somewhere several times a day.
So the question is:
Is it possible to gather information about execution time of 200 bash scripts without editing each of them?
time module considered as fallback solution, if nothing better could be found
Depending on your systems cron implementation you may define the log-levels of the cron daemon. For ubuntus default vixie-cron setting log-level will log start and end of a job-execution which can then be analyzed.
On current LTS Ubuntu it works defining the log-level in /etc/init/cron
appending the -L 3 option to the exec line letting it look like:
exec cron -L 3
You could change your cron to run your scripts under time?
time scriptname
And pipe output to you logs.

unix application benchmarking through time utility by running app in parallel

i am trying to find the resources used by my application. i am using time utility advised here stackoverflow.com/questions/560089/unix-command-for-benchmarking-code-running-k-times
i used command as time (for i in $(seq 100); do ./mycode; done) as suggested by one of the answers.
problem: application run one by one but not n parallel. i need to run mycode 100/1000 times in parallel. any suggestion how to run application in parallel more than once. In other words how to run above command 100 times so 100X100 instances will be running at the same time.
I am also not able to place any format switch along with for loop.
i tried time -v (for i in $(seq 100); do ./mycode; done) for verbose output
note: I also tried /usr/bin/time and complete switch --verbose
EDIT: I changed my code as per instructions from Cyrus reply. simultaneous ruuning of mycode is solved. but still I am looking for my second question how to get verbose output of time utility by using -v or --verbose.

What happens while launching /bin/sh?

Suspecting /bin/sh launching taking considerable boot ticks on my embedded linux kit.
In the kernel, /bin/sh being launched from kernel_init().
Will it look for start-up scripts?
If so, what would be the name of the scripts?
How is possible to measure the boot-up time of /bin/sh?
To measure the run-time of a command, you can prefix it with the time command. In this case
time /bin/sh -c exit
would be useful for you. It starts /bin/sh, which then immediately runs the exit command, so the time you will get is the startup time plus the termination time of sh. Alternatively, you might be interested in the strace command, which prints all the system calls initiated by the program it runs. This is a way to guess if sh tries to open any files, through examinig stat and/or open calls. strace works in the same way as time. I hope it helps.
Regards

How to get the elapsed time in milliseconds in a bash script?

I tried using t1=$(date +%s%N) to get the time in nanoseconds, but I kept in getting this error:
./script.sh: line 10: 1292460931N: value too great for base (error token is "1292460931N")
I looked up online and it seems that you can use the "time" command, however I can't find a good example of using the time command. Any help would be appreciated :)
The date command you're using doesn't support %N so your output is literally 1292460931N. I tried it on Linux and it worked, but on FreeBSD I see the results you got. Run that date command in a shell and see what comes out. Is it possible you're using busybox? Its cut-down date command also omits %N but the version I just tried gave me 1292463535%N.
Okay, a couple of things here.
First, not a lot of systems can give you a time that actually accurate to nanoseconds anyway.
Now, using time, either as /usr/bin/time or the shell builtin (bash: help time) is very easy. If the command you want to time is foo1, then
$ time foo
will return the elapsed time as three lines on stderr
real 0m0.001s
user 0m0.000s
sys 0m0.000s
which you can use any way you like.
If you want to get a better, more accurate timing, execute the command many times. This can be as simple as writing a short loop
time for i in 0 1 2 3 4; do foo; done
will do foo five times and give you the total time. You probably want to do more than 5 iterations, so you'd probably want a counter and a while loop or the like.

Resources