Blame benchtests/bench-strcoll.c

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