Bash shell script for 100 iterations and log time taken

后端 未结 5 1294
长发绾君心
长发绾君心 2020-12-15 07:27

How do I run 100 iterations using a bash shell script? I want to know how long it will take to execute one command (start and end time). I want to keep track which iteratio

相关标签:
5条回答
  • 2020-12-15 07:42

    You can try with:

    for i in {1..100}; do time some_script.sh; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'
    

    Explanation

    • The for loop runs some_script.sh 100 times, measuring its execution time with time
    • The stderr of the for loop is redirected to stdout, this is to capture the output of time so we can grep it
    • grep ^real is to get only the lines starting with "real" in the output of time
    • sed is to delete the beginning of the line up to minutes part (in the output of time)
    • For each line, awk adds to the sum, so that in the end it can output the average, which is the total sum, divided by the number of input records (= NR)

    Limitations

    The snippet assumes that the running time of some_script.sh is less than 1 minute, otherwise it won't work at all. Depending on your system, the time builtin might work differently. Another alternative is to use the time command /usr/bin/time instead of the bash builtin.

    Note: This script was extracted from here.

    0 讨论(0)
  • 2020-12-15 07:44

    This script is based on the answer from @marian0, but there's no limitation to the running time. Name it timeit.sh and then do ./timeit.sh 10 ./script to run ./script 10 times and print the average time. Additional arguments can be added as desired.

    #!/bin/bash
    
    for i in `seq 1 $1`; do
        time "${@:2}"
    done 2>&1 |\
        grep ^real |\
        sed -r -e "s/.*real\t([[:digit:]]+)m([[:digit:]]+\.[[:digit:]]+)s/\1 \2/" |\
        awk '{sum += $1 * 60 + $2} END {print sum / NR}'
    
    
    0 讨论(0)
  • You may use seq in iteration as well:

    for i in `seq 1 100`; do ... done
    
    0 讨论(0)
  • 2020-12-15 07:49

    The following script shows one way to do it.

    #!/usr/bin/bash
    for i in {1..100} ; do
        echo =============================
        echo "Number $i: $(date +%Y-%m-%d-%H:%M:%S)"
        ( time ( echo $i ; sleep 1 ) ) 2>&1 | sed 's/^/   /'
    done | tee timing.log
    

    It uses the bash range feature to run 100 iterations of the loop, outputting the loop counter and date.

    It then times your command (echo $i ; sleep 1 in this case) and combines standard output and error before nicely formatting it, and sending it to both the terminal and a log file for later analysis.

    A smaple run with five iterations:

    pax> testprog.sh
    =============================
    Number 1: 2010-09-16-13:44:19
       1
       real 0m1.063s
       user 0m0.077s
       sys  0m0.015s
    =============================
    Number 2: 2010-09-16-13:44:20
       2
       real 0m1.056s
       user 0m0.030s
       sys  0m0.046s
    =============================
    Number 3: 2010-09-16-13:44:21
       3
       real 0m1.057s
       user 0m0.046s
       sys  0m0.030s
    =============================
    Number 4: 2010-09-16-13:44:22
       4
       real 0m1.057s
       user 0m0.061s
       sys  0m0.031s
    =============================
    Number 5: 2010-09-16-13:44:23
       5
       real 0m1.057s
       user 0m0.046s
       sys  0m0.015s
    
    0 讨论(0)
  • 2020-12-15 07:58
    for ((i = 1; i <= 100; i++)); do
        echo "--- Iteration #$i: $(date) ---"
        time command1
    done 2>&1 | tee timing.log
    

    There's a lot going on here. What's happening?

    1. The for loop iterates from 1 to 100 using C-style syntax.
    2. The $i in the echo printout prints the current iteration number.
    3. $(date) inserts a timestamp into each printout.
    4. The time command runs a command and prints how long it took to execute.
    5. The output from everything inside of the loop is piped to tee, which saves a copy to timing.log.
    6. The 2>&1 redirects stderr to stdout so that the log file will contain both regular output and error messages.
    0 讨论(0)
提交回复
热议问题