Repository: dotnet/runtime
File: src/coreclr/scripts/superpmi_benchmarks.py (Python)

Code before:
    partition_count = coreclr_args.partition_count
    partition_index = coreclr_args.partition_index
    benchmark_path = coreclr_args.benchmark_path
    benchmark_binary = coreclr_args.benchmark_binary
    dotnet_directory = os.path.join(performance_directory, "tools", "dotnet", arch)
    dotnet_exe = os.path.join(dotnet_directory, "dotnet")

    artifacts_directory = os.path.join(performance_directory, "artifacts")
    artifacts_packages_directory = os.path.join(artifacts_directory, "packages")
    project_file = os.path.join(performance_directory, benchmark_path)
    benchmarks_dll = os.path.join(artifacts_directory, benchmark_binary)

    # Workaround https://github.com/dotnet/sdk/issues/23430
    project_file = os.path.realpath(project_file)

    if is_windows:
        shim_name = "%JitName%"
        corerun_exe = "CoreRun.exe"
        script_name = "run_benchmarks.bat"
    else:
        shim_name = "$JitName"
        corerun_exe = "corerun"
        script_name = "run_benchmarks.sh"

    # Install the dotnet sdk using the script within the performance repo
    install_dotnet_sdk(python_path, performance_directory, arch)

    # Start with a "dotnet --info" to see what we've got.
    run_command([dotnet_exe, "--info"])

    tfm = "net11.0"
    os.environ["PERFLAB_TARGET_FRAMEWORKS"] = tfm

    env_for_restore = os.environ.copy()

    if is_windows:
        # Try to work around problem with random NuGet failures in "dotnet restore":
        #   error NU3037: Package 'System.Runtime 4.1.0' from source 'https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json':
        #     The repository primary signature validity period has expired. [C:\h\w\A3B008C0\w\B581097F\u\performance\src\benchmarks\micro\MicroBenchmarks.csproj]
        # Using environment variable specified in https://github.com/NuGet/NuGet.Client/pull/4259.
        env_for_restore["NUGET_EXPERIMENTAL_CHAIN_BUILD_RETRY_POLICY"] = "9,2000"

    # If `dotnet restore` fails, retry.
    num_tries = 3
    for try_num in range(num_tries):
        # On the last try, exit on fail
        exit_on_fail = try_num + 1 == num_tries
        (_, _, return_code) = run_command(
            [dotnet_exe, "restore", project_file, "--packages", artifacts_packages_directory],
            _exit_on_fail=exit_on_fail, _env=env_for_restore)
        if return_code == 0:

Review comment (performance):
can you just inline it here? It feels a bit unnecessary to add 20 LOC for just one line. We also don't need comments for the windows issue - it will fix itself once that PR to dotnet/performance is merged

Code after:
        shim_name = "$JitName"
        corerun_exe = "corerun"
        script_name = "run_benchmarks.sh"

    # Set up NuGet cache locations to be within the performance_directory which is on a separate disk
    # so that we don't fill up the OS disk (space is limited on helix machines)
    os.environ["NUGET_PLUGINS_CACHE_PATH"] = os.path.join(performance_directory, "NUGET_PLUGINS_CACHE_PATH")
    os.environ["NUGET_PACKAGES"] = os.path.join(performance_directory, "NUGET_PACKAGES")
    os.environ["NUGET_HTTP_CACHE_PATH"] = os.path.join(performance_directory, "NUGET_HTTP_CACHE_PATH")
    os.environ["NUGET_SCRATCH"] = os.path.join(performance_directory, "NUGET_SCRATCH")

    # Install the dotnet sdk using the script within the performance repo
    dotnet_script = os.path.join(performance_directory, "scripts", "dotnet.py")
    run_command([python_path,
                dotnet_script,
                "install",
                "--architecture",
                    arch,
                "--channels",
                "main",
                "--verbose"])

    # Start with a "dotnet --info" to see what we've got.
    run_command([dotnet_exe, "--info"])

    tfm = "net11.0"
    os.environ["PERFLAB_TARGET_FRAMEWORKS"] = tfm

    env_for_restore = os.environ.copy()

    if is_windows:
        # Try to work around problem with random NuGet failures in "dotnet restore":
        #   error NU3037: Package 'System.Runtime 4.1.0' from source 'https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json':
        #     The repository primary signature validity period has expired. [C:\h\w\A3B008C0\w\B581097F\u\performance\src\benchmarks\micro\MicroBenchmarks.csproj]
        # Using environment variable specified in https://github.com/NuGet/NuGet.Client/pull/4259.
        env_for_restore["NUGET_EXPERIMENTAL_CHAIN_BUILD_RETRY_POLICY"] = "9,2000"

    # If `dotnet restore` fails, retry.
    num_tries = 3
    for try_num in range(num_tries):
        # On the last try, exit on fail
        exit_on_fail = try_num + 1 == num_tries
        (_, _, return_code) = run_command(
            [dotnet_exe, "restore", project_file, "--packages", artifacts_packages_directory],
            _exit_on_fail=exit_on_fail, _env=env_for_restore)
        if return_code == 0:
            # It succeeded!
            break
        print("Try {} of {} failed with error code {}: trying again".format(try_num + 1, num_tries, return_code))
        # Sleep 5 seconds before trying again
        time.sleep(5)