]> sourceware.org Git - glibc.git/commitdiff
benchtests: Add fclose benchmark
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 16 May 2024 15:08:42 +0000 (08:08 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 16 May 2024 15:12:59 +0000 (08:12 -0700)
Measure duration of 100 fclose calls after opening 1 million FILEs.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
benchtests/Makefile
benchtests/README
benchtests/bench-fclose.c [new file with mode: 0644]

index 7e73b8504e3c4ded597d98b2afd6189c961d8f82..b74b5fe1ad56e29c5cc9c72ac0af64209a8ad812 100644 (file)
@@ -261,6 +261,10 @@ stdlib-benchset := \
   strtod \
   # stdlib-benchset
 
+stdio-benchset := \
+  fclose \
+  # stdio-benchset
+
 stdio-common-benchset := sprintf
 
 math-benchset := math-inlines
@@ -269,6 +273,7 @@ ifeq (${BENCHSET},)
 benchset := \
   $(hash-benchset) \
   $(math-benchset) \
+  $(stdio-benchset) \
   $(stdio-common-benchset) \
   $(stdlib-benchset) \
   $(string-benchset-all) \
@@ -419,6 +424,7 @@ VALIDBENCHSETNAMES := \
   malloc-simple \
   malloc-thread \
   math-benchset \
+  stdio-benchset \
   stdio-common-benchset \
   stdlib-benchset \
   string-benchset \
index 998ba9b2b4aec895c0671332b2519d858e8d524f..15d014a4076e6f75eb03baf8545281e15a918449 100644 (file)
@@ -87,6 +87,7 @@ where BENCHSET may be a space-separated list of the following values:
     hash-benchset
     malloc-thread
     math-benchset
+    stdio-benchset
     stdio-common-benchset
     stdlib-benchset
     string-benchset
diff --git a/benchtests/bench-fclose.c b/benchtests/bench-fclose.c
new file mode 100644 (file)
index 0000000..5f1c103
--- /dev/null
@@ -0,0 +1,75 @@
+/* Benchmark fclose.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   Copyright The GNU Toolchain Authors.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "bench-timing.h"
+#include "json-lib.h"
+
+#define NUM_FILES      1000000
+#define NUM_FCLOSE     100
+
+int
+main (int argc, char **argv)
+{
+  json_ctx_t json_ctx;
+  json_init (&json_ctx, 0, stdout);
+  json_document_begin (&json_ctx);
+
+  json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
+  json_attr_object_begin (&json_ctx, "functions");
+  json_attr_object_begin (&json_ctx, "fclose");
+
+  FILE *ff, *keep[NUM_FCLOSE];
+  int i;
+
+  for (i = 0; i < NUM_FILES; i++)
+    {
+      ff = fdopen (STDIN_FILENO, "r");
+      if (!ff)
+       {
+         fprintf (stderr, "### failed to fdopen: %m\n");
+         return EXIT_FAILURE;
+       }
+      if (i < NUM_FCLOSE)
+       keep[i] = ff;
+    }
+
+  timing_t start, stop, elapsed;
+
+  TIMING_NOW (start);
+
+  for (i = 0; i < NUM_FCLOSE; i++)
+    fclose (keep[i]);
+
+  TIMING_NOW (stop);
+
+  TIMING_DIFF (elapsed, start, stop);
+
+  json_attr_uint (&json_ctx, "number of FILEs", NUM_FILES);
+  json_attr_uint (&json_ctx, "number of fclose calls", NUM_FCLOSE);
+  json_attr_uint (&json_ctx, "duration", elapsed);
+
+  json_attr_object_end (&json_ctx);
+  json_attr_object_end (&json_ctx);
+  json_document_end (&json_ctx);
+
+  return 0;
+}
This page took 0.04745 seconds and 5 git commands to generate.