From patchwork Tue Sep 3 06:30:55 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: 1979841 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=kv0t6Emh; 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 4WybPM3wcdz1yfv for ; Tue, 3 Sep 2024 16:31:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A40A385EC29 for ; Tue, 3 Sep 2024 06:31:33 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 7D4BE3858401 for ; Tue, 3 Sep 2024 06:31:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7D4BE3858401 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 7D4BE3858401 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725345074; cv=none; b=enBCz68swnqNgaDEqKUDha28Bdz9U7HYyfqfQ+NmDQg2bW4vPVD9uKAsD60AJVVPRl9XV6dUzPnjN23Nbu4PJXdI5FVHhjSSc34Qqoy5YAhrgfEMF3L0NerlYrq4QKl9oKX+2d+47WFOtqQZ1bKHjzrTujMRH+3FDXQh3K7MXzI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725345074; c=relaxed/simple; bh=Ihh0vlTQhLMchkmNiulE4YfLSORBz0oRV/07zyJnOdw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=vp1oyU79pMJk9UESbCxYph+X8W/zmizErK0wh2GkSXQM2DVkmaKenDlq9IDg5npUn9CeuwK7jYDXhxi8PXsUXGyvXiOMnTD3FtJca8wyftmXa1D9akZMYKWuXRSiEgVGsQtlZamxJyumcUnm1sNW5VJfGQZHzqRY51XKj4lWQug= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-205722ba00cso11850155ad.0 for ; Mon, 02 Sep 2024 23:31:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725345070; x=1725949870; 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=OpHoWha1exCVulDdKRFpoZTgnTDKZvqx5mO0AACNTH4=; b=kv0t6EmhTuq6CpqpbXp/tyLEp2culVH1QEPmaN9ssiSDOdzTmMOxxXezE0pCKQxwwe Gtz9g7+9Fxa3eSsAUpoR22fWBesGG1PrsRNNRu0Imciu6rpqNL1lFrqW9AFfBLn0TQyx 4S1kh0A99wvoPv1s3roF/rswgAc77SMeSp2je0F5Ps+s03z1na74RQMnPPA056wljaoZ I8SDGAN6mLRTJMcKRl0WxUlkpJZUeE5rrxObvovwjlGkEkjSnVvxfjNAuZtvU2ZGnuXv 2S73Nsg1o9ympy45U94jtlS/Qz/aUsT98e/NYVDKXOWAMFyt2/JMP0h3D+Xv51YukJjC qp1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725345070; x=1725949870; 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=OpHoWha1exCVulDdKRFpoZTgnTDKZvqx5mO0AACNTH4=; b=vPKSQTAD6O2YwMhVlfR1oca6WTiVLQEzOQZDXQ+gB9X+aDKBb8WNQ5jhZzxnOgQRjn 6rjDQsgJ+ZNliiVBo6akyQYiBeASRJkFr2B1CIQPpPzGTytUXwN3/0SE68V9wyODASqi a2VGie3b6/DSIvp1g6gYc3IYpg7gGfEXOPL91w3iBYRBQBLQa1DuimpM1kdRCSTtnWLA VAU+SlxRkBF9/YHQIVmiy2Biu94REj7t24MV23LfBU9wo7ZZmZ7NogFm7cRqpO9o43gn xU20JClUBW38jpjkzRuTq6wnIOmw+DPA04QC5PwOA7JLlU7R1aUKuzvKyrGi/SGKK+71 Ay0w== X-Gm-Message-State: AOJu0YyW8pfq7VaCjuoUt+rloqFFkqs6LddMQv5SLL8z+Gtsbl7DtvEw F66nlXGABfubcDPnDA/CthnwzU3BPhGwxJ2XAOVO0nX6VcOWulDQC7WWfQ== X-Google-Smtp-Source: AGHT+IHYCY2aK/ajd9eJsqiKCyVuYZI4xnI7GLHYitn5jtT1J6l7Qe/Z1KktxTqrbRZlCoEWKqNoDg== X-Received: by 2002:a17:902:d2c2:b0:202:38d8:16c with SMTP id d9443c01a7336-2054733519dmr75523095ad.37.1725345070281; Mon, 02 Sep 2024 23:31:10 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-205784d556dsm24803815ad.269.2024.09.02.23.31.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 23:31:09 -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 v3 1/2] benchtests: Add benchmark test for bsearch Date: Tue, 3 Sep 2024 14:30:55 +0800 Message-Id: <20240903063056.2724742-2-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240903063056.2724742-1-visitorckw@gmail.com> References: <20240903063056.2724742-1-visitorckw@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 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", "results": [121.887] } } } Signed-off-by: Kuan-Wei Chiu --- Changes in v3: - Sort the stdlib-benchset entries in Makefile to resolve CI failures. benchtests/Makefile | 1 + benchtests/bench-bsearch.c | 110 +++++++++++++++++++++++++++++++++++++ 2 files changed, 111 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..9ec88af60a --- /dev/null +++ b/benchtests/bench-bsearch.c @@ -0,0 +1,110 @@ +/* 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_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