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 "ucl.h" 25 #include "ucl_internal.h" 26 27 #include <sys/types.h> 28 29 #ifndef _WIN32 30 #include <sys/mman.h> 31 #include <sys/time.h> 32 #endif 33 34 #include <sys/stat.h> 35 #include <stdio.h> 36 #include <errno.h> 37 38 #ifndef _WIN32 39 #include <unistd.h> 40 #endif 41 42 #include <fcntl.h> 43 #include <time.h> 44 45 #ifdef __APPLE__ 46 #ifdef HAVE_MACH_MACH_TIME_H 47 #include <mach/mach_time.h> 48 #endif 49 #endif 50 51 static double 52 get_ticks (void) 53 { 54 double res; 55 56 #if defined(__APPLE__) && defined(HAVE_MACH_MACH_TIME_H) 57 res = mach_absolute_time () / 1000000000.; 58 #else 59 struct timespec ts; 60 clock_gettime (CLOCK_MONOTONIC, &ts); 61 62 res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.; 63 #endif 64 65 return res; 66 } 67 68 int 69 main (int argc, char **argv) 70 { 71 void *map; 72 struct ucl_parser *parser; 73 ucl_object_t *obj; 74 int fin; 75 unsigned char *emitted; 76 struct stat st; 77 const char *fname_in = NULL; 78 int ret = 0; 79 double start, end, seconds; 80 81 switch (argc) { 82 case 2: 83 fname_in = argv[1]; 84 break; 85 } 86 87 fin = open (fname_in, O_RDONLY); 88 if (fin == -1) { 89 perror ("open failed"); 90 exit (EXIT_FAILURE); 91 } 92 parser = ucl_parser_new (UCL_PARSER_ZEROCOPY); 93 94 (void)fstat (fin, &st); 95 map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fin, 0); 96 if (map == MAP_FAILED) { 97 perror ("mmap failed"); 98 exit (EXIT_FAILURE); 99 } 100 101 close (fin); 102 103 start = get_ticks (); 104 ucl_parser_add_chunk (parser, map, st.st_size); 105 106 obj = ucl_parser_get_object (parser); 107 end = get_ticks (); 108 109 seconds = end - start; 110 printf ("ucl: parsed input in %.4f seconds\n", seconds); 111 if (ucl_parser_get_error(parser)) { 112 printf ("Error occurred: %s\n", ucl_parser_get_error(parser)); 113 ret = 1; 114 goto err; 115 } 116 117 start = get_ticks (); 118 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); 119 end = get_ticks (); 120 121 seconds = end - start; 122 printf ("ucl: emitted config in %.4f seconds\n", seconds); 123 124 free (emitted); 125 126 start = get_ticks (); 127 emitted = ucl_object_emit (obj, UCL_EMIT_JSON); 128 end = get_ticks (); 129 130 seconds = end - start; 131 printf ("ucl: emitted json in %.4f seconds\n", seconds); 132 133 free (emitted); 134 135 start = get_ticks (); 136 emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT); 137 end = get_ticks (); 138 139 seconds = end - start; 140 printf ("ucl: emitted compact json in %.4f seconds\n", seconds); 141 142 free (emitted); 143 144 start = get_ticks (); 145 emitted = ucl_object_emit (obj, UCL_EMIT_YAML); 146 end = get_ticks (); 147 148 seconds = end - start; 149 printf ("ucl: emitted yaml in %.4f seconds\n", seconds); 150 151 free (emitted); 152 153 ucl_parser_free (parser); 154 ucl_object_unref (obj); 155 156 err: 157 munmap (map, st.st_size); 158 159 return ret; 160 } 161