diff mbox series

benchtests: Add fclose benchmark

Message ID 20240516135024.557291-1-hjl.tools@gmail.com
State New
Headers show
Series benchtests: Add fclose benchmark | expand

Commit Message

H.J. Lu May 16, 2024, 1:50 p.m. UTC
Measure duration of 100 fclose calls after opening 1 million FILEs.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 benchtests/Makefile       |  6 ++++
 benchtests/README         |  1 +
 benchtests/bench-fclose.c | 74 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+)
 create mode 100644 benchtests/bench-fclose.c

Comments

Carlos O'Donell May 16, 2024, 2:58 p.m. UTC | #1
On 5/16/24 9:50 AM, H.J. Lu wrote:
> Measure duration of 100 fclose calls after opening 1 million FILEs.

Fantastic idea.

I'd like to see the results of this for the doubly-linked list change.

OK to keep my Reviewed-by: and push if you only change the copyright notice as requested.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
 
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> ---
>  benchtests/Makefile       |  6 ++++
>  benchtests/README         |  1 +
>  benchtests/bench-fclose.c | 74 +++++++++++++++++++++++++++++++++++++++
>  3 files changed, 81 insertions(+)
>  create mode 100644 benchtests/bench-fclose.c
> 
> diff --git a/benchtests/Makefile b/benchtests/Makefile
> index 7e73b8504e..b74b5fe1ad 100644
> --- a/benchtests/Makefile
> +++ b/benchtests/Makefile
> @@ -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 \
> diff --git a/benchtests/README b/benchtests/README
> index 998ba9b2b4..15d014a407 100644
> --- a/benchtests/README
> +++ b/benchtests/README
> @@ -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
> index 0000000000..c1cdf03169
> --- /dev/null
> +++ b/benchtests/bench-fclose.c
> @@ -0,0 +1,74 @@
> +/* Benchmark fclose.
> +   Copyright (C) 2024 Free Software Foundation, Inc.

Should have "Copyright The GNU Toolchain Authors." since you are contributing under DCO
a file that is a combination of copying from an existing benchtests file and adding
your own changes.

> +   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

OK. I accept that on some systems this may fail due to the open files limit,
but this is for microbenchmarking so the system needs to be adjusted for that.
In the future we might want to run a small sanity checking program as the first
thing in the microbenchmark run that tests for all of these conditions. OK for now.

> +#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;
> +}
H.J. Lu May 16, 2024, 3:11 p.m. UTC | #2
On Thu, May 16, 2024 at 7:58 AM Carlos O'Donell <carlos@redhat.com> wrote:
>
> On 5/16/24 9:50 AM, H.J. Lu wrote:
> > Measure duration of 100 fclose calls after opening 1 million FILEs.
>
> Fantastic idea.
>
> I'd like to see the results of this for the doubly-linked list change.

Before:

{
 "timing_type": "hp_timing",
 "functions": {
  "fclose": {
   "number of FILEs": 1000000,
   "number of fclose calls": 100,
   "duration": 6143681688
  }
 }
}

After:

{
 "timing_type": "hp_timing",
 "functions": {
  "fclose": {
   "number of FILEs": 1000000,
   "number of fclose calls": 100,
   "duration": 38998
  }
 }
}

> OK to keep my Reviewed-by: and push if you only change the copyright notice as requested.
>
> Reviewed-by: Carlos O'Donell <carlos@redhat.com>
>
> > Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> > ---
> >  benchtests/Makefile       |  6 ++++
> >  benchtests/README         |  1 +
> >  benchtests/bench-fclose.c | 74 +++++++++++++++++++++++++++++++++++++++
> >  3 files changed, 81 insertions(+)
> >  create mode 100644 benchtests/bench-fclose.c
> >
> > diff --git a/benchtests/Makefile b/benchtests/Makefile
> > index 7e73b8504e..b74b5fe1ad 100644
> > --- a/benchtests/Makefile
> > +++ b/benchtests/Makefile
> > @@ -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 \
> > diff --git a/benchtests/README b/benchtests/README
> > index 998ba9b2b4..15d014a407 100644
> > --- a/benchtests/README
> > +++ b/benchtests/README
> > @@ -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
> > index 0000000000..c1cdf03169
> > --- /dev/null
> > +++ b/benchtests/bench-fclose.c
> > @@ -0,0 +1,74 @@
> > +/* Benchmark fclose.
> > +   Copyright (C) 2024 Free Software Foundation, Inc.
>
> Should have "Copyright The GNU Toolchain Authors." since you are contributing under DCO
> a file that is a combination of copying from an existing benchtests file and adding
> your own changes.

Fixed in the v2 patch:

https://patchwork.sourceware.org/project/glibc/list/?series=34009

which I am checking it in.

Thanks.

> > +   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
>
> OK. I accept that on some systems this may fail due to the open files limit,
> but this is for microbenchmarking so the system needs to be adjusted for that.
> In the future we might want to run a small sanity checking program as the first
> thing in the microbenchmark run that tests for all of these conditions. OK for now.
>
> > +#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;
> > +}
>
> --
> Cheers,
> Carlos.
>
diff mbox series

Patch

diff --git a/benchtests/Makefile b/benchtests/Makefile
index 7e73b8504e..b74b5fe1ad 100644
--- a/benchtests/Makefile
+++ b/benchtests/Makefile
@@ -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 \
diff --git a/benchtests/README b/benchtests/README
index 998ba9b2b4..15d014a407 100644
--- a/benchtests/README
+++ b/benchtests/README
@@ -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
index 0000000000..c1cdf03169
--- /dev/null
+++ b/benchtests/bench-fclose.c
@@ -0,0 +1,74 @@ 
+/* Benchmark fclose.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   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;
+}