From patchwork Wed Sep 4 16:01:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuan-Wei Chiu X-Patchwork-Id: 1980916 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=K4jFWTOZ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WzS1p1Xfhz1yXY for ; Thu, 5 Sep 2024 02:02:38 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BF0EA3864805 for ; Wed, 4 Sep 2024 16:02:35 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id B555C3861036 for ; Wed, 4 Sep 2024 16:02:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B555C3861036 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B555C3861036 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::633 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725465738; cv=none; b=ENLAtwWkLRVtd7KR5l8rdVB1I+XLaeRw0AyGl4rkeEMjklRdvnIILc+bCalNk51HsK0ExmAXqLfBP9LAul01qQY9Wk/KJpwoBOCUhKerf1QhV4uHbuEjjvz7fEz1xGnOtagXzMj8NwsBVOsirWpJWDudZFbSYjAYB4tcVhU1mI8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725465738; c=relaxed/simple; bh=dQjAIzpVP6o9E5sJ6W0OLrbdWfOuQU0nS1DLpSNP3Ik=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=FsKSsBu8kee8HXs0PPWnk0W0Q9dDF6qxvtjkT44Asfekx22cg5Fz1y8s7ZYDeWF4j22K8wbLatI8HQwQFsX2QVLK9xe26F9sVMz6cSzhdu202GukAiefyuK7K+2gUxgpWYxZLYqUMDykWT0bheWYPLmc/4/cCRXKJEOcqFm0reA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2055f630934so26978795ad.1 for ; Wed, 04 Sep 2024 09:02:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725465735; x=1726070535; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jdGOEG7JmbbLRQXGg4yqF7s8DJdmkUcEjKJeB+oCRKs=; b=K4jFWTOZ5TM1uDFuq/AhYnlPg73vqT7Pp6SnGIK8cbH87cst+Y2mYngQ68bJbCUHeA 6GfuLIpgsiEuAIPHPPjyyBMIVMVdSddTQbmc19vE6iWhN58A//A82D1VuiycK120AK8y L+FlgfdmAquzZ9y1I29xjn51ynw+wwYGTfyI4/gCCaLEZN0keE32SyOjC0StHyQAr7Je jYpL+OyAG5VI6V672IfP713Zr1d9CUgaZZ7ptM6aay2kziv59pEoeC8AAF+vGrNcT8C3 DeYbAdWvYnK1MyaI3HNOkgX4QtueSnO8Wl2kbVds70rIteiEFOx9UYSA0hw1NFA/Puqo vedg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725465735; x=1726070535; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jdGOEG7JmbbLRQXGg4yqF7s8DJdmkUcEjKJeB+oCRKs=; b=blPmqonA7pKZoHZ3M8upYjJv05SPlyc8LrvktpamMF4aJU2OW1pskYlDMgO93o/tb6 OTw5M4xdSK2ppO8RE6akm5TMEwk3r3iOwcnjjs3ouOuvJaCySkaAYtuXqRhZqPGtxMX4 0EhdM1PjQpOX991GqAmLbSHTGWibrP2SICAZSWlb5v3Zq1rsS5O9YwxLHewL8kybBNTi 8Vdl4jwDdHJjA7vqPFTREcBSyeNSfWaOJproLfwtM8OYvUuADCnsyDfTIgDiwwZPjzUk 2zZRRy5S17ckzPYPkT4zVDD7ltmpPfC48bSLF6FaOW0qYtunIcoDqTHtQBN5Utr7sMh3 pITQ== X-Gm-Message-State: AOJu0Yy48Kjrt8APF3KEQnLFMhpCcg7GOpfQl9FgZfmVpJRHGj3pGfIw Gt93R6cT0C8A2/sRN/PEVr7v9TSiL8S81krp8LMuvPx4zhoirC30FvidqQ== X-Google-Smtp-Source: AGHT+IG/jgZSkXLnST2s0tdCy8Yiefy1hqmbnXOj8fdveb0vTgOFEC02faUwv3AnJA1yn/lT7DZgVw== X-Received: by 2002:a17:903:3608:b0:205:5005:957b with SMTP id d9443c01a7336-205500597a2mr163455105ad.37.1725465734716; Wed, 04 Sep 2024 09:02:14 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea38c91sm15193565ad.169.2024.09.04.09.02.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 09:02:14 -0700 (PDT) From: Kuan-Wei Chiu To: libc-alpha@sourceware.org Cc: goldstein.w.n@gmail.com, jserv@ccns.ncku.edu.tw, Kuan-Wei Chiu Subject: [PATCH v4 1/2] benchtests: Add benchmark test for bsearch Date: Thu, 5 Sep 2024 00:01:39 +0800 Message-Id: <20240904160140.2743366-2-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240904160140.2743366-1-visitorckw@gmail.com> References: <20240904160140.2743366-1-visitorckw@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Introduce a benchmark test for the bsearch function to evaluate its performance. Example bench-bsearch.out: { "timing_type": "hp_timing", "functions": { "bsearch": { "bench-variant": "default", "array-size": 100000, "key-pattern": "ascending", "contained": "yes", "results": [121.735] } } } Signed-off-by: Kuan-Wei Chiu --- Changes in v4: - Add three fields: "array-size," "key-pattern," and "contained" to benchmark json output. - Update the benchmark result format in commit messages. benchtests/Makefile | 1 + benchtests/bench-bsearch.c | 114 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 benchtests/bench-bsearch.c diff --git a/benchtests/Makefile b/benchtests/Makefile index d228e9e68a..955b766756 100644 --- a/benchtests/Makefile +++ b/benchtests/Makefile @@ -253,6 +253,7 @@ hash-benchset := \ stdlib-benchset := \ arc4random \ + bsearch \ random-lock \ strtod \ # stdlib-benchset diff --git a/benchtests/bench-bsearch.c b/benchtests/bench-bsearch.c new file mode 100644 index 0000000000..75bf812290 --- /dev/null +++ b/benchtests/bench-bsearch.c @@ -0,0 +1,114 @@ +/* Measure bsearch functions. + Copyright (C) 2022-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 + . */ + +#define TEST_MAIN +#define TEST_NAME "bsearch" + +#define ARRAY_SIZE 100000 +#define LOOP_ITERS 100000000 + +/* Directly including leads to the use of an inline version + of bsearch(), which may cause our test cases to be optimized away by + the compiler due to predictability. To address this, we should + include directly and replace __extern_inline + with __attribute__((noinline)) to ensure the compiler does not + inline the function. Additionally, we need to add some macros + required for compilation. */ +#include +#define __extern_inline __attribute__((noinline)) +#define __GNUC_PREREQ(x, y) 0 +typedef int (*__compar_fn_t) (const void *, const void *); +#include +#undef __extern_inline +#undef __GNUC_PREREQ + +#include "json-lib.h" +#include "bench-timing.h" + +int arr[ARRAY_SIZE]; + +static int +comp (const void *p1, const void *p2) +{ + int x1 = *(int *) p1; + int x2 = *(int *) p2; + + if (x1 < x2) + return -1; + if (x1 > x2) + return 1; + return 0; +} + +static void +do_bench (json_ctx_t *json_ctx) +{ + size_t i, iters = LOOP_ITERS; + timing_t start, stop, cur; + int key; + volatile __attribute__((__unused__)) void *res; + + for (i = 0; i < ARRAY_SIZE; ++i) + { + arr[i] = i; + } + + TIMING_NOW (start); + + for (i = 0; i < iters; ++i) + { + key = i % ARRAY_SIZE; + res = bsearch(&key, arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), comp); + } + + TIMING_NOW (stop); + + TIMING_DIFF (cur, start, stop); + + json_element_double (json_ctx, (double) cur / (double) iters); +} + +int +do_test (void) +{ + 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, TEST_NAME); + json_attr_string (&json_ctx, "bench-variant", "default"); + json_attr_uint (&json_ctx, "array-size", ARRAY_SIZE); + json_attr_string (&json_ctx, "key-pattern", "ascending"); + json_attr_string (&json_ctx, "contained", "yes"); + + json_array_begin (&json_ctx, "results"); + + do_bench(&json_ctx); + + json_array_end (&json_ctx); + json_attr_object_end (&json_ctx); + json_attr_object_end (&json_ctx); + json_document_end (&json_ctx); + + return 0; +} + +#include