1+ import os
2+ import shutil
3+ import unittest
4+ import logging
5+
6+ import subprocess
7+ import json
8+ from collections import namedtuple
9+
10+ # class TestBasic(unittest.TestCase):
11+ # def setUp(self):
12+ # # Load test data
13+ # self.app = App(database="fixtures/test_basic.json")
14+ #
15+ # def test_customer_count(self):
16+ # self.assertEqual(len(self.app.customers), 100)
17+ #
18+ # def test_existence_of_customer(self):
19+ # customer = self.app.get_customer(id=10)
20+ # self.assertEqual(customer.name, "Org XYZ")
21+ # self.assertEqual(customer.address, "10 Red Road, Reading")
22+
23+
24+ # https://realpython.com/python-testing/
25+
26+ DataJson = namedtuple ("DataJson" , ["runs" , "metadata" , "tags" , "names" ])
27+ DataJsonRun = namedtuple ("DataJsonRun" , ["name" , "results" ])
28+ DataJsonResult = namedtuple ("DataJsonResult" , ["name" , "label" , "suite" , "value" , "unit" ])
29+
30+ class App :
31+ def __init__ (self ):
32+ self .TMP_DIR = os .path .dirname (__file__ )
33+ self .OUTPUT_DIR = os .path .join (self .TMP_DIR , "tmp-output" )
34+ self .RESULTS_DIR = os .path .join (self .TMP_DIR , "tmp-results" )
35+ self .WORKDIR_DIR = os .path .join (self .TMP_DIR , "tmp-workdir" )
36+
37+
38+
39+ def prepare_dirs (self ):
40+ for d in [self .RESULTS_DIR , self .OUTPUT_DIR , self .WORKDIR_DIR ]:
41+ os .makedirs (d )
42+
43+ # when UT does not want to build compute-benchmarks from scratch, it can provide prebuilt path
44+ cb_targetpath = os .environ .get ("COMPUTE_BENCHMARKS_BUILD_PATH" )
45+ if cb_targetpath and os .path .isdir (cb_targetpath ):
46+ cb_build_dir = os .path .join (self .WORKDIR_DIR , "compute-benchmarks-build" )
47+ os .symlink (cb_targetpath , cb_build_dir )
48+ with open (os .path .join (self .WORKDIR_DIR , "BENCH_WORKDIR_VERSION" ), "w" ) as f :
49+ f .write ("2.0" ) # TODO: take from main.INTERNAL_WORKDIR_VERSION
50+
51+
52+ def remove_dirs (self ):
53+ for d in [self .RESULTS_DIR , self .OUTPUT_DIR , self .WORKDIR_DIR ]:
54+ if os .path .exists (d ):
55+ shutil .rmtree (d )
56+ def run_main (self , * args ):
57+
58+ # TODO: not yet tested: "--detect-version", "sycl,compute_runtime"
59+
60+ return subprocess .run (["./devops/scripts/benchmarks/main.py" , self .WORKDIR_DIR ,
61+ "--sycl" , os .environ .get ("ONEAPI_ROOT" ),
62+ "--ur" , os .environ .get ("CMPLR_ROOT" ),
63+ "--adapter" , "opencl" ,
64+ "--save" , "testplik" ,
65+ "--output-html" , "remote" ,
66+ "--results-dir" , self .RESULTS_DIR ,
67+ "--output-dir" , self .OUTPUT_DIR ,
68+ "--preset" , "Minimal" ,
69+ "--timestamp-override" , "20240102_030405" ,
70+ "--exit-on-failure" ,
71+ * args ])
72+
73+ def get_output (self ):
74+ output_file = os .path .join (self .OUTPUT_DIR , "data.json" )
75+ with open (output_file ) as f :
76+ out = json .load (f )
77+ return DataJson (
78+ runs = [DataJsonRun (name = run ["name" ], results = [DataJsonResult (name = r ["name" ], label = r ["label" ], suite = r ["suite" ], value = r ["value" ], unit = r ["unit" ]) for r in run ["results" ]]) for run in out ["benchmarkRuns" ]],
79+ metadata = out ["benchmarkMetadata" ],
80+ tags = out ["benchmarkTags" ],
81+ names = out ["defaultCompareNames" ],
82+ )
83+
84+ # add "--verbose" for debug logs
85+
86+ class TestE2E (unittest .TestCase ):
87+ def setUp (self ):
88+ # Load test data
89+ self .app = App ()
90+ self .app .remove_dirs ()
91+ self .app .prepare_dirs ()
92+
93+ run_result = self .app .run_main ("--filter" , "RecordGraph" )
94+ self .assertEqual (run_result .returncode , 0 , "Subprocess did not exit cleanly" )
95+ # clean directory with input, output
96+ out = self .app .get_output ()
97+ self .assertIn ("record_and_replay_benchmark_l0 RecordGraph AppendCopy 1, AppendKern 10, CmdSetsInLvl 10, ForksInLvl 2, Instantiations 10, Lvls 4, Rec" , [r .name for r in out .runs [0 ].results ])
98+
99+
100+ def tearDown (self ):
101+ pass
102+ # self.app.remove_dirs()
103+
104+ def test_record_and_reply (self ):
105+ pass
106+
107+
108+ if __name__ == "__main__" :
109+ unittest .main ()
110+
0 commit comments