Shell Script average runtime

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Shell Script average runtime
# 1  
Old 10-12-2011
Shell Script average runtime

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Make a bash script that calculates average runtime for the first two scripts you made. The average should be based on 100 runs of the scripts. For measuring the runtime you shall use the built in GNU time(1) function (/usr/bin/time). For calculating the average (by addition and floatdivition) use bc(1).


2. Relevant commands, code, scripts, algorithms:
bash
/usr/bin/time
bc

return values from the built in time:

0.00user 0.01system 0:00.11elapsed 13%CPU (0avgtext+0avgdata 0maxresident)k0inputs+0outputs (0major+529minor)pagefaults 0swaps

3. The attempts at a solution (include all code and scripts):

script 1
Code:
#!/bin/bash
for I in {1..6}
do
        echo "downloading 0$I.html"
        wget filefromurlhere    <-(not adding this as it requires a password and stuff)
 echo "deleting 0$I.html"
        rm 0$I.html
done

script2
Code:
#!/bin/bash
 for I in {1..6}
do
        echo "downloading 0${I}.html"
        wget "file from url here"
        wait
        echo "deleting 0${I}.html"
 rm "0${I}.html"
 done

script3 for average runtime.
Code:
#!/bin/bash
 
#/usr/bin/time
for I in {1.100}
do
        echo $(time script1.sh 2>&1)+
done | sed 's/+$//\n/' | bc


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Norwegian University of Science and Technology NTNU, Trondheim, Norway, Svein Erik Bratsberg, TDT4186 Operativsystemer, h t t p : / / w w w . i d i . n t n u . n o / e m n e r / t d t 4 1 8 6 / (can't post urls, remove spaces ;D )

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).


I'm in need of the elapsed return value from the time (atleast that's what makes sense to me), added up for 100 run throughs and divided by 100 to find the average time, using bc to do it all. just not sure how to get exactly those numbers from all the junk I get returned with the built in time function.


I know my 3rd script might be far off, but I'm a real newbie to linux scripting Smilie

---------- Post updated at 10:54 PM ---------- Previous update was at 06:00 PM ----------

Well I found I could use awk like this, but I really need it to do the calculations in bc. can I just replace awk with bc in this and have it do what it's supposed to? D:

Code:
usr/bin/time -p {myScript} | grep elapsed | awk '{print $2}' >> datafile.dat
awk 'BEGIN {t=0.0;c=0;a=0.0;} {t+=$1; c++;} END {printf("Total %f Count %d Avg %f\n", t, c, t/c);}' < datafile.dat

---------- Post updated 10-12-11 at 06:06 AM ---------- Previous update was 10-11-11 at 10:54 PM ----------

I'll update in a new post instead of editing, I've had a pretty major breakthrough, just being stuck trying to get my stdin into my calculations.

script1
Code:
!#/ in/bash
for I in {1..6}
do
wget --quiet myUrl/file$I.html
done
rm 0{1..6}.html


script2
Code:
#!/bin/bash
for I in {1..6}
do
wget  --quiet myUrl/file$I.html &
done
wait
rm 0{1..6}.html



script3 for runtime calculations
Code:
#!/bin/bash
for I in {1..5}
do
/usr/bin/time -f "%E (elapsed)" ~/script1.sh 2>&1
echo 'x+stdout' <--- how to get the value of stdout for calculations?
done
echo 'scale=20;x/100' | bc
echo "gjennomsnittstid for skript1a er $x"
 
for I in {1..5}
do
/usr/bin/time -f "%E (elapsed)" ~/script2.sh 2>&1
echo 'y+stdout' | bc <--- how to get the value of stdout for calculations?
done
echo 'scale=20;y/100' | bc

so yeh. I've rerouted the elapsed time from my time call into stdout, but how do I get it back so I can calculate on it?

Last edited by navlelo; 10-12-2011 at 11:58 AM..
# 2  
Old 10-12-2011
redirect the output of 'done' into 'bc' with a pipe. That's valid for entire loops and such in the bourne shell, not just individual commands.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-12-2011
ok, not really got a clue how, but thanks. trying to look into it Smilie
# 4  
Old 10-12-2011
It's simpler than whatever you're thinking. I only mean exactly what I'm saying:
Code:
for I in {1..5}
do
/usr/bin/time -f "%E (elapsed)" ~/script2.sh 2>&1
done | program1 | program2 ...

You'll need to organize the text somehow before sending it into bc, but not knowing what your 'time' program prints, I can't say how.

awk would probably be better than bc, but if they insist on you using the worst program for the job... Smilie
# 5  
Old 10-12-2011
well with that format I print 0:00.00elapsed , asuming minutes:seconds.tenth of a second. I'd be interested in adding up all the seconds.tenths of seconds during the loop, then averaging by dividing on number of runs. supposed to run it 100 times, but my test code runs 5 for simplicity till I get it going.
# 6  
Old 10-12-2011
Does it really print that? Or does it actually print "0:00.00 (Elapsed)"? Be specific, if we make a program for the wrong input it won't give the right output.

You can probably make that simpler, by just feeding it "%F" instead of "%F (elapsed)", so it prints 0:00.00 per loop.

