Daniel Nichols commited on
Commit
f20b453
·
1 Parent(s): dc10826

better error handling

Browse files
Files changed (1) hide show
  1. src/rag.py +40 -36
src/rag.py CHANGED
@@ -85,22 +85,24 @@ class SlowestFunctionPromptFormatter(PerfGuruPromptFormatter):
85
  if error_fn:
86
  error_fn("Profile type must be provided if a profile file is provided.")
87
  return None
88
- profile = self._read_profile(profile_path, profile_type)
89
- slowest = profile.gf.dataframe.nlargest(self.k, 'time')
90
- function_names = [slowest['name'].values[i] for i in range(self.k) if i < len(slowest['name'].values)]
91
- execution_times = [slowest['time'].values[i] for i in range(self.k) if i < len(slowest['name'].values)]
92
- # print(profile_content)
93
- hot_path = profile.gf.hot_path()
94
- hot_path_functions = []
95
-
96
- for node in hot_path:
97
- if "name" in node.frame.attrs:
98
- hot_path_functions.append(node.frame["name"])
99
- hot_path_functions = hot_path_functions[:self.k]
100
-
101
- profile_content = (f"The slowest functions are {function_names} and they took {execution_times} seconds, respectively." +
102
- f" Also, these functions were in the hot path: {hot_path_functions}.")
103
- print(profile_content)
 
 
104
 
105
  else:
106
  profile_content = ""
@@ -127,28 +129,30 @@ class SlowestFunctionParsedPromptFormatter(PerfGuruPromptFormatter):
127
  error_fn("Profile type must be provided if a profile file is provided.")
128
  return None
129
 
130
- k = 1
131
- profile = self._read_profile(profile_path, profile_type)
132
- slowest = profile.gf.dataframe.nlargest(k, 'time')
133
- function_name = slowest['name'].values[0] if len(slowest['name'].values) > 0 else None
134
- line_number = slowest['line'].values[0] if len(slowest['line'].values) > 0 else None
135
- code = None
136
-
137
- if line_number:
138
- filename = ""
139
- code_file_contents = self._read_code_files(code_paths)
140
- for code_path, content in code_file_contents.items():
141
- filename = basename(code_path)
142
- code, _ = get_function_at_line(filename, str(line_number))
143
- if code:
144
- break
145
 
146
- if code:
147
- concatenated_code = f"{fname}:\n{code}\n\n"
148
- print("Only function code:", concatenated_code)
 
 
 
 
 
 
 
 
 
149
 
150
- profile_content = (f"The slowest function is {function_name}.")
151
- print(profile_content)
 
152
 
153
  if concatenated_code == "":
154
  code_file_contents = self._read_code_files(code_paths)
 
85
  if error_fn:
86
  error_fn("Profile type must be provided if a profile file is provided.")
87
  return None
88
+
89
+ try:
90
+ profile = self._read_profile(profile_path, profile_type)
91
+ slowest = profile.gf.dataframe.nlargest(self.k, 'time')
92
+ function_names = [slowest['name'].values[i] for i in range(self.k) if i < len(slowest['name'].values)]
93
+ execution_times = [slowest['time'].values[i] for i in range(self.k) if i < len(slowest['name'].values)]
94
+ hot_path = profile.gf.hot_path()
95
+ hot_path_functions = []
96
+
97
+ for node in hot_path:
98
+ if "name" in node.frame.attrs:
99
+ hot_path_functions.append(node.frame["name"])
100
+ hot_path_functions = hot_path_functions[:self.k]
101
+
102
+ profile_content = (f"The slowest functions are {function_names} and they took {execution_times} seconds, respectively." +
103
+ f" Also, these functions were in the hot path: {hot_path_functions}.")
104
+ except:
105
+ profile_content = ""
106
 
107
  else:
108
  profile_content = ""
 
129
  error_fn("Profile type must be provided if a profile file is provided.")
130
  return None
131
 
132
+ try:
133
+ k = 1
134
+ profile = self._read_profile(profile_path, profile_type)
135
+ slowest = profile.gf.dataframe.nlargest(k, 'time')
136
+ function_name = slowest['name'].values[0] if len(slowest['name'].values) > 0 else None
137
+ line_number = slowest['line'].values[0] if len(slowest['line'].values) > 0 else None
138
+ code = None
 
 
 
 
 
 
 
 
139
 
140
+ if line_number:
141
+ filename = ""
142
+ code_file_contents = self._read_code_files(code_paths)
143
+ for code_path, content in code_file_contents.items():
144
+ filename = basename(code_path)
145
+ code, _ = get_function_at_line(filename, str(line_number))
146
+ if code:
147
+ break
148
+
149
+ if code:
150
+ concatenated_code = f"{fname}:\n{code}\n\n"
151
+ print("Only function code:", concatenated_code)
152
 
153
+ profile_content = (f"The slowest function is {function_name}.")
154
+ except:
155
+ profile_content = ""
156
 
157
  if concatenated_code == "":
158
  code_file_contents = self._read_code_files(code_paths)