How to add automatic benchmarking to your example

From KratosWiki
Revision as of 17:45, 27 November 2008 by Mossaiby (Talk | contribs)
Jump to: navigation, search

Contents

Automatic benchmarking

Kratos contains an automatic benchmarking feature which can be used to automatically check the status of nightly build, for example. To use this feature you need to import benchmarking module from kratos_root/benchmarking. To do this add the lines marked in bold to your code:

#################################################################
##################################################################
## ATTENTION: here the order is important

#including kratos path
kratos_libs_path = '../../../../libs' ##kratos_root/libs
kratos_applications_path = '../../../../applications' ##kratos_root/applications
kratos_benchmarking_path = '../../../../benchmarking' ##kratos_root/benchmarking

import sys
sys.path.append(kratos_libs_path)
sys.path.append(kratos_applications_path)
sys.path.append(kratos_benchmarking_path)

#importing Kratos main library
from Kratos import *
kernel = Kernel()   #defining kernel

#importing applications
import applications_interface
applications_interface.Import_IncompressibleFluidApplication = True
applications_interface.Import_PFEMApplication = True
applications_interface.ImportApplications(kernel, kratos_applications_path)

import benchmarking
## from now on the order is not anymore crucial
##################################################################
##################################################################

Specifying the values to be benchmarked

The principle of the benchmarking module is to first marking some values in your program which need to be examined, or some part of the code which needs to be checked for amount of the time it takes to run. Then, the program is run (using the benchmarking module) in a special mode, called benchmarking mode, and the required data are collected and stored in a file. These data are called the reference data, and will be used to verify the functionality of the program later. This also is done using the benchmarking module.

To specify a value for benchmarking, pass it to benchmarking.Output. The format of using benchmarking.Output is:

benchmarking.Output(Var, Label = "", AbsErr = None, RelErr = None)

In the above, Var is the variable you want to benchmark. This variable can either be a floating number, an integer number, a string, or even a boolean. The label is an string, which you can later use to identify the source of a difference, if something goes wrong. For floating point numbers and integers, you may specify the amount of the absolute or relative errors allowed. These default to None, which means that the values should be exactly the same every time the program runs. Please note that you cannot specify a relative error tolerance for zero reference data! The reason should be clear.

Determining if the program is running in benchmarking mode

Another useful function is benchmarking.InBenchmarkingMode, which tells if the program is being run in benchmarking mode. This is useful if you need to calculate some results only for benchmarking use:

if (benchmarking.InBenchmarkingMode()):
      calculate some results...
    benchmarking.Output(...)

You may note that the benchmarking.Output also uses this function to avoid any outputting of any benchmark data during a normal run.

Checking time consumed by some part of your code

The benchmarking module can also check the time consumed by some part of your code and see if it is running much faster or slower than the original run. The parts checked can also be nested. To use this feature, use benchmarking.StartTiming and benchmarking.StopTiming, as specified in the code below:

t = benchmarking.StartTiming()
  the code to be checked goes here...
benchmarking.StopTiming(t)

Getting notification of changes via email

The benchmarking module features a function which can be used to send an email, notifying a group of recipients from changes in benchmarked data and/or problems in them. The function can be used as:

benchmarking.NotifyViaEmail(Subject, Text, Recipients)

In the above code, Subject and Text are the subject of the email and the text of it. The recipients will always get the email from no-reply-kratos-benchmarking@cimne.upc.es, so they may need to add this address to their address book/white list to avoid its delivery to Spam/Bulk folder. The Recipients is a list of email addresses as strings, obviously.

Building the reference data

Building the reference data is very simple. To do this, just call benchmarking.BuildReferenceData like this:

benchmarking.BuildReferenceData(PathToThePythonScript, PathToTheReferenceDataFile)

This will invoke python to run the script and use grep to filter the benchmarking data into the specified reference data file. It is recommended that you use the above code from the same directory as the example itself, as the Kratos example are very sensitive to the path they are executed from. A systematic way to do this will be presented later in this article.

Running a benchmark

The way of running a benchmark is quite the same as making the reference data. To do this use the code below:

print benchmarking.RunBenchmark(PathToThePythonScript, PathToTheReferenceDataFile)

This will also run the example in benchmarking mode and filter the benchmarking data to a temporary file, name BenchTemp.txt. Then, the contents of this file will be checked against the specified reference data file, and any difference will be reported. If no differences is found, the function will return True.

Personal tools
Categories