Skip to content
Merged
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
58 changes: 49 additions & 9 deletions tools/Python/mctest/mctest.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import shutil
import platform
import subprocess
import io

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from mccodelib import utils, mccode_config
Expand Down Expand Up @@ -195,6 +196,34 @@ def extract_testvals(datafolder, monitorname):
return (I, I_err, N)
break

def parse_detector_I_value(resfile_path, detector_name):
"""
Return (value_float, success_bool, raw_value_str_or_None).
value_float is the parsed float (or -1.0 on failure).
success_bool is True when a value was parsed successfully.
raw_value_str_or_None is the string extracted (before conversion) or None.
"""
prefix = f"Detector: {detector_name}_I="
try:
with open(resfile_path, "r", encoding="utf-8", errors="replace") as f:
for line in f:
if line.startswith(prefix):
# extract after first '=' then take up to first whitespace
# matches the shell pipeline: cut -f2 -d= | cut -f1 -d' '
_, _, after_eq = line.partition("=")
raw = after_eq.split()[0] if after_eq else ""
if raw == "":
return -1.0, False, None
try:
return float(raw), True, raw
except ValueError:
return -1.0, False, raw
return -1.0, False, None
except FileNotFoundError:
return -1.0, False, None
except OSError:
return -1.0, False, None

def mccode_test(branchdir, testdir, limitinstrs=None, instrfilter=None, compfilter=None, version=None):
''' this main test function tests the given mccode branch/version '''
skipped=False
Expand Down Expand Up @@ -394,6 +423,7 @@ def mccode_test(branchdir, testdir, limitinstrs=None, instrfilter=None, compfilt
failed=True
continue

resbase="(No file)"
# test value extraction
if not didwrite_nexus:
extraction = extract_testvals(join(testdir, test.instrname, str(test.testnb)), test.detector)
Expand All @@ -402,26 +432,36 @@ def mccode_test(branchdir, testdir, limitinstrs=None, instrfilter=None, compfilt
else:
test.testval = -1
failed=True
resbase ="run_stdout_%d.txt" % (test.testnb)
resfile = join(testdir,test.instrname,resbase)
# Look for detector output in run_stdout
else:
metalog = LineLogger()
resfile = join(testdir,test.instrname,"run_stdout_%d.txt" % (test.testnb))
cmd = r"grep %s_I= %s | head -1 | cut -f2 -d= | cut -f1 -d' '" %(test.detector, resfile)
utils.run_subtool_to_completion(cmd, stdout_cb=metalog.logline)
try:
test.testval=float(metalog.lst[0])
except:
resbase ="run_stdout_%d.txt" % (test.testnb)
resfile = join(testdir,test.instrname,resbase)
val, ok, raw = parse_detector_I_value(resfile, test.detector)
if ok:
test.testval = val
else:
test.testval=-1
failed=True

if test.didrun and not failed:
percent=0
if test.didrun:
if failed:
suffix += " + !! RUNTIME FAILURE - see %s !! " % (resbase)
formatstr = "%-" + "%ds: " % (maxnamelen+1) + \
"{:3d}.".format(math.floor(test.runtime)) + str(test.runtime-int(test.runtime)).split('.')[1][:2]
if test.targetval!=0: # Normal situation, non-zero target value
logging.info(formatstr % test.get_display_name() + " [val: " + str(test.testval) + " / " + str(test.targetval) + " = " + str(round(100.0*test.testval/test.targetval)) + " %]" + suffix)
percent=round(100.0*test.testval/test.targetval)
if percent<80 or percent>120:
suffix += " <--- BIG DISCREPANCY??"
logging.info(formatstr % test.get_display_name() + " [val: " + str(test.testval) + " / " + str(test.targetval) + " = " + str(percent) + " %]" + suffix)
else: # Special case, expected test target value is 0
logging.info(formatstr % test.get_display_name() + " [val: " + str(test.testval) + " vs " + str(test.targetval) + " (absolute vs 0) ]" + suffix)

else:
logging.info((formatstr % test.get_display_name()) + (" !! [TEST INDICATES RUNTIME ERROR - see %s + suffix ] !!" % (resbase)))
suffix=""
# save test result to disk
test.testcomplete = True
if not skipped:
Expand Down
Loading