timem_results.py
Go to the documentation of this file.
1 # Copyright 2023 Ekumen, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import argparse
16 import json
17 from pathlib import Path
18 
19 import matplotlib.pyplot as plt
20 import numpy as np
21 import pandas as pd
22 
23 from .exceptions import ScriptError
24 
25 DEFAULT_FILE_NAME = "timem-output.json"
26 
27 
28 def read_timem_output(dir_path: Path):
29  file = dir_path / DEFAULT_FILE_NAME
30  try:
31  with open(file, mode='r') as f:
32  output = json.load(f)
33  except (FileExistsError, FileNotFoundError) as ex:
34  raise ScriptError(f"Failed to open file '{file.absolute()}': {ex}")
35  except json.decoder.JSONDecodeError:
36  return None
37  return output['timemory']['timem'][0]
38 
39 
41  return f"{data['value']:.2f}{data['unit_repr']}"
42 
43 
44 def get_timem_metrics(output):
45  return output['peak_rss'], output['cpu_util']
46 
47 
49  return tuple(data['value'] for data in get_timem_metrics(output))
50 
51 
53  return tuple(get_value_with_unit(data) for data in get_timem_metrics(output))
54 
55 
56 def create_timeseries(output):
57  data = output['wall_clock']
58  elapsed_time = data['value']
59  time = np.arange(0.0, elapsed_time, 0.2)
60  wall_clock_to_time_offset = output['history'][0]['wall_clock']['value']
61  wall_clock_to_time_scale = elapsed_time / (
62  output['history'][-1]['wall_clock']['value'] - wall_clock_to_time_offset
63  )
64  time = []
65  rss = []
66  virtual_memory = []
67  for sample in output['history']:
68  time.append(
69  (sample['wall_clock']['value'] - wall_clock_to_time_offset)
70  * wall_clock_to_time_scale
71  )
72  rss.append(sample['page_rss']['value'])
73  virtual_memory.append(sample['virtual_memory']['value'])
74 
75  series = pd.DataFrame(
76  {'time': time, 'rss': rss, 'virtual_memory': virtual_memory}
77  ).set_index('time')
78  return series
79 
80 
81 def main():
82  arg_parser = argparse.ArgumentParser(
83  description='Script to postprocess timem results.'
84  )
85  arg_parser.add_argument(
86  'dir', help='Directory with timem-output.json file', type=Path
87  )
88  args = arg_parser.parse_args()
89  dir_path = Path(args.dir)
90  name = dir_path.name
91  output = read_timem_output(dir_path)
92  peak_rss_str, cpu_usage_str = get_timem_metrics_with_units(output)
93  series = create_timeseries(output)
94  elapsed_time_str = get_value_with_unit(output['wall_clock'])
95  print(f"timem metrics for run '{name}':")
96  print(f"\telapsed_time: {elapsed_time_str}")
97  print(f"\tcpu_usage: {cpu_usage_str}")
98  print(f"\tpeak_rss: {peak_rss_str}")
99  series.plot(subplots=True)
100  plt.show()
beluga_benchmark.timem_results.get_timem_metrics
def get_timem_metrics(output)
Definition: timem_results.py:44
beluga_benchmark.timem_results.get_timem_metrics_values
def get_timem_metrics_values(output)
Definition: timem_results.py:48
beluga_benchmark.timem_results.read_timem_output
def read_timem_output(Path dir_path)
Definition: timem_results.py:28
beluga_benchmark.timem_results.get_value_with_unit
def get_value_with_unit(data)
Definition: timem_results.py:40
beluga_benchmark.timem_results.create_timeseries
def create_timeseries(output)
Definition: timem_results.py:56
beluga_benchmark.timem_results.get_timem_metrics_with_units
def get_timem_metrics_with_units(output)
Definition: timem_results.py:52
beluga_benchmark.timem_results.main
def main()
Definition: timem_results.py:81
beluga_benchmark.exceptions.ScriptError
Definition: exceptions.py:16


beluga_benchmark
Author(s):
autogenerated on Tue Jul 16 2024 03:00:12