[tests] on more -fanalyzer
[hkl.git] / tests / hkl-bench-t.c
blob252c283727c9f64d8584d2cb451554228c1e8112
1 /* This file is part of the hkl library.
3 * The hkl library is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * The hkl library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2003-2019, 2021, 2023 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
22 #include <stdio.h>
23 #include <sys/time.h>
24 #include <tap/basic.h>
25 #include "hkl.h"
27 static void hkl_test_bench_run_real(HklEngine *engine, HklGeometry *geometry,
28 size_t n_values, double values[n_values],
29 size_t n)
31 size_t i;
32 const darray_string *modes = hkl_engine_modes_names_get(engine);
33 const char **mode;
35 /* pseudo -> geometry */
36 darray_foreach(mode, *modes){
37 double min, max, mean;
38 const darray_string *parameters;
39 size_t n_params;
41 if (FALSE == hkl_engine_current_mode_set(engine, *mode, NULL))
42 continue;
44 parameters = hkl_engine_parameters_names_get(engine);
45 n_params = darray_size(*parameters);
46 if (n_params){
47 double params[n_params];
49 hkl_engine_parameters_values_get(engine,
50 params, n_params,
51 HKL_UNIT_DEFAULT);
52 values[0] = 1;
53 if(FALSE == hkl_engine_parameters_values_set(engine,
54 params, n_params,
55 HKL_UNIT_DEFAULT, NULL))
56 continue;
59 mean = max = 0;
60 min = 1000; /* arbitrary value always greater than the real min */
61 for(i=0; i<n; ++i){
62 struct timeval debut, fin, dt;
63 double t;
64 HklGeometryList *solutions;
66 if(FALSE == hkl_geometry_set_values_v(geometry, HKL_UNIT_USER, NULL, 0., 0., 0., 0., 10., 10.))
67 break;
68 gettimeofday(&debut, NULL);
69 solutions = hkl_engine_pseudo_axis_values_set(engine, values, n_values, HKL_UNIT_DEFAULT, NULL);
70 if (NULL != solutions)
71 hkl_geometry_list_free(solutions);
72 gettimeofday(&fin, NULL);
73 timersub(&fin, &debut, &dt);
74 t = dt.tv_sec * 1000. + dt.tv_usec / 1000.;
75 min = t < min ? t : min;
76 max = t > max ? t : max;
77 mean += t;
79 fprintf(stdout, "\"%s\" \"%s\" \"%s\" (%zd/%zd) iterations %f / %f / %f [min/mean/max] ms each\n",
80 hkl_geometry_name_get(geometry),
81 hkl_engine_name_get(engine),
82 *mode, n, i, min, mean/n, max);
86 static void hkl_test_bench_run_v(HklEngineList *engines, HklGeometry *geometry,
87 char const *name, int n, size_t n_values, ...)
89 va_list ap;
90 size_t i;
91 HklEngine *engine = hkl_engine_list_engine_get_by_name(engines, name, NULL);
92 double values[n_values];
94 va_start(ap, n_values);
95 for(i=0; i<n_values; ++i)
96 values[i] = va_arg(ap, double);
97 va_end(ap);
99 hkl_test_bench_run_real(engine, geometry, n_values, values, n);
102 static void hkl_test_bench_k6c(int n)
104 const HklFactory *factory;
105 HklEngineList *engines;
106 HklGeometry *geom;
107 HklDetector *detector;
108 HklSample *sample;
110 factory = hkl_factory_get_by_name("K6C", NULL);
112 geom = hkl_factory_create_new_geometry(factory);
114 detector = hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D);
116 sample = hkl_sample_new("test");
118 engines = hkl_factory_create_new_engine_list(factory);
119 hkl_engine_list_init(engines, geom, detector, sample);
121 hkl_test_bench_run_v(engines, geom, "hkl", n, 3, 1., 0., 0.);
122 hkl_test_bench_run_v(engines, geom, "eulerians", n, 3, 0., 90*HKL_DEGTORAD, 0.);
123 hkl_test_bench_run_v(engines, geom, "psi", n, 1, 10.*HKL_DEGTORAD);
124 hkl_test_bench_run_v(engines, geom, "q2", n, 2, 1., 10.*HKL_DEGTORAD);
125 hkl_test_bench_run_v(engines, geom, "qper_qpar", n, 2, 1., 1.);
127 hkl_engine_list_free(engines);
128 hkl_sample_free(sample);
129 hkl_detector_free(detector);
130 hkl_geometry_free(geom);
133 static void hkl_test_bench_eulerians(void)
135 HklEngineList *engines;
136 HklEngine *engine;
137 const char **mode;
138 const darray_string *modes;
139 const HklFactory *factory;
140 HklGeometry *geometry;
141 HklDetector *detector;
142 HklSample *sample;
144 factory = hkl_factory_get_by_name("K6C", NULL);
146 geometry = hkl_factory_create_new_geometry(factory);
147 detector = hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D);
149 sample = hkl_sample_new("test");
150 engines = hkl_factory_create_new_engine_list(factory);
151 hkl_engine_list_init(engines, geometry, detector, sample);
153 engine = hkl_engine_list_engine_get_by_name(engines, "eulerians", NULL);
154 modes = hkl_engine_modes_names_get(engine);
156 darray_foreach(mode, *modes){
157 static double values[3] = {0, 90 * HKL_DEGTORAD, 0};
158 HklGeometryList *solutions;
160 if(FALSE == hkl_engine_current_mode_set(engine, *mode, NULL))
161 continue;
163 /* studdy this degenerated case */
164 solutions = hkl_engine_pseudo_axis_values_set(engine,
165 values, ARRAY_SIZE(values),
166 HKL_UNIT_DEFAULT, NULL);
167 if (solutions) {
168 const HklGeometryListItem *item;
170 HKL_GEOMETRY_LIST_FOREACH(item, solutions){
171 hkl_geometry_set(geometry,
172 hkl_geometry_list_item_geometry_get(item));
173 if(FALSE == hkl_engine_pseudo_axis_values_get(engine, values, ARRAY_SIZE(values),
174 HKL_UNIT_DEFAULT, NULL))
175 break;
177 hkl_geometry_list_free(solutions);
181 hkl_engine_list_free(engines);
182 hkl_sample_free(sample);
183 hkl_detector_free(detector);
184 hkl_geometry_free(geometry);
187 int main(int argc, char **argv)
189 int n;
191 plan(1);
193 if (argc > 1)
194 n = atoi(argv[1]);
195 else
196 n = 10;
198 hkl_test_bench_k6c(n);
199 hkl_test_bench_eulerians();
201 ok(TRUE == TRUE, __func__);
203 return 0;