Skip to content

Commit 1fc325a

Browse files
Make participant argument optional, analyze all if omitted (#24)
1 parent 36a96a2 commit 1fc325a

2 files changed

Lines changed: 63 additions & 19 deletions

File tree

preciceprofiling/analyze.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ def makeAnalyzeParser(add_help: bool = True):
1111
Parallel solvers show events of the primary rank next to the secondary ranks spending the least and most time in advance of preCICE.
1212
"""
1313
analyze = argparse.ArgumentParser(description=analyze_help, add_help=add_help)
14-
analyze.add_argument("participant", type=str, help="The participant to analyze")
14+
analyze.add_argument(
15+
"participant",
16+
type=str,
17+
nargs="?",
18+
default=None,
19+
help="The participant to analyze. If omitted, all participants are analyzed.",
20+
)
1521
addInputArgument(analyze)
1622
addUnitArgument(analyze)
1723
analyze.add_argument(
@@ -116,26 +122,10 @@ def runAnalyze(ns):
116122
)
117123

118124

119-
def analyzeCommand(profilingfile, participant, event, outfile=None, unit="us"):
120-
# Translate display name "total" back to internal name "_GLOBAL"
121-
if event == "total":
122-
event = "_GLOBAL"
123-
124-
run = Run(profilingfile)
125-
126-
participants = run.participants()
127-
assert (
128-
participant in participants
129-
), f"Given participant {participant} doesn't exist. Known: " + ", ".join(
130-
participants
131-
)
132-
125+
def computeAnalysis(run, participant, event, unit="us"):
126+
"""Compute the analysis DataFrame for a single participant."""
133127
df = run.toDataFrame(participant=participant)
134128

135-
print(f"Output timing are in {unit}.")
136-
137-
# Filter by participant
138-
# Convert duration to requested unit
139129
dur_factor = 1000 * ns_to_unit_factor(unit)
140130
df = (
141131
df.filter(pl.col("participant") == participant)
@@ -213,6 +203,39 @@ def analyzeCommand(profilingfile, participant, event, outfile=None, unit="us"):
213203
.collect()
214204
)
215205

206+
return joined
207+
208+
209+
def analyzeCommand(profilingfile, participant, event, outfile=None, unit="us"):
210+
# Translate display name "total" back to internal name "_GLOBAL"
211+
if event == "total":
212+
event = "_GLOBAL"
213+
214+
run = Run(profilingfile)
215+
all_participants = run.participants()
216+
217+
assert (
218+
participant is None or participant in all_participants
219+
), f"Given participant {participant} doesn't exist. Known: " + ", ".join(
220+
all_participants
221+
)
222+
223+
print(f"Output timings are in {unit}.")
224+
225+
if participant is None:
226+
if outfile is not None:
227+
print(
228+
"Error: --output requires a specific participant. "
229+
"Use `analyze <participant> --output <file>`.",
230+
file=sys.stderr,
231+
)
232+
return 1
233+
for p in all_participants:
234+
print(f"\n=== Participant: {p} ===")
235+
printWide(computeAnalysis(run, p, event, unit))
236+
return 0
237+
238+
joined = computeAnalysis(run, participant, event, unit)
216239
printWide(joined)
217240

218241
if outfile:

tests/test_examples.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,24 @@ def test_truncated_case(case: pathlib.Path, useDir: bool):
8686
cwd = pathlib.Path(tmp)
8787
truncate_case_files(case, cwd)
8888
run_case(cwd, cwd, useDir)
89+
90+
91+
def test_analyze_all_participants():
92+
"""analyze with no participant should analyze all participants"""
93+
case = pathlib.Path(__file__).parent / "cases" / "fiveparticipants-json"
94+
with tempfile.TemporaryDirectory() as tmp:
95+
cwd = pathlib.Path(tmp)
96+
profiling = cwd / "profiling.db"
97+
assert mergeCommand([case], profiling, True) == 0
98+
assert analyzeCommand(profiling, None, "advance", None, "us") == 0
99+
100+
101+
def test_analyze_no_participant_with_outfile_errors():
102+
"""analyze with no participant but --output should return error"""
103+
case = pathlib.Path(__file__).parent / "cases" / "fiveparticipants-json"
104+
with tempfile.TemporaryDirectory() as tmp:
105+
cwd = pathlib.Path(tmp)
106+
profiling = cwd / "profiling.db"
107+
assert mergeCommand([case], profiling, True) == 0
108+
result = analyzeCommand(profiling, None, "advance", cwd / "out.csv", "us")
109+
assert result == 1

0 commit comments

Comments
 (0)