The Benchmark module allows you to write easily benchmarks framework in a project for timing critical part and detect slow parts of code.

In addition it automatically creates data files of these benchmark, as well as a gnuplot file which can display the comparison curves of the benchmarks.\hypertarget{tutorial_benchmark_page_tutorial_benchmark_basic_usage}{}\section{Basic Usage}\label{tutorial_benchmark_page_tutorial_benchmark_basic_usage}
To create a basic benchmark, you have to follow these steps\+:

\begin{DoxyItemize}
\item Create a new benchmark \item Write the functions that wraps the functions you want to benchmark. \item Register these wrappers functions. \item Run the benchmark. \item Free the memory.\end{DoxyItemize}
Here is a basic example of benchmark which creates two functions that will be run. These functions just print a message.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <stdlib.h>}
\textcolor{preprocessor}{#include <stdio.h>}

\textcolor{preprocessor}{#include <\hyperlink{Eina_8h}{Eina.h}>}

\textcolor{keyword}{static}
\textcolor{keywordtype}{void} work1(\textcolor{keywordtype}{int} request)
\{
  printf (\textcolor{stringliteral}{"work1 in progress... Request: %d\(\backslash\)n"}, request);
\}

\textcolor{keyword}{static}
\textcolor{keywordtype}{void} work2(\textcolor{keywordtype}{int} request)
\{
  printf (\textcolor{stringliteral}{"work2 in progress... Request: %d\(\backslash\)n"}, request);
\}

\textcolor{keywordtype}{int} main()
\{
  \hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark} *test;
  \hyperlink{struct__Eina__Array}{Eina\_Array}     *ea;

  \textcolor{keywordflow}{if} (!\hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}())
    \textcolor{keywordflow}{return} EXIT\_FAILURE;

  test = \hyperlink{group__Eina__Benchmark__Group_gacd3e2dcff8292721754714125572936a}{eina\_benchmark\_new}(\textcolor{stringliteral}{"test"}, \textcolor{stringliteral}{"run"});
  \textcolor{keywordflow}{if} (!test)
    \textcolor{keywordflow}{goto} shutdown\_eina;

  \hyperlink{group__Eina__Benchmark__Group_gacd15985f85c6cbfe21f7c1c1902dd2b3}{eina\_benchmark\_register}(test, \textcolor{stringliteral}{"work-1"}, \hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}(work1), 200, 
      300, 10);
  \hyperlink{group__Eina__Benchmark__Group_gacd15985f85c6cbfe21f7c1c1902dd2b3}{eina\_benchmark\_register}(test, \textcolor{stringliteral}{"work-2"}, \hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}(work2), 100, 
      150, 5);

  ea = \hyperlink{group__Eina__Benchmark__Group_ga56b67e26be3cf49cccffa4c148148eaf}{eina\_benchmark\_run}(test);

  \hyperlink{group__Eina__Benchmark__Group_gac8f2877d714ab6fe90d0fa08f87567c5}{eina\_benchmark\_free}(test);
  \hyperlink{group__Eina__Main__Group_ga6741fcaba116e31e96ce14dcfa03f568}{eina\_shutdown}();

  \textcolor{keywordflow}{return} EXIT\_SUCCESS;

 shutdown\_eina:
  \hyperlink{group__Eina__Main__Group_ga6741fcaba116e31e96ce14dcfa03f568}{eina\_shutdown}();

  \textcolor{keywordflow}{return} EXIT\_FAILURE;
\}
\end{DoxyCode}


As \char`\"{}test\char`\"{}, \char`\"{}run\char`\"{} are passed to \hyperlink{group__Eina__Benchmark__Group_gacd3e2dcff8292721754714125572936a}{eina\+\_\+benchmark\+\_\+new()} and as the tests \char`\"{}work-\/1\char`\"{} and \char`\"{}work-\/2\char`\"{} are registered, the data files bench\+\_\+test\+\_\+run.\+work-\/1.\+data and bench\+\_\+test\+\_\+run.\+work-\/2.\+data will be created after the \hyperlink{group__Eina__Benchmark__Group_ga56b67e26be3cf49cccffa4c148148eaf}{eina\+\_\+benchmark\+\_\+run()} call. They contain four columns. The file bench\+\_\+test\+\_\+run.\+work-\/1.\+data contains for example\+:


