Skip to content

Some create_benchmark.py script enhancements. #71867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions benchmark/scripts/Template.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//===--- {name}.swift -------------------------------------------===//
//===--- {name}.swift {padding}---===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Copyright (c) {year} Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -12,7 +12,7 @@

import TestsUtils

public let {name} = [
public let benchmarks = [
BenchmarkInfo(name: "{name}", runFunction: run_{name}, tags: [.validation, .api]),
]

Expand Down
25 changes: 15 additions & 10 deletions benchmark/scripts/create_benchmark.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import argparse
import datetime
import os
import re

Expand Down Expand Up @@ -46,18 +47,22 @@ def create_benchmark_file(name):
and places it in the `single-source` directory.
"""

file_text = ""
template_path = create_relative_path("Template.swift")
benchmark_template = ""
with open(template_path, "r") as f:
benchmark_template = "".join(f.readlines())
file_text = "".join(f.readlines())

# fill in template with benchmark name.
formatted_template = benchmark_template.format(name=name)
# fill in missing template details
file_text = file_text.format(
name=name,
padding="-" * (56 - len(name)),
year=datetime.date.today().year
)

relative_path = create_relative_path("../single-source/")
source_file_path = os.path.join(relative_path, name + ".swift")
with open(source_file_path, "w") as f:
f.write(formatted_template)
file_path_prefix = create_relative_path("../single-source/")
file_path = os.path.join(file_path_prefix, name + ".swift")
with open(file_path, "w") as f:
f.write(file_text)


def add_import_benchmark(name):
Expand Down Expand Up @@ -119,9 +124,9 @@ def add_register_benchmark(name):

file_new_contents = insert_line_alphabetically(
name,
"registerBenchmark(" + name + ")\n",
"register(" + name + ".benchmarks)\n",
file_contents,
r"registerBenchmark\(([a-zA-Z]+)\)",
r"register\(([a-zA-Z]+)\.benchmarks\)",
)
with open(relative_path, "w") as f:
for line in file_new_contents:
Expand Down