Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / benchmark / benchmark.cc
1 #include <sys/time.h>
2 #include "pbd/compose.h"
3 #include "canvas/types.h"
4 #include "canvas/canvas.h"
5 #include "benchmark.h"
6
7 using namespace std;
8 using namespace ArdourCanvas;
9
10 double
11 double_random ()
12 {
13         return ((double) rand() / RAND_MAX);
14 }
15
16 ArdourCanvas::Rect
17 rect_random (double rough_size)
18 {
19         double const x = double_random () * rough_size / 2;
20         double const y = double_random () * rough_size / 2;
21         double const w = double_random () * rough_size / 2;
22         double const h = double_random () * rough_size / 2;
23         return Rect (x, y, x + w, y + h);
24 }
25
26 Benchmark::Benchmark (string const & session)
27         : _iterations (1)
28 {
29         string path = string_compose ("../../libs/canvas/benchmark/sessions/%1.xml", session);
30         _canvas = new ImageCanvas (new XMLTree (path), Duple (4096, 4096));
31 }
32
33 void
34 Benchmark::set_iterations (int n)
35 {
36         _iterations = n;
37 }
38
39 /** @return wallclock time in seconds */
40 double
41 Benchmark::run ()
42 {
43         timeval start;
44         gettimeofday (&start, 0);
45
46         for (int i = 0; i < _iterations; ++i) {
47                 do_run (*_canvas);
48         }
49
50         timeval stop;
51         gettimeofday (&stop, 0);
52
53         finish (*_canvas);
54
55         int sec = stop.tv_sec - start.tv_sec;
56         int usec = stop.tv_usec - start.tv_usec;
57         if (usec < 0) {
58                 --sec;
59                 usec += 1e6;
60         }
61
62         return sec + ((double) usec / 1e6);
63 }