\begin{DoxyCode}
\textcolor{preprocessor}{# specimen      experiment time starting time   ending time}
200     23632   2852446 2876078
210     6924    2883046 2889970
220     6467    2895962 2902429
230     6508    2908271 2914779
240     6278    2920610 2926888
250     6342    2932830 2939172
260     6252    2944954 2951206
270     6463    2956978 2963441
280     6347    2969548 2975895
290     6457    2981702 2988159
\end{DoxyCode}


The first column (specimen) is the integer passed to the work1() function when the test is run. The second column (experiment time) is the time, in nanosecond, that work1() takes. The third and fourth columnd are self-\/explicit.

You can see that the integer passed work1() starts from 200 and finishes at 290, with a step of 10. These values are computed withe last 3 values passed to \hyperlink{group__Eina__Benchmark__Group_gacd15985f85c6cbfe21f7c1c1902dd2b3}{eina\+\_\+benchmark\+\_\+register()}. See the document of that function for the detailed behavior.

The gnuplot file will be named bench\+\_\+test\+\_\+run.\+gnuplot. Just run\+:


\begin{DoxyCode}
gnuplot bench\_test\_run.gnuplot
\end{DoxyCode}


to create the graphic of the comparison curves. The image file is named output\+\_\+test\+\_\+run.\+png.\hypertarget{tutorial_benchmark_page_tutorial_benchmark_advanced_usage}{}\section{More Advanced Usage}\label{tutorial_benchmark_page_tutorial_benchmark_advanced_usage}
In this section, several test will be created and run. The idea is exactly the same than in the previous section, but with some basic automatic way to run all the benchmarks. The following code benchmarks some Eina converts functions, and some Eina containers types\+:


\begin{DoxyCode}
\textcolor{preprocessor}{#include <stdlib.h>}
\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <time.h>}

\textcolor{preprocessor}{#include <\hyperlink{Eina_8h}{Eina.h}>}

\textcolor{keyword}{static} \textcolor{keywordtype}{void} bench\_convert(\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark} *bench);
\textcolor{keyword}{static} \textcolor{keywordtype}{void} bench\_container(\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark} *bench);

\textcolor{keyword}{typedef} \textcolor{keyword}{struct }\_Benchmark\_Case Benchmark\_Case;

\textcolor{keyword}{struct }\_Benchmark\_Case
\{
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *bench\_case;
   void (*build)(\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark} *bench);
\};

\textcolor{keyword}{static} \textcolor{keyword}{const} Benchmark\_Case benchmarks[] = \{
  \{ \textcolor{stringliteral}{"Bench 1"}, bench\_convert \},
  \{ \textcolor{stringliteral}{"Bench 2"}, bench\_container \},
  \{ NULL,      NULL \}
\};

\textcolor{keyword}{static}
\textcolor{keywordtype}{void} convert1(\textcolor{keywordtype}{int} request)
\{
  \textcolor{keywordtype}{char} tmp[128];
  \textcolor{keywordtype}{int} i;

  srand(time(NULL));

  \textcolor{keywordflow}{for} (i = 0; i < request; ++i)
    \hyperlink{group__Eina__Convert__Group_gafe831a6e2a4017b2fae5c29d60d53586}{eina\_convert\_itoa}(rand(), tmp);
\}

\textcolor{keyword}{static}
\textcolor{keywordtype}{void} convert2(\textcolor{keywordtype}{int} request)
\{
  \textcolor{keywordtype}{char} tmp[128];
  \textcolor{keywordtype}{int} i;

  srand(time(NULL));

  \textcolor{keywordflow}{for} (i = 0; i < request; ++i)
    \hyperlink{group__Eina__Convert__Group_gae539636184d2c4e80c2e29bf26f40be1}{eina\_convert\_xtoa}(rand(), tmp);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
bench\_convert(\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark} *bench)
\{
  \hyperlink{group__Eina__Benchmark__Group_gacd15985f85c6cbfe21f7c1c1902dd2b3}{eina\_benchmark\_register}(bench, \textcolor{stringliteral}{"convert-1"}, 
      \hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}(convert1), 200, 400, 10);
  \hyperlink{group__Eina__Benchmark__Group_gacd15985f85c6cbfe21f7c1c1902dd2b3}{eina\_benchmark\_register}(bench, \textcolor{stringliteral}{"convert-2"}, 
      \hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}(convert2), 200, 400, 10);
\}