Then feed it through another loop so you can transform it into an expression to feed into bc.

Here's an incomplete example:
Code:
(

echo "(" # Start of bracket

while whatever
do
...
done | while IFS=":." read MIN SEC TENTH
do
        echo "( ( $MIN * 60 ) * $SEC) +"
done

echo "0 )" # End of bracket.  So we get ( A + B + C + D + 0 )
echo "/ 100" # Divide the whole mess by 100
 ) | bc


Last edited by Corona688; 10-12-2011 at 01:31 PM.. Reason: missing (. whoops!
# 7  
Old 10-12-2011
0:00.05 (elapsed) indeed, forgot the space there. changed it to "%E" to only get the numbers.

---------- Post updated at 11:42 AM ---------- Previous update was at 11:22 AM ----------

Code:
#!/bin/bash
(
echo "("
for I in {1..5}
do
/usr/bin/time -f "%E"  ~/oblig1/oblig1a.sh 2>&1
done | while IFS=":." read MIN SEC TENTH
do
echo "( ( $MIN * 60) * $SEC ) +"
done
echo "0 )"
echo "/ 100"
) | bc

getting standard_in errors tho, syntax error and variable occurances of illegal character. :/

Code:
(standard_in) 1689: illegal character: L
(standard_in) 1689: syntax error
(standard_in) 1689: illegal character: K
(standard_in) 1689: syntax error
(standard_in) 1689: syntax error
(standard_in) 1690: illegal character: S
(standard_in) 1690: syntax error
(standard_in) 1691: syntax error
(standard_in) 1692: illegal character: K
(standard_in) 1692: syntax error
(standard_in) 1693: syntax error
(standard_in) 1694: syntax error
(standard_in) 1694: syntax error
(standard_in) 1695: syntax error
(standard_in) 1696: syntax error
(standard_in) 1696: syntax error
(standard_in) 1696: syntax error
(standard_in) 1697: illegal character: L
(standard_in) 1697: illegal character: \370
(standard_in) 1697: syntax error
(standard_in) 1697: syntax error
(standard_in) 1698: syntax error
(standard_in) 1698: syntax error
(standard_in) 1699: illegal character: H
... and the list goes on for ages

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. Shell Programming and Scripting

Shell or awk script to compute average of all the points within a circle

HI Help, I have a file which looks like below --- Input file ---> 1970113.00000 3460.00000 1.09516 1970116.00000 3791.00000 1.06350 1970120.00000 4120.00000 1.07588 1970115.00000 4450.00000 1.09591 1970116.00000 4780.00000 1.09965 1970120.00000 5109.00000 1.06733 ... (7 Replies)
Discussion started by: Indra2011
7 Replies

3. Shell Programming and Scripting

shell script for finding average runtime of other script

so I've made a shell script that downloads 6 files in succession from a given url, then deletes them. Now I want to time the script, and the average time it uses by running it ~100 times. My problem is tho, how do I store the time it takes for each run through of the script? I know time writes to... (3 Replies)
Discussion started by: navlelo
3 Replies

4. Shell Programming and Scripting

Shell Runtime Statistics

Hi, I am trying to capture runtime stats of a shell script (c shell). Are there system variables to call? Or should I create a date variable at the start of the script and at the end of the script? I am trying to capture the time if the script stops or ends with error. Please help. ... (4 Replies)
Discussion started by: CKT_newbie88
4 Replies

5. Shell Programming and Scripting

passing runtime arguments to a shell script...

hi I am new to shell programming.....my question is while running one of my shell program it stops in between to accept input from the user and proceeds furthur after giving input....I want to know whether I can set this input through some files so that the shell acript reads the input from the... (10 Replies)
Discussion started by: santy
10 Replies

6. UNIX for Advanced & Expert Users

Allocate memory for a shell script in Aix at runtime-urgent critical

How to allocate memory for a shell script on aix box at the time of execution i.e at runtime Are there any commands for AIX in specific Thanks in Advance (1 Reply)
Discussion started by: aixjadoo
1 Replies

7. UNIX for Dummies Questions & Answers

allocate memory for a shell script at runtime--urgent critical help!!!

How to allocate memory for a shell script on aix box at the time of execution i.e at runtime Are there any commands for AIX in specific Thanks in Advance (3 Replies)
Discussion started by: aixjadoo
3 Replies

8. AIX

allocate memory for shell script at runtime during execution--urgent critical help!!

How to allocate memory for a shell script on aix box at the time of execution i.e at runtime Are there any commands for AIX in specific Thanks in Advance (1 Reply)
Discussion started by: aixjadoo
1 Replies

9. Shell Programming and Scripting

pass runtime parm to at -f shell script

Hi Folks... I am using a ksh script to submit the at command to run a shell script for immediate execution. The shell script requries 1 parameter. Command in the script is at -m -f $EXE_DIR/process_server.sh $START_TIME $DB_NAME where START_TIME=now and DB_NAME= tnsname of Oracle... (1 Reply)
Discussion started by: island360
1 Replies

10. Shell Programming and Scripting

korn shell version at runtime?

How can I check what kornshell version I am using at runtime from within a kornshell script? (3 Replies)
Discussion started by: qanda
3 Replies
Login or Register to Ask a Question