From 5c4e5d37e3b1d46a157c1f431a48bacbb93a9377 Mon Sep 17 00:00:00 2001 From: hongtaozhang Date: Sat, 16 Nov 2024 21:07:00 -0800 Subject: [PATCH] Add unittest for platform.machine(). --- .../cpu_copy_performance/cpu_copy.cpp | 9 ++-- .../test_cpu_memory_bw_latency_performance.py | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/superbench/benchmarks/micro_benchmarks/cpu_copy_performance/cpu_copy.cpp b/superbench/benchmarks/micro_benchmarks/cpu_copy_performance/cpu_copy.cpp index 6831f4093..5f1193918 100644 --- a/superbench/benchmarks/micro_benchmarks/cpu_copy_performance/cpu_copy.cpp +++ b/superbench/benchmarks/micro_benchmarks/cpu_copy_performance/cpu_copy.cpp @@ -243,19 +243,18 @@ double BenchmarkNUMACopy(int src_node, int dst_node, Opts &opts) { * @param opts A reference to an Opts object containing various options and configurations for the benchmark. */ double RunCPUCopyBenchmark(int src_node, int dst_node, Opts &opts) { - double max_time_ns = 0; - // Run warm up rounds for (int i = 0; i < opts.num_warm_up; i++) { BenchmarkNUMACopy(src_node, dst_node, opts); } + double time_used_ns = 0; + for (int i = 0; i < opts.num_loops; i++) { - double time_used_ns = BenchmarkNUMACopy(src_node, dst_node, opts); - max_time_ns = std::max(max_time_ns, time_used_ns); + time_used_ns += BenchmarkNUMACopy(src_node, dst_node, opts); } - return max_time_ns; + return time_used_ns / opts.num_loops; } int main(int argc, char **argv) { diff --git a/tests/benchmarks/micro_benchmarks/test_cpu_memory_bw_latency_performance.py b/tests/benchmarks/micro_benchmarks/test_cpu_memory_bw_latency_performance.py index 0a2d2a21f..df3ac6347 100644 --- a/tests/benchmarks/micro_benchmarks/test_cpu_memory_bw_latency_performance.py +++ b/tests/benchmarks/micro_benchmarks/test_cpu_memory_bw_latency_performance.py @@ -3,6 +3,8 @@ """Tests for cpu-memory-bw-latency benchmark.""" +import os +import platform import unittest from tests.helper.testcase import BenchmarkTestCase @@ -148,3 +150,43 @@ def test_cpu_mem_bw_latency_benchmark_result_parsing(self): # Negative case - invalid raw output. assert (benchmark._process_raw_result(0, 'Invalid raw output') is False) assert (benchmark.return_code == ReturnCode.MICROBENCHMARK_RESULT_PARSING_FAILURE) + + def test_preprocess_mlc(self): + """Test _preprocess method for Intel MLC tool.""" + benchmark_name = 'cpu-memory-bw-latency' + (benchmark_class, predefine_params) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CPU) + assert (benchmark_class) + + benchmark = benchmark_class(benchmark_name, parameters='--tests bandwidth_matrix') + benchmark._args = benchmark._parser.parse_args(['--bin_dir', '/mock/bin/dir']) + benchmark._bin_name = 'mlc' + benchmark._commands = [] + + # Mock os.access to return True + os.access = lambda path, mode: True + + ret = benchmark._preprocess() + assert (ret is True) + assert (benchmark.return_code == ReturnCode.SUCCESS) + assert (len(benchmark._commands) == 1) + assert ('mlc --bandwidth_matrix;' in benchmark._commands[0]) + + def test_preprocess_general(self): + """Test _preprocess method for general CPU copy benchmark.""" + benchmark_name = 'cpu-memory-bw-latency' + (benchmark_class, predefine_params) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CPU) + assert (benchmark_class) + + benchmark = benchmark_class(benchmark_name, parameters='--size 1024 --num_warm_up 10 --num_loops 50 --check_data') + benchmark._args = benchmark._parser.parse_args(['--bin_dir', '/mock/bin/dir']) + benchmark._bin_name = 'cpu_copy' + benchmark._commands = [] + + # Mock platform.machine to return non-x86_64 + platform.machine = lambda: 'arm64' + + ret = benchmark._preprocess() + assert (ret is True) + assert (benchmark.return_code == ReturnCode.SUCCESS) + assert (len(benchmark._commands) == 1) + assert ('cpu_copy --size 1024 --num_warm_up 10 --num_loops 50 --check_data' in benchmark._commands[0])