\textcolor{keyword}{static}
\textcolor{keywordtype}{void} array(\textcolor{keywordtype}{int} request)
\{
  \hyperlink{struct__Eina__Array}{Eina\_Array} *array;
  \hyperlink{group__Eina__Array__Group_gaf9af7cd8afa60d6b1cab805bd5b838aa}{Eina\_Array\_Iterator} it;
  \textcolor{keywordtype}{int} *data;
  \textcolor{keywordtype}{int} i;

  srand(time(NULL));

  array = \hyperlink{group__Eina__Array__Group_ga4a389204db37b44b31d29f581258ac7d}{eina\_array\_new}(64);

  \textcolor{keywordflow}{for} (i = 0; i < request; ++i)
    \{
      data = (\textcolor{keywordtype}{int} *)malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{int}));
      \textcolor{keywordflow}{if} (!data) \textcolor{keywordflow}{continue};
      *data = rand();
      \hyperlink{group__Eina__Array__Group_gaf2f4a5907f1826354977adbd63e3379a}{eina\_array\_push}(array, data);
    \}

  \hyperlink{group__Eina__Array__Group_ga810ed3618ca03f7d549a1bf2c33b2b6e}{EINA\_ARRAY\_ITER\_NEXT}(array, i, data, it)
    free(data);

  \hyperlink{group__Eina__Array__Group_ga97995d81ca401d71a2913a52f8b40c41}{eina\_array\_free}(array);
\}

static
\textcolor{keywordtype}{void} list(\textcolor{keywordtype}{int} request)
\{
  \hyperlink{struct__Eina__List}{Eina\_List} *l = NULL;
  \textcolor{keywordtype}{int} *data;
  \textcolor{keywordtype}{int} i;

  srand(time(NULL));

  \textcolor{keywordflow}{for} (i = 0; i < request; ++i)
    \{
      data = (\textcolor{keywordtype}{int} *)malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{int}));
      \textcolor{keywordflow}{if} (!data) \textcolor{keywordflow}{continue};
      *data = rand();
      l = \hyperlink{group__Eina__List__Group_ga9f35bfc7c5b798014c3e0a74bc6f39ad}{eina\_list\_prepend}(l, data);
    \}

  \textcolor{keywordflow}{while} (l)
    \{
      free(\hyperlink{group__Eina__List__Group_gaec845f0bfb16a14e8d7f0cd508ca7e9c}{eina\_list\_data\_get}(l));
      l = \hyperlink{group__Eina__List__Group_ga7edefe0975beecc76c02525fb9200f5a}{eina\_list\_remove\_list}(l, l);
    \}
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
bench\_container(\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark} *bench)
\{
  \hyperlink{group__Eina__Benchmark__Group_gacd15985f85c6cbfe21f7c1c1902dd2b3}{eina\_benchmark\_register}(bench, \textcolor{stringliteral}{"array"}, \hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}(array), 200, 
      300, 10);
  \hyperlink{group__Eina__Benchmark__Group_gacd15985f85c6cbfe21f7c1c1902dd2b3}{eina\_benchmark\_register}(bench, \textcolor{stringliteral}{"list"}, \hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}(list), 200, 30
      0, 10);
\}

\textcolor{keywordtype}{int} main()
\{
  \hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark} *test;
  \hyperlink{struct__Eina__Array}{Eina\_Array}     *ea;
  \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int}    i;

  \textcolor{keywordflow}{if} (!\hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}())
    \textcolor{keywordflow}{return} EXIT\_FAILURE;

  \textcolor{keywordflow}{for} (i = 0; benchmarks[i].bench\_case != NULL; ++i)
    \{
      test = \hyperlink{group__Eina__Benchmark__Group_gacd3e2dcff8292721754714125572936a}{eina\_benchmark\_new}(benchmarks[i].bench\_case, \textcolor{stringliteral}{"Benchmark example"});
      \textcolor{keywordflow}{if} (!test)
        \textcolor{keywordflow}{continue};

      benchmarks[i].build(test);

      ea = \hyperlink{group__Eina__Benchmark__Group_ga56b67e26be3cf49cccffa4c148148eaf}{eina\_benchmark\_run}(test);

      \hyperlink{group__Eina__Benchmark__Group_gac8f2877d714ab6fe90d0fa08f87567c5}{eina\_benchmark\_free}(test);
    \}

  \hyperlink{group__Eina__Main__Group_ga6741fcaba116e31e96ce14dcfa03f568}{eina\_shutdown}();

  \textcolor{keywordflow}{return} EXIT\_SUCCESS;
\}
\end{DoxyCode}


gnuplot can be used to see how are performed the convert functions together, as well as how are performed the containers. So it is now easy to see that the hexadecimal convert function is faster than the decimal one, and that arrays are faster than lists.

You can improve all that by executing automatically gnuplot in your program, or integrate the Eina benchmark framework in an autotooled project. See that \href{http://trac.enlightenment.org/e/wiki/AutotoolsIntegration#Benchmark}{\tt page} for more informations. 