Benchmarking at source code level
Example 1
Here a first example using the ViennaProfiler-API is shown. The function myFunction(...) is benchmarked. Here an approach with a for-loop is chosen:
... #include "viennaprofiler/mysqldb.hpp" #include "viennaprofiler/timer/precisetimer.hpp" #include "viennaprofiler/host.hpp" #include "viennaprofiler/profiler.hpp" ... using namespace ViennaProfiler; ... int main(int argc, char **argv) { /* Establish a connection to a MySQL-database */ MySQLDB dbConn("db", "server", "user", "pwd"); /* Create an appropriate Timer-class */ PreciseTimer timer; /* Create a Profiler */ Profiler<MySQLDB, PreciseTimer> myProfiler(dbConn, timer, "host"); const long runs = 100000; /* Set up the basic properties of a test */ myProfiler.setCollection("MyCollection"); myProfiler.setFunction("MyFunction"); /* Start a specific test */ myProfiler.setImplementation("MyImpl"); myProfiler.setOperations(1000); myProfiler.setSourceCode("MySourceCode"); myProfiler.addParameter("Param1","Value1"); myProfiler.addParameter("Param2","Value2"); /*****************************/ /* Run the test 'runs' times */ /*****************************/ myProfiler.start(runs); for(long i = 0; i < runs; i++) myFunction(...); // <--- This function is benchmarked! myProfiler.stop(); /* Send the result to the database */ myProfiler.send(); /* Some more tests here if required */ }
Example 2
It is cumbersome to use myProfiler.stop() respectively myProfiler.send(), so calling these two functions in this order can be abbreviated by a single call to myProfiler.submit(). Furthermore, every property of a test can be updated (e.g.: myProfiler.setCollection(...), myProfiler.setFunction(...), ...). Parameters can be updated and removed arbitrarily.
/* UPDATE Implementation */ myProfiler.setImplementation("MyNewImpl"); /* UPDATE "Param1" */ myProfiler.addParameter("Param1","NewValue1"); /* ADD additional parameter */ myProfiler.addParameter("Param3","Value3"); /* REMOVE old parameter */ myProfiler.removeParameter("Param2"); /*****************************/ /* Run the test 'runs' times */ /*****************************/ myProfiler.start(runs); for(long i = 0; i < runs; i++) myFunction(...); // <--- This function is benchmarked! /* Instead of stop() and send() submit() is used */ myProfiler.submit();
Example 3
The amount of code needed for benchmarking can be further reduced by using functors without arguments. In this way, both functions and objects with overloaded operator() can be supplied.
struct Functor { void operator() () { /* code to be benchmarked here */ } }; /* run benchmark for Functor */ myProfiler.run(Functor(), runs); // <--- Functor()() is benchmarked!
Example 4
In the case that an benchmarking functionality already exists, it can be reused easily:
/* Measured values are gathered externally */ measuredTime = myGetExecTime(); // in seconds const long runs = 100; /*************************************************************/ /* set the measured time and send the result to the database */ /*************************************************************/ myProfiler.external_timing(measuredTime, runs); myProfiler.submit();
Example 5
Not every host is connected to a network or to a database. In such a case, FileDB offers a workaround solution. The third constructor argument is then ignored.
#include "viennaprofiler/filedb.hpp"; ... FileDB file("example.sql"); Profiler<FileDB, PreciseTimer> myProfiler(file, timer, "host"); /* continue as before */
Example 6
If necessary, a benchmark test can also be paused and continued afterwards.
/*******************/ /* start the timer */ /*******************/ myProfiler.start(runs); for(long i = 0; i < runs/2; i++) myFunction(...); /**********************************/ /* stop the timer to make a pause */ /**********************************/ myProfiler.pause(); doingSomethingDifferent(); /**********/ /* resume */ /**********/ myProfiler.resume(); for(long i = 0; i < runs/2; i++) myFunction(...); /******************************************************/ /* stop the timer and send the result to the database */ /******************************************************/ myProfiler.submit();