]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/libucl/tests/test_speed.c
[SA-14:25] Fix kernel stack disclosure in setlogin(2) / getlogin(2).
[FreeBSD/releng/9.3.git] / contrib / libucl / tests / test_speed.c
1 /* Copyright (c) 2013, Vsevolod Stakhov
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *       * Redistributions of source code must retain the above copyright
7  *         notice, this list of conditions and the following disclaimer.
8  *       * Redistributions in binary form must reproduce the above copyright
9  *         notice, this list of conditions and the following disclaimer in the
10  *         documentation and/or other materials provided with the distribution.
11  *
12  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  */
23
24 #include <sys/types.h>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <time.h>
33
34 #ifdef __APPLE__
35 #include <mach/mach_time.h>
36 #endif
37
38 #include "ucl.h"
39
40 static double
41 get_ticks (void)
42 {
43         double res;
44
45 #ifdef __APPLE__
46         res = mach_absolute_time () / 1000000000.;
47 #else
48         struct timespec ts;
49         clock_gettime (CLOCK_MONOTONIC, &ts);
50
51         res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.;
52 #endif
53
54         return res;
55 }
56
57 int
58 main (int argc, char **argv)
59 {
60         void *map;
61         struct ucl_parser *parser;
62         ucl_object_t *obj;
63         int fin;
64         unsigned char *emitted;
65         struct stat st;
66         const char *fname_in = NULL;
67         int ret = 0;
68         double start, end, seconds;
69
70         switch (argc) {
71         case 2:
72                 fname_in = argv[1];
73                 break;
74         }
75
76         fin = open (fname_in, O_RDONLY);
77         if (fin == -1) {
78                 perror ("open failed");
79                 exit (EXIT_FAILURE);
80         }
81         parser = ucl_parser_new (UCL_PARSER_ZEROCOPY);
82
83         (void)fstat (fin, &st);
84         map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fin, 0);
85         if (map == MAP_FAILED) {
86                 perror ("mmap failed");
87                 exit (EXIT_FAILURE);
88         }
89
90         close (fin);
91
92         start = get_ticks ();
93         ucl_parser_add_chunk (parser, map, st.st_size);
94
95         obj = ucl_parser_get_object (parser);
96         end = get_ticks ();
97
98         seconds = end - start;
99         printf ("ucl: parsed input in %.4f seconds\n", seconds);
100         if (ucl_parser_get_error(parser)) {
101                 printf ("Error occurred: %s\n", ucl_parser_get_error(parser));
102                 ret = 1;
103                 goto err;
104         }
105
106         start = get_ticks ();
107         emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG);
108         end = get_ticks ();
109
110         seconds = end - start;
111         printf ("ucl: emitted config in %.4f seconds\n", seconds);
112
113         free (emitted);
114
115         start = get_ticks ();
116         emitted = ucl_object_emit (obj, UCL_EMIT_JSON);
117         end = get_ticks ();
118
119         seconds = end - start;
120         printf ("ucl: emitted json in %.4f seconds\n", seconds);
121
122         free (emitted);
123
124         start = get_ticks ();
125         emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT);
126         end = get_ticks ();
127
128         seconds = end - start;
129         printf ("ucl: emitted compact json in %.4f seconds\n", seconds);
130
131         free (emitted);
132
133         start = get_ticks ();
134         emitted = ucl_object_emit (obj, UCL_EMIT_YAML);
135         end = get_ticks ();
136
137         seconds = end - start;
138         printf ("ucl: emitted yaml in %.4f seconds\n", seconds);
139
140         free (emitted);
141
142         ucl_parser_free (parser);
143         ucl_object_unref (obj);
144
145 err:
146         munmap (map, st.st_size);
147
148         return ret;
149 }