LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to run 'time' on a function? (https://www.linuxquestions.org/questions/programming-9/how-to-run-time-on-a-function-811079/)

lucmove 05-30-2010 09:59 AM

How to run 'time' on a function?
 
Code:

#!/bin/ksh

(...)

# FILL UP PARTITION
diskfillerfunction  ()          {
        for i in $(seq 1000); do echo -n 'FILLER ' >> "${MOUNTPOINT}/filler-1.txt"; done
        local counter=2
        while  (( 1 )); do
                cp -f "${MOUNTPOINT}/filler-1.txt" "${MOUNTPOINT}/filler-${counter}.txt" || break
                counter=$((counter+1))
        done
       
        echo "${FILESYSTEM} filled up, $(($counter-1)) filler files created."
        echo "${FILESYSTEM} filled up, $(($counter-1)) filler files created." >> $OUTPUTFILE
}
echo
echo "Test 15: fill up partition"
echo "Test 15: fill up partition" >> $OUTPUTFILE
/usr/bin/time -o ${OUTPUTFILE} -a -f '%C\n${FILESYSTEM}\n%E %PCPU\n' diskfillerfunction

(...)

Test 15: fill up partition
/usr/bin/time: cannot run diskfillerfunction: No such file or directory

-----
I am obviously omitting (...) a lot of the code, but the relevant part is all there. How do I 'time' a function?

TIA

Sergei Steshenko 05-30-2010 11:16 AM

Quote:

Originally Posted by lucmove (Post 3986280)
Code:

#!/bin/ksh

(...)

# FILL UP PARTITION
diskfillerfunction  ()          {
        for i in $(seq 1000); do echo -n 'FILLER ' >> "${MOUNTPOINT}/filler-1.txt"; done
        local counter=2
        while  (( 1 )); do
                cp -f "${MOUNTPOINT}/filler-1.txt" "${MOUNTPOINT}/filler-${counter}.txt" || break
                counter=$((counter+1))
        done
       
        echo "${FILESYSTEM} filled up, $(($counter-1)) filler files created."
        echo "${FILESYSTEM} filled up, $(($counter-1)) filler files created." >> $OUTPUTFILE
}
echo
echo "Test 15: fill up partition"
echo "Test 15: fill up partition" >> $OUTPUTFILE
/usr/bin/time -o ${OUTPUTFILE} -a -f '%C\n${FILESYSTEM}\n%E %PCPU\n' diskfillerfunction

(...)

Test 15: fill up partition
/usr/bin/time: cannot run diskfillerfunction: No such file or directory

-----
I am obviously omitting (...) a lot of the code, but the relevant part is all there. How do I 'time' a function?

TIA

Well, the 'date' command still works and can display current time in seconds and even nanoseconds, so by adding a couple of variables you can calculate the time it takes to do something.

bgeddy 05-30-2010 11:38 AM

Rather than using the time application why not use the Bash built-in time command when timing a Bash function? This will hopefully give you ideas how to implement this:
Code:

#!/bin/bash
function mywait(){
        sleep 10
}
time mywait) 2>waitfile

This will produce the results from a 10 second wait in a file named waitfile.

Edit: Sorry - I just noticed you are using the ksh shell so the time builtin may not work

Edit2: Just tried this in ksh and it sems to work - the amended script is here:
Code:


#!/bin/ksh

mywait()
{
        sleep 10
}

(time mywait) 2>waitfileksh


lucmove 05-30-2010 03:05 PM

Problems with using the shell built-in:

I like /usr/bin/time because it has nice output formatting options and a very easy redirection mechanism.

1) Bash has the formatting options, but I really would rather use ksh (it's mksh, by the way).

2) The 'time' built-in in mksh does not include average CPU load.

3) Both Bash and mksh output 'time' to standard error, which I don't know how to write into a file. I can do this:

Code:

# { time diskfillerfunction; } 2> ./output.txt
But that output isn't good enough to me. How do I pipe stderr through 'cut' for example? I can't seem to figure that out.

Sergei Steshenko 05-30-2010 03:10 PM

Quote:

Originally Posted by lucmove (Post 3986562)
...How do I pipe stderr through 'cut' for example? I can't seem to figure that out.


http://wiki.bash-hackers.org/mirroring/bashfaq/047

lucmove 05-31-2010 01:17 PM

I solved the problem. Instead of using a function to fill up the file system, I am using an external file:

Code:

/usr/bin/time -o ${OUTPUTFILE} -a -f '%C\n${FILESYSTEM}\n%E %PCPU\n' ksh ./diskfiller.sh
Whatever. It works.

Thanks for all the help.


All times are GMT -5. The time now is 02:20 AM.