Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix smpi_execute_benched.
[simgrid.git] / teshsuite / xbt / heap_bench / heap_bench.c
1 /* A few tests for the xbt_heap module                                      */
2
3 /* Copyright (c) 2004-2010, 2012-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdio.h>
10 #include <math.h>
11 #include <xbt/xbt_os_time.h>
12
13 #include "simgrid/msg.h"
14 #include "xbt/heap.h"
15 #include "xbt/sysdep.h"
16
17 #define MAX_TEST 1000000
18
19 static int compare_double(const void *a, const void *b)
20 {
21   double pa = *((double *) a);
22   double pb = *((double *) b);
23
24   if (pa > pb)
25     return 1;
26   if (pa < pb)
27     return -1;
28   return 0;
29 }
30
31 static void test_reset_heap(xbt_heap_t * heap, int size)
32 {
33   xbt_heap_free(*heap);
34   *heap = xbt_heap_new(size, NULL);
35
36   for (int i = 0; i < size; i++) {
37     xbt_heap_push(*heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
38   }
39 }
40
41 static void test_heap_validity(int size)
42 {
43   xbt_heap_t heap = xbt_heap_new(size, NULL);
44   double *tab = xbt_new0(double, size);
45   int i;
46
47   for (i = 0; i < size; i++) {
48     tab[i] = (double) (10.0 * rand() / (RAND_MAX + 1.0));
49     xbt_heap_push(heap, NULL, (double) tab[i]);
50   }
51
52   qsort(tab, size, sizeof(double), compare_double);
53
54   for (i = 0; i < size; i++) {
55     if (fabs(xbt_heap_maxkey(heap) - tab[i]) > 1e-9) {
56       fprintf(stderr, "Problem !\n");
57       exit(1);
58     }
59     xbt_heap_pop(heap);
60   }
61   xbt_heap_free(heap);
62   free(tab);
63   printf("Validity test complete!\n");
64 }
65
66 static void test_heap_mean_operation(int size)
67 {
68   xbt_heap_t heap = xbt_heap_new(size, NULL);
69
70   double date = xbt_os_time() * 1000000;
71   for (int i = 0; i < size; i++)
72     xbt_heap_push(heap, NULL, (10.0 * rand() / (RAND_MAX + 1.0)));
73
74   date = xbt_os_time() * 1000000 - date;
75   printf("Creation time  %d size heap : %g\n", size, date);
76
77   date = xbt_os_time() * 1000000;
78   for (int j = 0; j < MAX_TEST; j++) {
79
80     if (!(j % size) && j)
81       test_reset_heap(&heap, size);
82
83     double val = xbt_heap_maxkey(heap);
84     xbt_heap_pop(heap);
85     xbt_heap_push(heap, NULL, 3.0 * val);
86   }
87   date = xbt_os_time() * 1000000 - date;
88   printf("Mean access time for a %d size heap : %g\n", size, date * 1.0 / (MAX_TEST + 0.0));
89
90   xbt_heap_free(heap);
91 }
92
93 int main(int argc, char** argv)
94 {
95   MSG_init(&argc, argv);
96
97   for (int size = 100; size < 10000; size *= 10) {
98     test_heap_validity(size);
99     test_heap_mean_operation(size);
100   }
101   return 0;
102 }