Content  


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:

Example 1
  1. ...
  2. #include "viennaprofiler/mysqldb.hpp"
  3. #include "viennaprofiler/timer/precisetimer.hpp"
  4. #include "viennaprofiler/host.hpp"
  5. #include "viennaprofiler/profiler.hpp"
  6. ...
  7. using namespace ViennaProfiler;
  8. ...
  9. int main(int argc, char **argv) {
  10. /* Establish a connection to a MySQL-database */
  11. MySQLDB dbConn("db", "server", "user", "pwd");
  12. /* Create an appropriate Timer-class */
  13. PreciseTimer timer;
  14. /* Create a Profiler */
  15. Profiler<MySQLDB, PreciseTimer> myProfiler(dbConn, timer, "host");
  16. const long runs = 100000;
  17. /* Set up the basic properties of a test */
  18. myProfiler.setCollection("MyCollection");
  19. myProfiler.setFunction("MyFunction");
  20. /* Start a specific test */
  21. myProfiler.setImplementation("MyImpl");
  22. myProfiler.setOperations(1000);
  23. myProfiler.setSourceCode("MySourceCode");
  24. myProfiler.addParameter("Param1","Value1");
  25. myProfiler.addParameter("Param2","Value2");
  26.  
  27. /*****************************/
  28. /* Run the test 'runs' times */
  29. /*****************************/
  30. myProfiler.start(runs);
  31. for(long i = 0; i < runs; i++)
  32. myFunction(...); // <--- This function is benchmarked!
  33. myProfiler.stop();
  34. /* Send the result to the database */
  35. myProfiler.send();
  36.  
  37. /* Some more tests here if required */
  38. }

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.

Example 2
  1. /* UPDATE Implementation */
  2. myProfiler.setImplementation("MyNewImpl");
  3.  
  4. /* UPDATE "Param1" */
  5. myProfiler.addParameter("Param1","NewValue1");
  6. /* ADD additional parameter */
  7. myProfiler.addParameter("Param3","Value3");
  8. /* REMOVE old parameter */
  9. myProfiler.removeParameter("Param2");
  10.  
  11. /*****************************/
  12. /* Run the test 'runs' times */
  13. /*****************************/
  14. myProfiler.start(runs);
  15. for(long i = 0; i < runs; i++)
  16. myFunction(...); // <--- This function is benchmarked!
  17. /* Instead of stop() and send() submit() is used */
  18. 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.

Example 3
  1. struct Functor {
  2. void operator() () {
  3. /* code to be benchmarked here */
  4. }
  5. };
  6.  
  7. /* run benchmark for Functor */
  8. myProfiler.run(Functor(), runs); // <--- Functor()() is benchmarked!

Example 4

In the case that an benchmarking functionality already exists, it can be reused easily:

Example 4
  1. /* Measured values are gathered externally */
  2. measuredTime = myGetExecTime(); // in seconds
  3. const long runs = 100;
  4.  
  5. /*************************************************************/
  6. /* set the measured time and send the result to the database */
  7. /*************************************************************/
  8. myProfiler.external_timing(measuredTime, runs);
  9. 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.

Example 5
  1. #include "viennaprofiler/filedb.hpp";
  2. ...
  3. FileDB file("example.sql");
  4. Profiler<FileDB, PreciseTimer> myProfiler(file, timer, "host");
  5.  
  6. /* continue as before */

Example 6

If necessary, a benchmark test can also be paused and continued afterwards.

Example 6
  1. /*******************/
  2. /* start the timer */
  3. /*******************/
  4. myProfiler.start(runs);
  5. for(long i = 0; i < runs/2; i++)
  6. myFunction(...);
  7. /**********************************/
  8. /* stop the timer to make a pause */
  9. /**********************************/
  10. myProfiler.pause();
  11.  
  12. doingSomethingDifferent();
  13.  
  14. /**********/
  15. /* resume */
  16. /**********/
  17. myProfiler.resume();
  18. for(long i = 0; i < runs/2; i++)
  19. myFunction(...);
  20.  
  21. /******************************************************/
  22. /* stop the timer and send the result to the database */
  23. /******************************************************/
  24. myProfiler.submit();