benchmark-toolbox: add benchmark-toolbox

a unified benchmarking tool
This commit is contained in:
souldbminersmwc
2026-06-07 21:10:53 -04:00
parent 8ebf887956
commit 4a5e889c40
1252 changed files with 356811 additions and 491 deletions

View File

@@ -0,0 +1,30 @@
#!/bin/bash
cd "$( dirname "${BASH_SOURCE[0]}" )/.."
FIX=""
COLOR="auto"
for v in "$@"; do
if [[ "$v" == "--no-ansi" ]] || [[ "$v" == "-n" ]]; then
COLOR="never"
fi
if [[ "$v" == "--fix" ]] || [[ "$v" == "-f" ]]; then
FIX="1"
fi
done
function clang_format_run() {
python ./scripts/run-clang-format.py -r \
--clang-format-executable="clang-format-8" \
--color="$COLOR" \
--exclude ./library/include/borealis/extern \
--exclude ./library/lib/extern \
./library ./demo
}
if [[ -z "$FIX" ]]; then
clang_format_run
else
clang_format_run | patch -p1 -N -r -
fi

View File

@@ -0,0 +1,247 @@
"""
Copyright 2020 natinusala
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Run with Python 3
import argparse
import json
import re
from jsonpointer import resolve_pointer, JsonPointerException
from pathlib import Path
# All locales supported by HOS
_SUPPORTED_LOCALES = [
"ja",
"en-US",
"en-GB",
"fr",
"fr-CA",
"de",
"it",
"es",
"zh-CN",
"zh-Hans",
"zh-Hant",
"zh-TW",
"ko",
"nl",
"pt",
"pt-BR",
"ru",
"es-419",
]
# The default locale used by brls
_DEFAULT_LOCALE = "en-US"
# All illegal chars in the string keys
# Mostly jsonpointer reserved chars
_ILLEGAL_KEYS_CHARS = [
"/",
"~",
" ",
"#",
"$",
]
# All locales and their strings, filled by _check_locales
# {locale -> {key -> string}}
_LOCALES_CACHE = {}
def _folder_exists(path: Path, errors: list, warnings: list) -> tuple:
"""Checks that the i18n folder exists and is a folder"""
if not path.exists():
errors.append((1, f"Cannot continue with the checks: folder \"{path}\" doesn't exist"))
elif not path.is_dir():
errors.append((2, f"Cannot continue with the checks: file \"{path}\" is not a folder"))
def _ensure_default_locale(path: Path, errors: list, warnings: list) -> tuple:
"""Ensures that the default locale exists"""
defaultlocalefile = path / f"{_DEFAULT_LOCALE}"
if not defaultlocalefile.exists() or not defaultlocalefile.is_dir():
warnings.append((6, f"Default locale {_DEFAULT_LOCALE} is missing from the i18n folder"))
def _check_locales(path: Path, errors: list, warnings: list) -> tuple:
"""
Checks that the i18n only contains known locales
and loads them for subsequent checks
"""
for f in path.iterdir():
# Load locale
if f.is_dir():
# Known locale
if f.name not in _SUPPORTED_LOCALES:
warnings.append((3, f"Unknown locale for folder \"{f.name}\""))
continue
#Load all JSON files inside
for ff in f.iterdir():
# Directory
if ff.is_dir():
warnings.append((1, f"{f.name} folder contains stray folder \"{ff.name}\""))
# Known format
elif not ff.name.endswith(".json"):
warnings.append((2, f"{f.name} folder contains stray file \"{ff.name}\""))
# Load it
else:
with open(ff, "r") as jsonf:
_LOCALES_CACHE.setdefault(f.name, {})
try:
_LOCALES_CACHE[f.name][ff.name[:-5]] = json.loads(jsonf.read())
except json.JSONDecodeError as e:
errors.append((5, f"Cannot parse JSON file \"{f.name}/{ff.name}\": {e}"))
return # don't bother continuing
# File
else:
warnings.append((2, f"i18n folder contains stray file \"{f.name}\""))
if _DEFAULT_LOCALE not in _LOCALES_CACHE:
_LOCALES_CACHE[_DEFAULT_LOCALE] = {}
def _check_types(path: Path, errors: list, warnings: list) -> tuple:
"""Checks that locales only contain valid data"""
def _check_node(breadcrumb: str, key: str, value: dict, locale: str):
# Illegal chars in key
for char in _ILLEGAL_KEYS_CHARS:
if char in key:
errors.append((6, f"String \"{breadcrumb}\" of {locale} locale contains illegal character \"{char}\" in its name"))
# Dict
if isinstance(value, dict):
for nested_key in value:
new_breadcrumb = f"{breadcrumb}/{nested_key}" if breadcrumb else nested_key
_check_node(new_breadcrumb, nested_key, value[nested_key], locale)
# Not strings
elif not isinstance(value, str):
errors.append((7, f"String \"{breadcrumb}\" of {locale} locale contains data \"{str(value)}\" of invalid type \"{type(value).__name__}\""))
for locale in _LOCALES_CACHE:
_check_node("", "", _LOCALES_CACHE[locale], locale)
def _check_untranslated_strings(path: Path, errors: list, warnings: list) -> tuple:
"""
Ensure there are no untranslated strings
"""
def _check_node(breadcrumb: str, value: dict):
for nested_key in value:
if breadcrumb:
base = f"{breadcrumb}/{nested_key}"
else:
base = nested_key
# Dict
if isinstance(value[nested_key], dict):
_check_node(base, value[nested_key])
# Str
else:
for locale in _LOCALES_CACHE:
if locale == _DEFAULT_LOCALE:
continue
pointer = f"/{base}"
try:
resolve_pointer(_LOCALES_CACHE[locale], pointer)
except JsonPointerException:
warnings.append((4, f"Locale {locale} is missing string \"{base}\" (untranslated from {_DEFAULT_LOCALE})"))
_check_node("", _LOCALES_CACHE[_DEFAULT_LOCALE])
def _check_unknown_translations(path: Path, errors: list, warnings: list) -> tuple:
"""Ensures all strings from all translations are in default locale"""
default_locale = _LOCALES_CACHE[_DEFAULT_LOCALE]
def _check_node(locale: str, breadcrumb: str, value: dict):
# Dict
if isinstance(value, dict):
for nested_key in value:
if breadcrumb:
base = f"{breadcrumb}/{nested_key}"
else:
base = nested_key
_check_node(locale, base, value[nested_key])
# Str
elif isinstance(value, str):
pointer = f"/{breadcrumb}"
try:
resolve_pointer(default_locale, pointer)
except JsonPointerException:
warnings.append((5, f"String \"{breadcrumb}\" is translated in locale {locale} but is missing from default locale {_DEFAULT_LOCALE} (translation of unknown string)"))
for locale in _LOCALES_CACHE:
if locale == _DEFAULT_LOCALE:
continue
_check_node(locale, "", _LOCALES_CACHE[locale])
if __name__ == "__main__":
# Arguments parsing
parser = argparse.ArgumentParser(description="Check integrity of i18n strings")
parser.add_argument(
dest="path",
action="store",
help="The path to the i18n folder to check",
)
args = parser.parse_args()
path = Path(args.path)
print(f"Checking i18n folder {path}...\n")
# Validation
checks = [
_folder_exists,
_ensure_default_locale,
_check_locales,
_check_types,
_check_untranslated_strings,
_check_unknown_translations,
]
errors = []
warnings = []
for check in checks:
check(path, errors, warnings)
if errors:
break # errors are fatal
if warnings:
print(f"{len(warnings)} warning(s):")
for code, warning in warnings:
print(f" - W{code:02}: {warning}")
print("\nWarnings are not fatal but should be fixed to avoid missing / broken translations in the app.")
if errors:
print(f"{len(errors)} error(s):")
for code, error in errors:
print(f" - E{code:02}: {error}")
print("\nPlease fix them and run the script again.")
if not errors and not warnings:
print("No errors or warnings detected, your i18n folder is good to go!")

View File

@@ -0,0 +1,357 @@
#!/usr/bin/env python
"""A wrapper script around clang-format, suitable for linting multiple files
and to use for continuous integration.
This is an alternative API for the clang-format command line.
It runs over multiple files and directories in parallel.
A diff output is produced and a sensible exit code is returned.
https://github.com/Sarcasm/run-clang-format/blob/master/run-clang-format.py
"""
from __future__ import print_function, unicode_literals
import argparse
import codecs
import difflib
import fnmatch
import io
import multiprocessing
import os
import signal
import subprocess
import sys
import traceback
from functools import partial
try:
from subprocess import DEVNULL # py3k
except ImportError:
DEVNULL = open(os.devnull, "wb")
DEFAULT_EXTENSIONS = 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx'
class ExitStatus:
SUCCESS = 0
DIFF = 1
TROUBLE = 2
def list_files(files, recursive=False, extensions=None, exclude=None):
if extensions is None:
extensions = []
if exclude is None:
exclude = []
out = []
for file in files:
if recursive and os.path.isdir(file):
for dirpath, dnames, fnames in os.walk(file):
fpaths = [os.path.join(dirpath, fname) for fname in fnames]
for pattern in exclude:
# os.walk() supports trimming down the dnames list
# by modifying it in-place,
# to avoid unnecessary directory listings.
dnames[:] = [
x for x in dnames
if
not fnmatch.fnmatch(os.path.join(dirpath, x), pattern)
]
fpaths = [
x for x in fpaths if not fnmatch.fnmatch(x, pattern)
]
for f in fpaths:
ext = os.path.splitext(f)[1][1:]
if ext in extensions:
out.append(f)
else:
out.append(file)
return out
def make_diff(file, original, reformatted):
return list(
difflib.unified_diff(
original,
reformatted,
fromfile='{}\t(original)'.format(file),
tofile='{}\t(reformatted)'.format(file),
n=3))
class DiffError(Exception):
def __init__(self, message, errs=None):
super(DiffError, self).__init__(message)
self.errs = errs or []
class UnexpectedError(Exception):
def __init__(self, message, exc=None):
super(UnexpectedError, self).__init__(message)
self.formatted_traceback = traceback.format_exc()
self.exc = exc
def run_clang_format_diff_wrapper(args, file):
try:
ret = run_clang_format_diff(args, file)
return ret
except DiffError:
raise
except Exception as e:
raise UnexpectedError('{}: {}: {}'.format(file, e.__class__.__name__,
e), e)
def run_clang_format_diff(args, file):
try:
with io.open(file, 'r', encoding='utf-8') as f:
original = f.readlines()
except IOError as exc:
raise DiffError(str(exc))
invocation = [args.clang_format_executable, file]
# Use of utf-8 to decode the process output.
#
# Hopefully, this is the correct thing to do.
#
# It's done due to the following assumptions (which may be incorrect):
# - clang-format will returns the bytes read from the files as-is,
# without conversion, and it is already assumed that the files use utf-8.
# - if the diagnostics were internationalized, they would use utf-8:
# > Adding Translations to Clang
# >
# > Not possible yet!
# > Diagnostic strings should be written in UTF-8,
# > the client can translate to the relevant code page if needed.
# > Each translation completely replaces the format string
# > for the diagnostic.
# > -- http://clang.llvm.org/docs/InternalsManual.html#internals-diag-translation
#
# It's not pretty, due to Python 2 & 3 compatibility.
encoding_py3 = {}
if sys.version_info[0] >= 3:
encoding_py3['encoding'] = 'utf-8'
try:
proc = subprocess.Popen(
invocation,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
**encoding_py3)
except OSError as exc:
raise DiffError(
"Command '{}' failed to start: {}".format(
subprocess.list2cmdline(invocation), exc
)
)
proc_stdout = proc.stdout
proc_stderr = proc.stderr
if sys.version_info[0] < 3:
# make the pipes compatible with Python 3,
# reading lines should output unicode
encoding = 'utf-8'
proc_stdout = codecs.getreader(encoding)(proc_stdout)
proc_stderr = codecs.getreader(encoding)(proc_stderr)
# hopefully the stderr pipe won't get full and block the process
outs = list(proc_stdout.readlines())
errs = list(proc_stderr.readlines())
proc.wait()
if proc.returncode:
raise DiffError(
"Command '{}' returned non-zero exit status {}".format(
subprocess.list2cmdline(invocation), proc.returncode
),
errs,
)
return make_diff(file, original, outs), errs
def bold_red(s):
return '\x1b[1m\x1b[31m' + s + '\x1b[0m'
def colorize(diff_lines):
def bold(s):
return '\x1b[1m' + s + '\x1b[0m'
def cyan(s):
return '\x1b[36m' + s + '\x1b[0m'
def green(s):
return '\x1b[32m' + s + '\x1b[0m'
def red(s):
return '\x1b[31m' + s + '\x1b[0m'
for line in diff_lines:
if line[:4] in ['--- ', '+++ ']:
yield bold(line)
elif line.startswith('@@ '):
yield cyan(line)
elif line.startswith('+'):
yield green(line)
elif line.startswith('-'):
yield red(line)
else:
yield line
def print_diff(diff_lines, use_color):
if use_color:
diff_lines = colorize(diff_lines)
if sys.version_info[0] < 3:
sys.stdout.writelines((l.encode('utf-8') for l in diff_lines))
else:
sys.stdout.writelines(diff_lines)
def print_trouble(prog, message, use_colors):
error_text = 'error:'
if use_colors:
error_text = bold_red(error_text)
print("{}: {} {}".format(prog, error_text, message), file=sys.stderr)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--clang-format-executable',
metavar='EXECUTABLE',
help='path to the clang-format executable',
default='clang-format')
parser.add_argument(
'--extensions',
help='comma separated list of file extensions (default: {})'.format(
DEFAULT_EXTENSIONS),
default=DEFAULT_EXTENSIONS)
parser.add_argument(
'-r',
'--recursive',
action='store_true',
help='run recursively over directories')
parser.add_argument('files', metavar='file', nargs='+')
parser.add_argument(
'-q',
'--quiet',
action='store_true')
parser.add_argument(
'-j',
metavar='N',
type=int,
default=0,
help='run N clang-format jobs in parallel'
' (default number of cpus + 1)')
parser.add_argument(
'--color',
default='auto',
choices=['auto', 'always', 'never'],
help='show colored diff (default: auto)')
parser.add_argument(
'-e',
'--exclude',
metavar='PATTERN',
action='append',
default=[],
help='exclude paths matching the given glob-like pattern(s)'
' from recursive search')
args = parser.parse_args()
# use default signal handling, like diff return SIGINT value on ^C
# https://bugs.python.org/issue14229#msg156446
signal.signal(signal.SIGINT, signal.SIG_DFL)
try:
signal.SIGPIPE
except AttributeError:
# compatibility, SIGPIPE does not exist on Windows
pass
else:
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
colored_stdout = False
colored_stderr = False
if args.color == 'always':
colored_stdout = True
colored_stderr = True
elif args.color == 'auto':
colored_stdout = sys.stdout.isatty()
colored_stderr = sys.stderr.isatty()
version_invocation = [args.clang_format_executable, str("--version")]
try:
subprocess.check_call(version_invocation, stdout=DEVNULL)
except subprocess.CalledProcessError as e:
print_trouble(parser.prog, str(e), use_colors=colored_stderr)
return ExitStatus.TROUBLE
except OSError as e:
print_trouble(
parser.prog,
"Command '{}' failed to start: {}".format(
subprocess.list2cmdline(version_invocation), e
),
use_colors=colored_stderr,
)
return ExitStatus.TROUBLE
retcode = ExitStatus.SUCCESS
files = list_files(
args.files,
recursive=args.recursive,
exclude=args.exclude,
extensions=args.extensions.split(','))
if not files:
return
njobs = args.j
if njobs == 0:
njobs = multiprocessing.cpu_count() + 1
njobs = min(len(files), njobs)
if njobs == 1:
# execute directly instead of in a pool,
# less overhead, simpler stacktraces
it = (run_clang_format_diff_wrapper(args, file) for file in files)
pool = None
else:
pool = multiprocessing.Pool(njobs)
it = pool.imap_unordered(
partial(run_clang_format_diff_wrapper, args), files)
while True:
try:
outs, errs = next(it)
except StopIteration:
break
except DiffError as e:
print_trouble(parser.prog, str(e), use_colors=colored_stderr)
retcode = ExitStatus.TROUBLE
sys.stderr.writelines(e.errs)
except UnexpectedError as e:
print_trouble(parser.prog, str(e), use_colors=colored_stderr)
sys.stderr.write(e.formatted_traceback)
retcode = ExitStatus.TROUBLE
# stop at the first unexpected error,
# something could be very wrong,
# don't process all files unnecessarily
if pool:
pool.terminate()
break
else:
sys.stderr.writelines(errs)
if outs == []:
continue
if not args.quiet:
print_diff(outs, use_color=colored_stdout)
if retcode == ExitStatus.SUCCESS:
retcode = ExitStatus.DIFF
return retcode
if __name__ == '__main__':
sys.exit(main())