Blame benchtests/bench-strcoll.c

Packit 6c4009
/* Measure strcoll execution time in different locales.
Packit 6c4009
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include "json-lib.h"
Packit 6c4009
#include "bench-timing.h"
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
/* Many thanks to http://generator.lorem-ipsum.info/  */
Packit 6c4009
#define INPUT_PREFIX "strcoll-inputs/"
Packit 6c4009
Packit 6c4009
static const char *const input_files[] = {
Packit 6c4009
  "filelist#C",
Packit 6c4009
  "filelist#en_US.UTF-8",
Packit 6c4009
  "lorem_ipsum#vi_VN.UTF-8",
Packit 6c4009
  "lorem_ipsum#ar_SA.UTF-8",
Packit 6c4009
  "lorem_ipsum#en_US.UTF-8",
Packit 6c4009
  "lorem_ipsum#zh_CN.UTF-8",
Packit 6c4009
  "lorem_ipsum#cs_CZ.UTF-8",
Packit 6c4009
  "lorem_ipsum#en_GB.UTF-8",
Packit 6c4009
  "lorem_ipsum#da_DK.UTF-8",
Packit 6c4009
  "lorem_ipsum#pl_PL.UTF-8",
Packit 6c4009
  "lorem_ipsum#fr_FR.UTF-8",
Packit 6c4009
  "lorem_ipsum#pt_PT.UTF-8",
Packit 6c4009
  "lorem_ipsum#el_GR.UTF-8",
Packit 6c4009
  "lorem_ipsum#ru_RU.UTF-8",
Packit 6c4009
  "lorem_ipsum#he_IL.UTF-8",
Packit 6c4009
  "lorem_ipsum#es_ES.UTF-8",
Packit 6c4009
  "lorem_ipsum#hi_IN.UTF-8",
Packit 6c4009
  "lorem_ipsum#sv_SE.UTF-8",
Packit 6c4009
  "lorem_ipsum#hu_HU.UTF-8",
Packit 6c4009
  "lorem_ipsum#tr_TR.UTF-8",
Packit 6c4009
  "lorem_ipsum#is_IS.UTF-8",
Packit 6c4009
  "lorem_ipsum#it_IT.UTF-8",
Packit 6c4009
  "lorem_ipsum#sr_RS.UTF-8",
Packit 6c4009
  "lorem_ipsum#ja_JP.UTF-8"
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
#define TEXTFILE_DELIMITER " \n\r\t.,?!"
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
read_file (const char *filename)
Packit 6c4009
{
Packit 6c4009
  struct stat stats;
Packit 6c4009
  char *buffer = NULL;
Packit 6c4009
  int fd = open (filename, O_CLOEXEC);
Packit 6c4009
Packit 6c4009
  if (fd >= 0)
Packit 6c4009
    {
Packit 6c4009
      if (fstat (fd, &stats) == 0)
Packit 6c4009
	{
Packit 6c4009
	  buffer = malloc (stats.st_size + 1);
Packit 6c4009
	  if (buffer)
Packit 6c4009
	    {
Packit 6c4009
	      if (read (fd, buffer, stats.st_size) == stats.st_size)
Packit 6c4009
		buffer[stats.st_size] = '\0';
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  free (buffer);
Packit 6c4009
		  buffer = NULL;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      close (fd);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return buffer;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static size_t
Packit 6c4009
count_words (const char *text, const char *delim)
Packit 6c4009
{
Packit 6c4009
  size_t wordcount = 0;
Packit 6c4009
  char *tmp = strdup (text);
Packit 6c4009
Packit 6c4009
  char *token = strtok (tmp, delim);
Packit 6c4009
  while (token != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (*token != '\0')
Packit 6c4009
	wordcount++;
Packit 6c4009
      token = strtok (NULL, delim);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  free (tmp);
Packit 6c4009
  return wordcount;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
typedef struct
Packit 6c4009
{
Packit 6c4009
  size_t size;
Packit 6c4009
  char **words;
Packit 6c4009
} word_list;
Packit 6c4009
Packit 6c4009
static word_list *
Packit 6c4009
new_word_list (size_t size)
Packit 6c4009
{
Packit 6c4009
  word_list *list = malloc (sizeof (word_list));
Packit 6c4009
  assert (list != NULL);
Packit 6c4009
  list->size = size;
Packit 6c4009
  list->words = malloc (size * sizeof (char *));
Packit 6c4009
  assert (list->words != NULL);
Packit 6c4009
  return list;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static word_list *
Packit 6c4009
str_word_list (const char *str, const char *delim)
Packit 6c4009
{
Packit 6c4009
  size_t n = 0;
Packit 6c4009
  word_list *list = new_word_list (count_words (str, delim));
Packit 6c4009
Packit 6c4009
  char *toks = strdup (str);
Packit 6c4009
  char *word = strtok (toks, delim);
Packit 6c4009
  while (word != NULL && n < list->size)
Packit 6c4009
    {
Packit 6c4009
      if (*word != '\0')
Packit 6c4009
	list->words[n++] = strdup (word);
Packit 6c4009
      word = strtok (NULL, delim);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  free (toks);
Packit 6c4009
  return list;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static word_list *
Packit 6c4009
copy_word_list (const word_list *list)
Packit 6c4009
{
Packit 6c4009
  size_t i;
Packit 6c4009
  word_list *copy = new_word_list (list->size);
Packit 6c4009
Packit 6c4009
  for (i = 0; i < list->size; i++)
Packit 6c4009
    copy->words[i] = strdup (list->words[i]);
Packit 6c4009
Packit 6c4009
  return copy;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
free_word_list (word_list *list)
Packit 6c4009
{
Packit 6c4009
  size_t i;
Packit 6c4009
  for (i = 0; i < list->size; i++)
Packit 6c4009
    free (list->words[i]);
Packit 6c4009
Packit 6c4009
  free (list->words);
Packit 6c4009
  free (list);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
compare_words (const void *a, const void *b)
Packit 6c4009
{
Packit 6c4009
  const char *s1 = *(char **) a;
Packit 6c4009
  const char *s2 = *(char **) b;
Packit 6c4009
  return strcoll (s1, s2);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#undef INNER_LOOP_ITERS
Packit 6c4009
#define INNER_LOOP_ITERS 16
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
bench_list (json_ctx_t *json_ctx, word_list *list)
Packit 6c4009
{
Packit 6c4009
  size_t i;
Packit 6c4009
  timing_t start, stop, cur;
Packit 6c4009
Packit 6c4009
  word_list **tests = malloc (INNER_LOOP_ITERS * sizeof (word_list *));
Packit 6c4009
  assert (tests != NULL);
Packit 6c4009
  for (i = 0; i < INNER_LOOP_ITERS; i++)
Packit 6c4009
    tests[i] = copy_word_list (list);
Packit 6c4009
Packit 6c4009
  TIMING_NOW (start);
Packit 6c4009
  for (i = 0; i < INNER_LOOP_ITERS; i++)
Packit 6c4009
    qsort (tests[i]->words, tests[i]->size, sizeof (char *), compare_words);
Packit 6c4009
  TIMING_NOW (stop);
Packit 6c4009
Packit 6c4009
  TIMING_DIFF (cur, start, stop);
Packit 6c4009
  setlocale (LC_ALL, "en_US.UTF-8");
Packit 6c4009
  json_attr_double (json_ctx, "duration", cur);
Packit 6c4009
  json_attr_double (json_ctx, "iterations", i);
Packit 6c4009
  json_attr_double (json_ctx, "mean", (double) cur / i);
Packit 6c4009
Packit 6c4009
  for (i = 0; i < INNER_LOOP_ITERS; i++)
Packit 6c4009
    free_word_list (tests[i]);
Packit 6c4009
  free (tests);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
typedef enum
Packit 6c4009
{
Packit 6c4009
  OK,
Packit 6c4009
  ERROR_FILENAME,
Packit 6c4009
  ERROR_LOCALE,
Packit 6c4009
  ERROR_IO
Packit 6c4009
} result_t;
Packit 6c4009
Packit 6c4009
static result_t
Packit 6c4009
bench_file (json_ctx_t *json_ctx, const char *testname, const char *filename,
Packit 6c4009
	    const char *locale)
Packit 6c4009
{
Packit 6c4009
  if (setlocale (LC_ALL, locale) == NULL)
Packit 6c4009
    return ERROR_LOCALE;
Packit 6c4009
Packit 6c4009
  char *text = read_file (filename);
Packit 6c4009
  if (text == NULL)
Packit 6c4009
    return ERROR_IO;
Packit 6c4009
Packit 6c4009
  word_list *list = str_word_list (text, TEXTFILE_DELIMITER);
Packit 6c4009
Packit 6c4009
  json_attr_object_begin (json_ctx, testname);
Packit 6c4009
  bench_list (json_ctx, list);
Packit 6c4009
  json_attr_object_end (json_ctx);
Packit 6c4009
Packit 6c4009
  free_word_list (list);
Packit 6c4009
  free (text);
Packit 6c4009
  return OK;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  json_ctx_t *json_ctx = malloc (sizeof (json_ctx_t));
Packit 6c4009
  assert (json_ctx != NULL);
Packit 6c4009
  json_init (json_ctx, 2, stdout);
Packit 6c4009
  json_attr_object_begin (json_ctx, "strcoll");
Packit 6c4009
Packit 6c4009
  size_t i;
Packit 6c4009
  result_t result = OK;
Packit 6c4009
  for (i = 0; i < (sizeof (input_files) / sizeof (input_files[0])); i++)
Packit 6c4009
    {
Packit 6c4009
      char *locale = strchr (input_files[i], '#');
Packit 6c4009
      if (locale == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("Failed to get locale from filename %s, aborting!\n",
Packit 6c4009
		  input_files[i]);
Packit 6c4009
	  return ERROR_FILENAME;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      char *filename;
Packit 6c4009
      asprintf (&filename, INPUT_PREFIX "%s", input_files[i]);
Packit 6c4009
      result = bench_file (json_ctx, input_files[i], filename, locale + 1);
Packit 6c4009
Packit 6c4009
      if (result != OK)
Packit 6c4009
	{
Packit 6c4009
	  if (result == ERROR_LOCALE)
Packit 6c4009
	    printf ("Failed to set locale %s, aborting!\n", locale);
Packit 6c4009
	  else if (result == ERROR_IO)
Packit 6c4009
	    printf ("Failed to read file %s, aborting!\n", filename);
Packit 6c4009
	  free (filename);
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
      free (filename);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
out:
Packit 6c4009
  json_attr_object_end (json_ctx);
Packit 6c4009
  free (json_ctx);
Packit 6c4009
  return result;
Packit 6c4009
}