Path: blob/main/contrib/libucl/tests/test_speed.c
104187 views
/* Copyright (c) 2013, Vsevolod Stakhov1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10*11* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY12* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED13* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE14* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY15* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES16* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;17* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND18* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT19* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS20* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.21*/2223#include <sys/types.h>24#include <sys/mman.h>25#include <sys/stat.h>26#include <sys/time.h>27#include <stdio.h>28#include <errno.h>29#include <unistd.h>30#include <fcntl.h>31#include <time.h>3233#ifdef __APPLE__34#ifdef HAVE_MACH_MACH_TIME_H35#include <mach/mach_time.h>36#endif37#endif3839#include "ucl.h"4041static double42get_ticks (void)43{44double res;4546#if defined(__APPLE__) && defined(HAVE_MACH_MACH_TIME_H)47res = mach_absolute_time () / 1000000000.;48#else49struct timespec ts;50clock_gettime (CLOCK_MONOTONIC, &ts);5152res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.;53#endif5455return res;56}5758int59main (int argc, char **argv)60{61void *map;62struct ucl_parser *parser;63ucl_object_t *obj;64int fin;65unsigned char *emitted;66struct stat st;67const char *fname_in = NULL;68int ret = 0;69double start, end, seconds;7071switch (argc) {72case 2:73fname_in = argv[1];74break;75}7677fin = open (fname_in, O_RDONLY);78if (fin == -1) {79perror ("open failed");80exit (EXIT_FAILURE);81}82parser = ucl_parser_new (UCL_PARSER_ZEROCOPY);8384(void)fstat (fin, &st);85map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fin, 0);86if (map == MAP_FAILED) {87perror ("mmap failed");88exit (EXIT_FAILURE);89}9091close (fin);9293start = get_ticks ();94ucl_parser_add_chunk (parser, map, st.st_size);9596obj = ucl_parser_get_object (parser);97end = get_ticks ();9899seconds = end - start;100printf ("ucl: parsed input in %.4f seconds\n", seconds);101if (ucl_parser_get_error(parser)) {102printf ("Error occurred: %s\n", ucl_parser_get_error(parser));103ret = 1;104goto err;105}106107start = get_ticks ();108emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG);109end = get_ticks ();110111seconds = end - start;112printf ("ucl: emitted config in %.4f seconds\n", seconds);113114free (emitted);115116start = get_ticks ();117emitted = ucl_object_emit (obj, UCL_EMIT_JSON);118end = get_ticks ();119120seconds = end - start;121printf ("ucl: emitted json in %.4f seconds\n", seconds);122123free (emitted);124125start = get_ticks ();126emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT);127end = get_ticks ();128129seconds = end - start;130printf ("ucl: emitted compact json in %.4f seconds\n", seconds);131132free (emitted);133134start = get_ticks ();135emitted = ucl_object_emit (obj, UCL_EMIT_YAML);136end = get_ticks ();137138seconds = end - start;139printf ("ucl: emitted yaml in %.4f seconds\n", seconds);140141free (emitted);142143ucl_parser_free (parser);144ucl_object_unref (obj);145146err:147munmap (map, st.st_size);148149return ret;150}151152153