Logo AND Algorithmique Numérique Distribuée

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