1tutorial_benchmark_page(3) Library Functions Manual tutorial_benchmark_page(3)
2
3
4
6 tutorial_benchmark_page - Benchmark Tutorial The Benchmark module
7 allows you to write easily benchmarks framework in a project for timing
8 critical part and detect slow parts of code.
9
10 In addition it automatically creates data files of these benchmark, as
11 well as a gnuplot file which can display the comparison curves of the
12 benchmarks.
13
15 To create a basic benchmark, you have to follow these steps:
16
17 · Create a new bechmark
18 · Write the functions that wraps the the functions you want to
19 bechmark.
20 · Register these wrappers functions.
21 · Run the benchmark.
22 · Free the memory.
23 Here is a basic example of bechmark which creates two functions that
24 will be run. These functions just print a message.
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #include <Eina.h>
29
30 static
31 void work1(int request)
32 {
33 printf ('work1 in progress... Request: %d0, request);
34 }
35
36 static
37 void work2(int request)
38 {
39 printf ('work2 in progress... Request: %d0, request);
40 }
41
42 int main()
43 {
44 Eina_Benchmark *test;
45 Eina_Array *ea;
46
47 if (!eina_init())
48 return EXIT_FAILURE;
49
50 test = eina_benchmark_new('test', 'run');
51 if (!test)
52 goto shutdown_eina;
53
54 eina_benchmark_register(test, 'work-1', EINA_BENCHMARK(work1), 200, 300, 10);
55 eina_benchmark_register(test, 'work-2', EINA_BENCHMARK(work2), 100, 150, 5);
56
57 ea = eina_benchmark_run(test);
58
59 eina_benchmark_free(test);
60 eina_shutdown();
61
62 return EXIT_SUCCESS;
63
64 shutdown_eina:
65 eina_shutdown();
66
67 return EXIT_FAILURE;
68 }
69 As 'test', 'run' are passed to eina_benchmark_new() and as the tests
70 'work-1' and 'work-2' are registered, the data files
71 bench_test_run.work-1.data and bench_test_run.work-2.data will be
72 created after the eina_benchmark_run() call. They contain four columns.
73 The file bench_test_run.work-1.data contains for example:
74 # specimen experiment time starting time ending time
75 200 23632 2852446 2876078
76 210 6924 2883046 2889970
77 220 6467 2895962 2902429
78 230 6508 2908271 2914779
79 240 6278 2920610 2926888
80 250 6342 2932830 2939172
81 260 6252 2944954 2951206
82 270 6463 2956978 2963441
83 280 6347 2969548 2975895
84 290 6457 2981702 2988159
85 The first column (specimen) is the integer passed to the work1()
86 function when the test is run. The second column (experiment time) is
87 the time, in nanosecond, that work1() takes. The third and fourth
88 columnd are self-explicit.
89 You can see that the integer passed work1() starts from 200 and
90 finishes at 290, with a step of 10. These values are computed withe
91 last 3 values passed to eina_benchmark_register(). See the document of
92 that function for the detailed behavior.
93 The gnuplot file will be named bench_test_run.gnuplot. Just run:
94 gnuplot bench_test_run.gnuplot
95 to create the graphic of the comparison curves. The image file is named
96 output_test_run.png.
98 In this section, several test will be created and run. The idea is
99 exactly the same than in the previous section, but with some basic
100 automatic way to run all the benchmarks. The following code benchmarks
101 some Eina converts functions, and some Eina containers types:
102 #include <stdlib.h>
103 #include <stdio.h>
104 #include <time.h>
105
106 #include <Eina.h>
107
108 static void bench_convert(Eina_Benchmark *bench);
109 static void bench_container(Eina_Benchmark *bench);
110
111 typedef struct _Benchmark_Case Benchmark_Case;
112
113 struct _Benchmark_Case
114 {
115 const char *bench_case;
116 void (*build)(Eina_Benchmark *bench);
117 };
118
119 static const Benchmark_Case benchmarks[] = {
120 { 'Bench 1', bench_convert },
121 { 'Bench 2', bench_container },
122 { NULL, NULL }
123 };
124
125 static
126 void convert1(int request)
127 {
128 char tmp[128];
129 int i;
130
131 srand(time(NULL));
132
133 for (i = 0; i < request; ++i)
134 eina_convert_itoa(rand(), tmp);
135 }
136
137 static
138 void convert2(int request)
139 {
140 char tmp[128];
141 int i;
142
143 srand(time(NULL));
144
145 for (i = 0; i < request; ++i)
146 eina_convert_xtoa(rand(), tmp);
147 }
148
149 static void
150 bench_convert(Eina_Benchmark *bench)
151 {
152 eina_benchmark_register(bench, 'convert-1', EINA_BENCHMARK(convert1), 200, 400, 10);
153 eina_benchmark_register(bench, 'convert-2', EINA_BENCHMARK(convert2), 200, 400, 10);
154 }
155
156 static
157 void array(int request)
158 {
159 Eina_Array *array;
160 Eina_Array_Iterator it;
161 int *data;
162 int i;
163
164 srand(time(NULL));
165
166 array = eina_array_new(64);
167
168 for (i = 0; i < request; ++i)
169 {
170 data = (int *)malloc(sizeof(int));
171 if (!data) continue;
172 *data = rand();
173 eina_array_push(array, data);
174 }
175
176 EINA_ARRAY_ITER_NEXT(array, i, data, it)
177 free(data);
178
179 eina_array_free(array);
180 }
181
182 static
183 void list(int request)
184 {
185 Eina_List *l = NULL;
186 int *data;
187 int i;
188
189 srand(time(NULL));
190
191 for (i = 0; i < request; ++i)
192 {
193 data = (int *)malloc(sizeof(int));
194 if (!data) continue;
195 *data = rand();
196 l = eina_list_prepend(l, data);
197 }
198
199 while (l)
200 {
201 free(eina_list_data_get(l));
202 l = eina_list_remove_list(l, l);
203 }
204 }
205
206 static void
207 bench_container(Eina_Benchmark *bench)
208 {
209 eina_benchmark_register(bench, 'array', EINA_BENCHMARK(array), 200, 300, 10);
210 eina_benchmark_register(bench, 'list', EINA_BENCHMARK(list), 200, 300, 10);
211 }
212
213 int main()
214 {
215 Eina_Benchmark *test;
216 Eina_Array *ea;
217 unsigned int i;
218
219 if (!eina_init())
220 return EXIT_FAILURE;
221
222 for (i = 0; benchmarks[i].bench_case != NULL; ++i)
223 {
224 test = eina_benchmark_new(benchmarks[i].bench_case, 'Benchmark example');
225 if (!test)
226 continue;
227
228 benchmarks[i].build(test);
229
230 ea = eina_benchmark_run(test);
231
232 eina_benchmark_free(test);
233 }
234
235 eina_shutdown();
236
237 return EXIT_SUCCESS;
238 }
239 gnuplot can be used to see how are performed the convert functions
240 together, as well as how are performed the containers. So it is now
241 easy to see that the hexadecimal convert function is faster than the
242 decimal one, and that arrays are faster than lists.
243 You can improve all that by executing automatically gnuplot in your
244 program, or integrate the Eina benchmark framework in an autotooled
245 project. See that page for more informations.
246
247
248
249Eina 2 Jul 2010 tutorial_benchmark_page(3)