From patchwork Sat Sep 7 15:48:37 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: 1982155 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=mIz1WPW/; 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 4X1Hb93vRkz1y1D for ; Sun, 8 Sep 2024 01:49:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E94C385E457 for ; Sat, 7 Sep 2024 15:49:23 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2c.google.com (mail-oa1-x2c.google.com [IPv6:2001:4860:4864:20::2c]) by sourceware.org (Postfix) with ESMTPS id D5E5F385C6C1 for ; Sat, 7 Sep 2024 15:49:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D5E5F385C6C1 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 D5E5F385C6C1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:4860:4864:20::2c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725724145; cv=none; b=ScTnkV33JKA5J0Av4YOYDU0nU1rQf8vNqqXCYm8Yvy9Oh8WEvetT42R/qpdb+a2KGhte/waUIFv0XOqz60kx/D/C0xDIPzXYVtp47OnBpYDvjDW7lRZg6malfSiNV0FeANHoeuSPLSIZXFtHPJLJsFwGuk43bb5bhNiR6oD8Pcg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725724145; c=relaxed/simple; bh=ztwOefAd2MkxT2tKJC/vAN0kTiRfd1dRIe/dAxv2SMo=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=WdmyQ7Xy85WL0lJKCM+tl5UDrJNVHk7GwVuDNSCHlRqsC261UK5UAQgh2vho9UoFzqs8yFeCH6DB4YfoMwWuCen8MuJRZIPaeBfZwZEHlj/E0xzTs4A67OhoW9ASqprPsll+iE4aBvhcpZWo8RVaAEL6JfY2X35eYKwXekNAE+I= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-277fa3de06fso1586880fac.0 for ; Sat, 07 Sep 2024 08:49:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725724141; x=1726328941; 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=WFh/APp5/36erXyA/v2XtfAO9duJSsevP/AHOYv3OCc=; b=mIz1WPW/rr6om7ETzECEXqBl7VwO9hVBXEJApZlXzSp0+5xnR5YH8g/ZBnPrLlgc9K RK9N4KRl1sZh+kcaD7ChySDndz3WD/b7tUm0ciPTxIRGhqJUdoNSKOY+Ym6vVE6NmlN8 eD/AYTaVlNh+ePlRp+gr+rOVQm5Ib61tL0Okp9R/6kmrXhmZILephg1jhIQAKkIIS9Kg 75Bcuy5VcWALzRDKbe8xcae+yUCRkYr2sVizej/7OE1uM06CHPZ4tAZKhKSAexa19nu3 zQ/3wiO2Q7HpsvVlpCQsUSHoXp2njM42eWyPG3O1B/8gcTCbeO1P2dINNrFWGRuvtQk3 zuUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725724141; x=1726328941; 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=WFh/APp5/36erXyA/v2XtfAO9duJSsevP/AHOYv3OCc=; b=Xr4l3wgL8jyYvsE2WRZULIQgunQlTGZpJ6ZHbsC8MV6AXKm0enDiKViBxSiSOyYhOf 5mVepBJQGFDm6PoDnDtcxsTMbT+dIDV/NYUm1eNxQW5Zn2bPcKI3BpZytvp3fnFQooYi aPKeTF/jYeK4JN3PAfQT06u3pikeCneoW3CFB/iuB9xUzmh+WvV99cacaAbGOLkjOnmb JOS7h3wvB0oYeIBhscjUlI0j4aW/pFK7HmBhWwOJCMAXI8t8iJZfV0se9BpRcAp+k+0Y r+yOq+aVpO2qUiTCYtKcHAmWkQ+hN8U5eLJNy3aGeTfbvJI2Ic/8Bz2VXheMU6u62kWj 8bLA== X-Gm-Message-State: AOJu0Yx89bAIDqamb+36k9DyVsuxhAHcPZ4iLcQjLf0bM9jrqsd9vJEk he+JC1xAZaccOK4/G+l2v6nQVluLq5KTvj2EWbsPjkiv7q5G61/XyXWPbA== X-Google-Smtp-Source: AGHT+IEwAAlURHDVXvd7BixhpMNxz0uCTFkZAAk5pkFumO1/8ZB0SEtQKH2pgTslwPvZ69vOUSXYAg== X-Received: by 2002:a05:6871:153:b0:278:aa3:1d0f with SMTP id 586e51a60fabf-27b82fab32bmr7200315fac.34.1725724141532; Sat, 07 Sep 2024 08:49:01 -0700 (PDT) Received: from visitorckw-System-Product-Name.. ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-718e58ca745sm1016913b3a.88.2024.09.07.08.49.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Sep 2024 08:49:00 -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 v5 1/2] benchtests: Add benchmark test for bsearch Date: Sat, 7 Sep 2024 23:48:37 +0800 Message-Id: <20240907154838.2781255-2-visitorckw@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240907154838.2781255-1-visitorckw@gmail.com> References: <20240907154838.2781255-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 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. Signed-off-by: Kuan-Wei Chiu --- Changes in v5: - Move 'array-size', 'key-pattern', and 'contained' fields into the 'results' array. - Add 'simple' field to the 'results' array. - Add tests for 'key-pattern' = 'descending' and 'contained' = 'no'. - Drop the full benchmark log from the commit message as it is becoming too verbose. benchtests/Makefile | 1 + benchtests/bench-bsearch.c | 136 +++++++++++++++++++++++++++++++++++++ 2 files changed, 137 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..87c9b5eb91 --- /dev/null +++ b/benchtests/bench-bsearch.c @@ -0,0 +1,136 @@ +/* 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_asc (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 int +comp_desc (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, bool ascending, bool contained) +{ + 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] = (ascending ? i : ARRAY_SIZE - 1 - i) << 1; + } + + json_element_object_begin (json_ctx); + json_attr_uint (json_ctx, "array-size", ARRAY_SIZE); + json_attr_string (json_ctx, "key-pattern", ascending ? "ascending" : "descending"); + json_attr_string (json_ctx, "contained", contained ? "yes" : "no"); + json_attr_string (json_ctx, "simple", "yes"); + + TIMING_NOW (start); + + for (i = 0; i < iters; ++i) + { + key = (i % ARRAY_SIZE << 1) + !contained; + res = bsearch(&key, arr, ARRAY_SIZE, sizeof(arr[0]), ascending ? comp_asc : comp_desc); + } + + TIMING_NOW (stop); + + TIMING_DIFF (cur, start, stop); + + json_attr_double (json_ctx, "timing", (double) cur / (double) iters); + json_element_object_end (json_ctx); +} + +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"); + + for (int ascending = 0; ascending < 2; ++ascending) + { + for (int contained = 0; contained < 2; ++contained) + { + do_bench(&json_ctx, ascending, contained); + } + } + + 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