LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2010, 09:59 AM   #1
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Rep: Reputation: 110Reputation: 110
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
 
Old 05-30-2010, 11:16 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by lucmove View Post
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.
 
Old 05-30-2010, 11:38 AM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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

Last edited by bgeddy; 05-30-2010 at 12:24 PM. Reason: Missed something
 
Old 05-30-2010, 03:05 PM   #4
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Original Poster
Rep: Reputation: 110Reputation: 110
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.
 
Old 05-30-2010, 03:10 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by lucmove View Post
...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
 
Old 05-31-2010, 01:17 PM   #6
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Original Poster
Rep: Reputation: 110Reputation: 110
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
time function in kernel space Gourab Das Linux - Newbie 1 01-26-2010 02:27 PM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM
time() function not returning jiml8 Programming 5 04-09-2008 01:03 PM
Is a script, run at boot time from init.d, run with root authority? tmbrwolf53 Linux - Server 2 03-31-2007 08:15 PM
How do I run a function within a function arguement? C/C++ RHLinuxGUY Programming 12 05-19-2006 02:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration