From nobody Sun Apr 13 04:33:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1640192023903139.01688269901513; Wed, 22 Dec 2021 08:53:43 -0800 (PST) Received: from localhost ([::1]:47528 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n04s3-000052-0A for importer@patchew.org; Wed, 22 Dec 2021 11:53:43 -0500 Received: from eggs.gnu.org ([209.51.188.92]:39682) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n04qy-0006N9-Kj for qemu-devel@nongnu.org; Wed, 22 Dec 2021 11:52:36 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:54878) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n04qw-0006Ul-Mc for qemu-devel@nongnu.org; Wed, 22 Dec 2021 11:52:36 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-13-nmE8it2dM6iTi5sIIECj-Q-1; Wed, 22 Dec 2021 11:52:27 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A30CC10B7440; Wed, 22 Dec 2021 16:52:26 +0000 (UTC) Received: from localhost (unknown [10.39.194.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 07F827B6C1; Wed, 22 Dec 2021 16:52:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1640191949; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B8z42Zt+W04uVmpA92VUdOl0PZnsS0Ksw9rf3I63QXs=; b=PbJPZ0H68z+Yv1F9nvrmYAT4KaQR/ydCiVWJhz+XMBDE/c1d1u1fenzP2Ye+r5n695Z4bU SqS7hq+x4XSPRNgWX1gTkO9A5IeMSGJae1l2jorPeRiQanHusQLXyOvWQLTW1pRiJUprqS AwjkmP2f3mgmKJlviCvugz0JhzXsOps= X-MC-Unique: nmE8it2dM6iTi5sIIECj-Q-1 From: Hanna Reitz To: qemu-block@nongnu.org Subject: [PULL 3/3] iotests: check: multiprocessing support Date: Wed, 22 Dec 2021 17:52:08 +0100 Message-Id: <20211222165208.693159-4-hreitz@redhat.com> In-Reply-To: <20211222165208.693159-1-hreitz@redhat.com> References: <20211222165208.693159-1-hreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=hreitz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=hreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.203, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Hanna Reitz , Richard Henderson , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1640192025073100001 Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Add -j parameter, to run tests in several jobs simultaneously. For realization - simply utilize multiprocessing.Pool class. Notes: 1. Of course, tests can't run simultaneously in same TEST_DIR. So, use subdirectories TEST_DIR/testname/ and SOCK_DIR/testname/ instead of simply TEST_DIR and SOCK_DIR 2. multiprocessing.Pool.starmap function doesn't support passing context managers, so we can't simply pass "self". Happily, we need self only for read-only access, and it just works if it is defined in global space. So, add a temporary link TestRunner.shared_self during run_tests(). Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20211203122223.2780098-4-vsementsov@virtuozzo.com> Reviewed-by: John Snow Tested-by: John Snow Signed-off-by: Hanna Reitz --- tests/qemu-iotests/check | 4 +- tests/qemu-iotests/testrunner.py | 69 ++++++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/tests/qemu-iotests/check b/tests/qemu-iotests/check index 43a4b694cc..0c27721a41 100755 --- a/tests/qemu-iotests/check +++ b/tests/qemu-iotests/check @@ -34,6 +34,8 @@ def make_argparser() -> argparse.ArgumentParser: help=3D'show me, do not run tests') p.add_argument('-makecheck', action=3D'store_true', help=3D'pretty print output for make check') + p.add_argument('-j', dest=3D'jobs', type=3Dint, default=3D1, + help=3D'run tests in multiple parallel jobs') =20 p.add_argument('-d', dest=3D'debug', action=3D'store_true', help=3D'de= bug') p.add_argument('-p', dest=3D'print', action=3D'store_true', @@ -165,6 +167,6 @@ if __name__ =3D=3D '__main__': with TestRunner(env, makecheck=3Dargs.makecheck, color=3Dargs.color) as tr: paths =3D [os.path.join(env.source_iotests, t) for t in tests] - ok =3D tr.run_tests(paths) + ok =3D tr.run_tests(paths, args.jobs) if not ok: sys.exit(1) diff --git a/tests/qemu-iotests/testrunner.py b/tests/qemu-iotests/testrunn= er.py index a9f2feb58c..0feaa396d0 100644 --- a/tests/qemu-iotests/testrunner.py +++ b/tests/qemu-iotests/testrunner.py @@ -26,6 +26,7 @@ import json import termios import sys +from multiprocessing import Pool from contextlib import contextmanager from typing import List, Optional, Iterator, Any, Sequence, Dict, \ ContextManager @@ -126,6 +127,31 @@ def __init__(self, status: str, description: str =3D '= ', =20 =20 class TestRunner(ContextManager['TestRunner']): + shared_self =3D None + + @staticmethod + def proc_run_test(test: str, test_field_width: int) -> TestResult: + # We are in a subprocess, we can't change the runner object! + runner =3D TestRunner.shared_self + assert runner is not None + return runner.run_test(test, test_field_width, mp=3DTrue) + + def run_tests_pool(self, tests: List[str], + test_field_width: int, jobs: int) -> List[TestResul= t]: + + # passing self directly to Pool.starmap() just doesn't work, becau= se + # it's a context manager. + assert TestRunner.shared_self is None + TestRunner.shared_self =3D self + + with Pool(jobs) as p: + results =3D p.starmap(self.proc_run_test, + zip(tests, [test_field_width] * len(tests)= )) + + TestRunner.shared_self =3D None + + return results + def __init__(self, env: TestEnv, makecheck: bool =3D False, color: str =3D 'auto') -> None: self.env =3D env @@ -219,11 +245,16 @@ def find_reference(self, test: str) -> str: =20 return f'{test}.out' =20 - def do_run_test(self, test: str) -> TestResult: + def do_run_test(self, test: str, mp: bool) -> TestResult: """ Run one test =20 :param test: test file path + :param mp: if true, we are in a multiprocessing environment, use + personal subdirectories for test run + + Note: this method may be called from subprocess, so it does not + change ``self`` object in any way! """ =20 f_test =3D Path(test) @@ -249,6 +280,12 @@ def do_run_test(self, test: str) -> TestResult: =20 args =3D [str(f_test.resolve())] env =3D self.env.prepare_subprocess(args) + if mp: + # Split test directories, so that tests running in parallel do= n't + # break each other. + for d in ['TEST_DIR', 'SOCK_DIR']: + env[d] =3D os.path.join(env[d], f_test.name) + Path(env[d]).mkdir(parents=3DTrue, exist_ok=3DTrue) =20 t0 =3D time.time() with f_bad.open('w', encoding=3D"utf-8") as f: @@ -291,23 +328,32 @@ def do_run_test(self, test: str) -> TestResult: casenotrun=3Dcasenotrun) =20 def run_test(self, test: str, - test_field_width: Optional[int] =3D None) -> TestResult: + test_field_width: Optional[int] =3D None, + mp: bool =3D False) -> TestResult: """ Run one test and print short status =20 :param test: test file path :param test_field_width: width for first field of status format + :param mp: if true, we are in a multiprocessing environment, don't= try + to rewrite things in stdout + + Note: this method may be called from subprocess, so it does not + change ``self`` object in any way! """ =20 last_el =3D self.last_elapsed.get(test) start =3D datetime.datetime.now().strftime('%H:%M:%S') =20 if not self.makecheck: - self.test_print_one_line(test=3Dtest, starttime=3Dstart, - lasttime=3Dlast_el, end=3D'\r', + self.test_print_one_line(test=3Dtest, + status =3D 'started' if mp else '...', + starttime=3Dstart, + lasttime=3Dlast_el, + end =3D '\n' if mp else '\r', test_field_width=3Dtest_field_width) =20 - res =3D self.do_run_test(test) + res =3D self.do_run_test(test, mp) =20 end =3D datetime.datetime.now().strftime('%H:%M:%S') self.test_print_one_line(test=3Dtest, status=3Dres.status, @@ -321,7 +367,7 @@ def run_test(self, test: str, =20 return res =20 - def run_tests(self, tests: List[str]) -> bool: + def run_tests(self, tests: List[str], jobs: int =3D 1) -> bool: n_run =3D 0 failed =3D [] notrun =3D [] @@ -332,9 +378,16 @@ def run_tests(self, tests: List[str]) -> bool: =20 test_field_width =3D max(len(os.path.basename(t)) for t in tests) = + 2 =20 - for t in tests: + if jobs > 1: + results =3D self.run_tests_pool(tests, test_field_width, jobs) + + for i, t in enumerate(tests): name =3D os.path.basename(t) - res =3D self.run_test(t, test_field_width=3Dtest_field_width) + + if jobs > 1: + res =3D results[i] + else: + res =3D self.run_test(t, test_field_width) =20 assert res.status in ('pass', 'fail', 'not run') =20 --=20 2.33.1