<commit_before>/* Copyright (c) 2015 udgover */

/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files (the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */

/* The above copyright notice and this permission notice shall be included in */
/* all copies or substantial portions of the Software. */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */
/* THE SOFTWARE. */

/* Benchmark for libmagic */
/* gcc -O2 magicbench.c -lmagic -o magicbench */
/* ./magicbench -f file -b 4096 -i 10000 */

#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <magic.h>


/* <NAME>aple code at http://stackoverflow.com/a/21665642 */
double get_elapsed_time(struct timespec *before, struct timespec *after)
{
  double deltat_s  = after->tv_sec - before->tv_sec;
  double deltat_ns = after->tv_nsec - before->tv_nsec;
  return deltat_s + deltat_ns*1e-9;
}


char*	get_magic(void* buffer, size_t size)
{
  magic_t	ms;
  const char*	tmp;
  char*		result;

  ms = NULL;
  tmp = NULL;
  result = NULL;
  if ((ms = magic_open(MAGIC_NONE)) != NULL)
    {
      if (magic_load(ms, "/usr/share/misc/magic.mgc") != -1)
	{
	  if ((tmp = magic_buffer(ms, buffer, size)) != NULL)
	    {
	      if ((result = malloc(strlen(tmp) * sizeof(char))) != NULL)
		memcpy(result, tmp, strlen(tmp));
	    }
	}
      magic_close(ms);
    }
  return result;
}


void		magic_benchmarked(void* buffer, size_t size)
{
  struct timespec	before;
  struct timespec	after;
  magic_t		ms;
  const char*		result;
  int			load;

  ms = NULL;
  result = NULL;
  clock_gettime(CLOCK_MONOTONIC, &before);
  ms = magic_open(MAGIC_NONE);
  clock_gettime(CLOCK_MONOTONIC, &after);
  printf("magic_open = %e s\n", get_elapsed_time(&before, &after));
  if (ms != NULL)
    {
      clock_gettime(CLOCK_MONOTONIC, &before);
      load = magic_load(ms, "/usr/share/misc/magic.mgc");
      clock_gettime(CLOCK_MONOTONIC, &after);
      printf("magic_load = %e s\n", get_elapsed_time(&before, &after));
      if (load != -1)
	{
	  clock_gettime(CLOCK_MONOTONIC, &before);
	  result = magic_buffer(ms, buffer, size);
	  clock_gettime(CLOCK_MONOTONIC, &after);
	  printf("magic_buffer = %e s\n", get_elapsed_time(&before, &after));
	  if (result != NULL)
	    printf("%s\n", result);
	}
      clock_gettime(CLOCK_MONOTONIC, &before);
      magic_close(ms);
      clock_gettime(CLOCK_MONOTONIC, &after);
      printf("magic_close = %e s\n", get_elapsed_time(&before, &after));	  
    }
  return;
}


void	magic_iter(void* buffer, int size, int iter)
{
  struct timespec	before;
  struct timespec	after;
  const char*		tmp;
  int			i;

  clock_gettime(CLOCK_MONOTONIC, &before);
  for (i = 0; i != iter; ++i)
    {
      if ((tmp = get_magic(buffer, size)) != NULL)
	free((void*)tmp);
    }
  clock_gettime(CLOCK_MONOTONIC, &after);
  printf("%d iter = %e s\n", iter, get_elapsed_time(&before, &after));
}


int	allocAndRead(char* path, void** buffer, int size)
{
  int	fd;
  int	rbytes;

  rbytes = -1;
  if ((fd = open("/mnt/loop/sda3_prev_desktop.dd", O_RDONLY)) != -1)
    {
      if ((*buffer = malloc(size)) != NULL)
	rbytes = read(fd, *buffer, size);
      close(fd);
    }
  return rbytes;
}


int	main(int argc, char* argv[])
{
  void*			buffer;
  char*			filename;
  int			rbytes;
  int			opt;
  int			buffsize;<commit_msg>Remove hardcode path :)<commit_after>/* Copyright (c) 2015 udgover */

/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files (the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */

/* The above copyright notice and this permission notice shall be included in */
/* all copies or substantial portions of the Software. */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */
/* THE SOFTWARE. */

/* Benchmark for libmagic */
/* gcc -O2 magicbench.c -lmagic -o magicbench */
/* ./magicbench -f file -b 4096 -i 10000 */

#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <magic.h>


/* <NAME>aple code at http://stackoverflow.com/a/21665642 */
double get_elapsed_time(struct timespec *before, struct timespec *after)
{
  double deltat_s  = after->tv_sec - before->tv_sec;
  double deltat_ns = after->tv_nsec - before->tv_nsec;
  return deltat_s + deltat_ns*1e-9;
}


char*	get_magic(void* buffer, size_t size)
{
  magic_t	ms;
  const char*	tmp;
  char*		result;

  ms = NULL;
  tmp = NULL;
  result = NULL;
  if ((ms = magic_open(MAGIC_NONE)) != NULL)
    {
      if (magic_load(ms, "/usr/share/misc/magic.mgc") != -1)
	{
	  if ((tmp = magic_buffer(ms, buffer, size)) != NULL)
	    {
	      if ((result = malloc(strlen(tmp) * sizeof(char))) != NULL)
		memcpy(result, tmp, strlen(tmp));
	    }
	}
      magic_close(ms);
    }
  return result;
}


void		magic_benchmarked(void* buffer, size_t size)
{
  struct timespec	before;
  struct timespec	after;
  magic_t		ms;
  const char*		result;
  int			load;

  ms = NULL;
  result = NULL;
  clock_gettime(CLOCK_MONOTONIC, &before);
  ms = magic_open(MAGIC_NONE);
  clock_gettime(CLOCK_MONOTONIC, &after);
  printf("magic_open = %e s\n", get_elapsed_time(&before, &after));
  if (ms != NULL)
    {
      clock_gettime(CLOCK_MONOTONIC, &before);
      load = magic_load(ms, "/usr/share/misc/magic.mgc");
      clock_gettime(CLOCK_MONOTONIC, &after);
      printf("magic_load = %e s\n", get_elapsed_time(&before, &after));
      if (load != -1)
	{
	  clock_gettime(CLOCK_MONOTONIC, &before);
	  result = magic_buffer(ms, buffer, size);
	  clock_gettime(CLOCK_MONOTONIC, &after);
	  printf("magic_buffer = %e s\n", get_elapsed_time(&before, &after));
	  if (result != NULL)
	    printf("%s\n", result);
	}
      clock_gettime(CLOCK_MONOTONIC, &before);
      magic_close(ms);
      clock_gettime(CLOCK_MONOTONIC, &after);
      printf("magic_close = %e s\n", get_elapsed_time(&before, &after));	  
    }
  return;
}


void	magic_iter(void* buffer, int size, int iter)
{
  struct timespec	before;
  struct timespec	after;
  const char*		tmp;
  int			i;

  clock_gettime(CLOCK_MONOTONIC, &before);
  for (i = 0; i != iter; ++i)
    {
      if ((tmp = get_magic(buffer, size)) != NULL)
	free((void*)tmp);
    }
  clock_gettime(CLOCK_MONOTONIC, &after);
  printf("%d iter = %e s\n", iter, get_elapsed_time(&before, &after));
}


int	allocAndRead(char* path, void** buffer, int size)
{
  int	fd;
  int	rbytes;

  rbytes = -1;
  if ((fd = open(path, O_RDONLY)) != -1)
    {
      if ((*buffer = malloc(size)) != NULL)
	rbytes = read(fd, *buffer, size);
      close(fd);
    }
  return rbytes;
}


int	main(int argc, char* argv[])
{
  void*			buffer;
  char*			filename;
  int			rbytes;
  int			opt;
  int			buffsize;
{'fraction_non_alphanumeric': 0.1386965862974457, 'fraction_numerical': 0.007042253521126761, 'mean_word_length': 3.5048387096774194, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5578453', 'n_tokens_mistral': 3113, 'n_tokens_neox': 2918, 'n_words': 868}