From 124146bf56ad7142e9f33cf3cf5356d9fdf42d35 Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 29 Apr 2025 16:16:49 +0200 Subject: [PATCH] spawn a worker process even with -j 1 --- CHANGES.md | 13 +++++++++++-- lib/main.ml | 9 +++++---- lib_core/cli.ml | 24 +++++++++++++++--------- lib_core/cli.mli | 2 +- lib_core/log.ml | 8 ++++++-- lib_core/test.ml | 7 +++++-- lib_core/test.mli | 2 +- lib_js/main.ml | 2 +- 8 files changed, 45 insertions(+), 22 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 5457667a..3aab5e20 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,17 +6,22 @@ ### New Features +- `-j 1` now causes Tezt to spawn a worker process. + Previously, Tezt would run in single-process mode; + only values of 2 or more would would trigger multi-process mode. + If `-j` is not specified, Tezt still runs in single-process mode. + - Added `get_runner_pid` to the `tezt.scheduler` library. - Added `--record-mem-peak`. It causes Tezt to regularly measure the memory usage of tests and to record the peak in reports when using `--record` or `--junit`. - Only works with `-j N` with `N` being at least 2. + Only works with `-j`. - Added `--mem-warn`. It causes Tezt to regularly measure the memory usage of tests and to warn if it goes beyond a threshold. - Only works with `-j N` with `N` being at least 2. + Only works with `-j`. - Added `--mem-poll-frequency`, which controls how often memory usage is measured when using `--record-mem-peak` or `--mem-warn`. @@ -30,6 +35,10 @@ - `Test.test_result` contains a new field: `peak_memory_usage`. +- `Cli.Options.job_count` is now an `int option` instead of an `int`. + Same for `~worker_count`. + The latter only affects custom schedulers. + ## Version 4.2.0 ### New Features diff --git a/lib/main.ml b/lib/main.ml index 1280d7e8..36abdd84 100644 --- a/lib/main.ml +++ b/lib/main.ml @@ -457,10 +457,11 @@ module Scheduler : Test.SCHEDULER = struct run_single_process ~on_worker_available let run ~on_worker_available ~worker_count continue = - (if worker_count = 1 then run_single_process ~on_worker_available - else - try run_multi_process ~on_worker_available ~worker_count - with exn -> internal_scheduler_error "%s" (Printexc.to_string exn)) ; + (match worker_count with + | None -> run_single_process ~on_worker_available + | Some worker_count -> ( + try run_multi_process ~on_worker_available ~worker_count + with exn -> internal_scheduler_error "%s" (Printexc.to_string exn))) ; continue () let get_current_worker_id () = !current_worker_id diff --git a/lib_core/cli.ml b/lib_core/cli.ml index 2afef912..82f887eb 100644 --- a/lib_core/cli.ml +++ b/lib_core/cli.ml @@ -194,7 +194,7 @@ module Options = struct false let job_count = - Clap.default_int + Clap.optional_int ~long:"job-count" ~short:'j' ~placeholder:"COUNT" @@ -203,13 +203,19 @@ module Options = struct --suggest-jobs, set the number of target jobs for --suggest-jobs \ instead.\n\n\ If environment variable TEZT_JOB_COUNT is a positive integer, default \ - value is the value of TEZT_JOB_COUNT. Else, default value is 1." - (match Sys.getenv_opt "TEZT_JOB_COUNT" with - | None -> 1 - | Some s -> ( - match int_of_string_opt s with - | None -> 1 - | Some n -> if n > 0 then n else 1)) + value is the value of TEZT_JOB_COUNT." + () + + let job_count = + match job_count with + | None -> ( + match Sys.getenv_opt "TEZT_JOB_COUNT" with + | None -> None + | Some s -> ( + match int_of_string_opt s with + | None -> None + | Some n -> if n > 0 then Some n else None)) + | Some _ -> job_count let test_arg_type = Clap.typ @@ -380,7 +386,7 @@ module Logs = struct ~section ~set_long:"log-worker-id" ~description: - "Decorate logs with worker IDs when --job-count is more than 1." + "Decorate logs with worker IDs when --job-count is specified." false let commands = diff --git a/lib_core/cli.mli b/lib_core/cli.mli index 6f78fcab..9e5b7f6c 100644 --- a/lib_core/cli.mli +++ b/lib_core/cli.mli @@ -96,7 +96,7 @@ module Options : sig val resume : bool (** [--job-count] *) - val job_count : int + val job_count : int option (** [--seed] *) val seed : int option diff --git a/lib_core/log.ml b/lib_core/log.ml index 79328870..3a8e39b2 100644 --- a/lib_core/log.ml +++ b/lib_core/log.ml @@ -229,7 +229,11 @@ let output_worker_id = (* [Cli.Options.job_count] can contain a dummy value (0) since we did not call [Clap.close] yet. *) let padding = - 1 + int_of_float (log10 (float_of_int (max 1 Cli.Options.job_count))) + 1 + + int_of_float + (log10 + (float_of_int + (max 1 (Option.value Cli.Options.job_count ~default:1)))) in fun output worker_id -> match worker_id with @@ -251,7 +255,7 @@ let log_line_to ~use_colors output (f timestamp) ; output "] ") ; if use_colors then Option.iter output color ; - if Cli.Logs.worker_id && Cli.Options.job_count > 1 then ( + if Cli.Logs.worker_id && Cli.Options.job_count <> None then ( output "[" ; output_worker_id output worker_id ; output "]" ; diff --git a/lib_core/test.ml b/lib_core/test.ml index 7cdf2596..d570b0dc 100644 --- a/lib_core/test.ml +++ b/lib_core/test.ml @@ -1030,7 +1030,10 @@ let skip_test () = list) let suggest_jobs () = - let jobs = split_tests_into_balanced_jobs Cli.Options.job_count in + let jobs = + split_tests_into_balanced_jobs + (Option.value Cli.Options.job_count ~default:1) + in let job_count = Array.length jobs in (* Jobs are allocated, now display them. *) let display_job ~negate (total_job_time, job_tests) = @@ -1206,7 +1209,7 @@ module type SCHEDULER = sig The result of this request, [response], is then given to [on_response]. *) val run : on_worker_available:(unit -> (request * (response -> unit)) option) -> - worker_count:int -> + worker_count:int option -> (unit -> unit) -> unit diff --git a/lib_core/test.mli b/lib_core/test.mli index 99d8e64d..46289e88 100644 --- a/lib_core/test.mli +++ b/lib_core/test.mli @@ -193,7 +193,7 @@ module type SCHEDULER = sig The last argument is a continuation to call once there is nothing left to do. *) val run : on_worker_available:(unit -> (request * (response -> unit)) option) -> - worker_count:int -> + worker_count:int option -> (unit -> unit) -> unit diff --git a/lib_js/main.ml b/lib_js/main.ml index 61296c24..e113089c 100644 --- a/lib_js/main.ml +++ b/lib_js/main.ml @@ -63,7 +63,7 @@ module Scheduler : Test.SCHEDULER = struct unit let run ~on_worker_available ~worker_count continue = - if worker_count <> 1 then + if worker_count <> None then Log.warn "The -j argument is ignored when using Tezt_js.run." ; let (_ : unit Lwt.t) = run ~on_worker_available continue in () -- GitLab