Hanna Escalante
Hanna Escalante

Reputation: 17

Wrapper script that would record the execution time of another script with parameters

I have a wrapper script wrapper.py that would time the full execution of benchrun.py.

Let's say I run the benchrun.py using this command:

python benchrun.py --host {host} -f {testfile} -t {number of threads} -s {mongo shell path}

What should I put in the wrapper script to run the benchrun.py script and get the execution time?

Upvotes: 0

Views: 433

Answers (4)

iops
iops

Reputation: 60

First, benchrun.py

import datetime

print ("sleeping now...")
time.sleep(10)
print ("done!") 

wrapper:

import os
from datetime import datetime, timedelta

before = datetime.now()
os.system("python benchrun.py")
after = datetime.now()

print ("execution time: {0}".format(after - before))

Upvotes: 0

Alec
Alec

Reputation: 135

# timer.py
import time


def timer():
    def wrapper(f):
        def wrapped_f(*args, **kwargs):
            tic = time.perf_counter()  # more precise than '.clock'
            f(*args, **kwargs)
            toc = time.perf_counter()
            method_name = f.__name__
            print('{}: {:.2f}sec'.format(method_name, toc - tic))
        return wrapped_f
    return wrapper
# benchrun.py
from timer import timer


@timer
def benchrun():
    ...

Upvotes: 0

to-b
to-b

Reputation: 118

You need to take the time before and after the execution and reduce between them so that you get the execution time;

In the wrapper script:

import time

Before the execute:

before= time.clock()

After the execute:

after = time.clock()
totaltime = after-before

The totaltime is the execute time of benchrun.py script.

Upvotes: 0

iops
iops

Reputation: 60

Are you using os.system to call the benchrun.py? If so, just set datetime.now() in the begining and after return, and calculate the delta. It should work.

Upvotes: 0

Related Questions