funcprofiler 1.1.0__tar.gz → 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: funcprofiler
3
- Version: 1.1.0
3
+ Version: 1.2.0
4
4
  Summary: An open-source Python library for finding bottlenecks in code. Includes function profiling, data exports, logging, and even line-by-line profiling, for more control.
5
5
  Home-page: https://github.com/infinitode/funcprofiler
6
6
  Author: Infinitode Pty Ltd
@@ -38,6 +38,14 @@ Dynamic: summary
38
38
 
39
39
  An open-source Python library for identifying bottlenecks in code. It includes function profiling, data exports, logging, and line-by-line profiling for more granular control.
40
40
 
41
+ ## Changelog (v.1.2.0):
42
+ - Significant performance improvements for both function and line-by-line profiling.
43
+ - Cached function metadata at decoration time to reduce overhead during execution.
44
+ - Optimized line-by-line tracing using code object identity comparison.
45
+ - Implemented buffered logging for shared logs to minimize frequent file I/O.
46
+ - Improved trace accuracy by properly capturing the final line's execution time.
47
+ - Refactored export logic for more efficient report generation.
48
+
41
49
  ## Changelog (v.1.1.0):
42
50
  - Added support for 2 new export formats: `yaml` and `toml`.
43
51
  - Exports now include more information: peak memory usage, timestamp, arguments, return value, filepath, line number, and docstring.
@@ -7,6 +7,14 @@
7
7
 
8
8
  An open-source Python library for identifying bottlenecks in code. It includes function profiling, data exports, logging, and line-by-line profiling for more granular control.
9
9
 
10
+ ## Changelog (v.1.2.0):
11
+ - Significant performance improvements for both function and line-by-line profiling.
12
+ - Cached function metadata at decoration time to reduce overhead during execution.
13
+ - Optimized line-by-line tracing using code object identity comparison.
14
+ - Implemented buffered logging for shared logs to minimize frequent file I/O.
15
+ - Improved trace accuracy by properly capturing the final line's execution time.
16
+ - Refactored export logic for more efficient report generation.
17
+
10
18
  ## Changelog (v.1.1.0):
11
19
  - Added support for 2 new export formats: `yaml` and `toml`.
12
20
  - Exports now include more information: peak memory usage, timestamp, arguments, return value, filepath, line number, and docstring.
@@ -26,6 +26,18 @@ def function_profile(export_format: Optional[str] = None, filename: Optional[str
26
26
  log_filename = f"func_profiler_logs_{time.strftime('%Y%m%d')}_{time.strftime('%H%M%S')}.txt"
27
27
 
28
28
  def decorator(func: Callable) -> Callable:
29
+ # Pre-fetch metadata
30
+ func_name = func.__name__
31
+ try:
32
+ filepath = inspect.getfile(func)
33
+ except (TypeError, ValueError):
34
+ filepath = "unknown"
35
+ try:
36
+ line_number = inspect.getsourcelines(func)[1]
37
+ except (IOError, TypeError):
38
+ line_number = 0
39
+ docstring = func.__doc__
40
+
29
41
  @functools.wraps(func)
30
42
  def wrapper(*args, **kwargs):
31
43
  if not enabled:
@@ -35,14 +47,6 @@ def function_profile(export_format: Optional[str] = None, filename: Optional[str
35
47
  tracemalloc.start()
36
48
  start_time = time.time()
37
49
 
38
- # Prepare shared logging if enabled
39
- log_file = None
40
- if shared_log:
41
- log_file = open(log_filename, 'a')
42
- log_file.write(f"Profiling log for {func.__name__}\n")
43
- log_file.write(f"Date: {time.strftime('%Y-%m-%d')}\n")
44
- log_file.write(f"Time: {time.strftime('%H:%M:%S')}\n\n")
45
-
46
50
  # Execute the function
47
51
  result = func(*args, **kwargs)
48
52
 
@@ -52,42 +56,52 @@ def function_profile(export_format: Optional[str] = None, filename: Optional[str
52
56
  tracemalloc.stop()
53
57
 
54
58
  # Prepare data for exporting
55
- profiling_data = {
56
- "function_name": func.__name__,
57
- "execution_times": end_time - start_time,
58
- "memory_usage": current / 10**6, # Convert to MB
59
- "peak_memory_usage": peak / 10**6,
60
- "timestamp": time.strftime('%Y-%m-%d %H:%M:%S'),
61
- "arguments": str({'args': args, 'kwargs': kwargs}),
62
- "return_value": str(result),
63
- "filepath": inspect.getfile(func),
64
- "line_number": inspect.getsourcelines(func)[1],
65
- "docstring": func.__doc__
66
- }
67
-
68
- if export_format:
69
- file = filename or f"{func.__name__}_funcprofile_report"
70
- export_function_profile_data(profiling_data, func, export_format, file)
59
+ execution_time = end_time - start_time
60
+ current_mb = current / 10**6
61
+ peak_mb = peak / 10**6
62
+
63
+ if export_format or log_level == "debug" or shared_log:
64
+ now = time.localtime()
65
+ timestamp = time.strftime('%Y-%m-%d %H:%M:%S', now)
66
+
67
+ # Prepare shared logging if enabled
68
+ if shared_log:
69
+ with open(log_filename, 'a') as log_file:
70
+ date_str = timestamp[:10]
71
+ time_str = timestamp[11:]
72
+ log_file.write(f"Profiling log for {func_name}\n")
73
+ log_file.write(f"Date: {date_str}\n")
74
+ log_file.write(f"Time: {time_str}\n\n")
75
+ log_file.write(f"Function {func_name} called with args: {args}, kwargs: {kwargs}\n")
76
+ log_file.write(f"Execution Time: {execution_time:.12f}s, Memory usage: {current_mb:.6f}MB; Peak: {peak_mb:.6f}MB\n")
77
+ log_file.write("-" * 40 + "\n")
78
+
79
+ if export_format:
80
+ profiling_data = {
81
+ "function_name": func_name,
82
+ "execution_times": execution_time,
83
+ "memory_usage": current_mb,
84
+ "peak_memory_usage": peak_mb,
85
+ "timestamp": timestamp,
86
+ "arguments": str({'args': args, 'kwargs': kwargs}),
87
+ "return_value": str(result),
88
+ "filepath": filepath,
89
+ "line_number": line_number,
90
+ "docstring": docstring
91
+ }
92
+ file = filename or f"{func_name}_funcprofile_report"
93
+ export_function_profile_data(profiling_data, func, export_format, file)
71
94
 
72
95
  # Display the profiling results
73
96
  if log_level == "info":
74
- print(f"[FUNCPROFILER] Function '{func.__name__}' executed in {end_time - start_time:.12f}s")
75
- print(f"[FUNCPROFILER] Current memory usage: {current / 10**6:.12f}MB; Peak: {peak / 10**6:.12f}MB")
97
+ print(f"[FUNCPROFILER] Function '{func_name}' executed in {execution_time:.12f}s")
98
+ print(f"[FUNCPROFILER] Current memory usage: {current_mb:.12f}MB; Peak: {peak_mb:.12f}MB")
76
99
  elif log_level == "debug":
77
- print(f"[FUNCPROFILER-DEBUG] Function '{func.__name__}' called with args: {args}, kwargs: {kwargs}")
78
- print(f"[FUNCPROFILER-DEBUG] Execution Time: {end_time - start_time:.12f}s")
79
- print(f"[FUNCPROFILER-DEBUG] Memory Usage: {current / 10**6:.6f}MB; Peak: {peak / 10**6:.6f}MB")
100
+ print(f"[FUNCPROFILER-DEBUG] Function '{func_name}' called with args: {args}, kwargs: {kwargs}")
101
+ print(f"[FUNCPROFILER-DEBUG] Execution Time: {execution_time:.12f}s")
102
+ print(f"[FUNCPROFILER-DEBUG] Memory Usage: {current_mb:.6f}MB; Peak: {peak_mb:.6f}MB")
80
103
  print(f"[FUNCPROFILER-DEBUG] Return Value: {result}")
81
104
 
82
- # Log the profiling data
83
- if shared_log and log_file:
84
- log_file.write(f"Function {func.__name__} called with args: {args}, kwargs: {kwargs}\n")
85
- log_file.write(f"Execution Time: {end_time - start_time:.12f}s, Memory usage: {current / 10**6:.6f}MB; Peak: {peak / 10**6:.6f}MB\n")
86
- log_file.write("-" * 40 + "\n") # Separator between calls
87
-
88
- if shared_log and log_file:
89
- log_file.close() # Close the log file after writing if shared_log is True
90
-
91
105
  return result
92
106
 
93
107
  return wrapper
@@ -126,24 +140,24 @@ def export_function_profile_data(profiling_data: dict, func: Callable, export_fo
126
140
  writer.writerow(profiling_data.values())
127
141
 
128
142
  elif export_format == "html":
143
+ html_lines = [
144
+ "<html><head><title>Function Profiling Report</title>",
145
+ "<style>",
146
+ " body { font-family: Arial, sans-serif; }",
147
+ " table { border-collapse: collapse; width: 60%; margin: 20px 0; }",
148
+ " th, td { border: 1px solid #dddddd; text-align: left; padding: 8px; }",
149
+ " th { background-color: #f2f2f2; }",
150
+ "</style>",
151
+ "</head><body>",
152
+ f"<h1>Function Profiling Report: {profiling_data['function_name']}</h1>",
153
+ "<table>",
154
+ "<tr><th>Metric</th><th>Value</th></tr>"
155
+ ]
156
+ for key, value in profiling_data.items():
157
+ html_lines.append(f"<tr><td>{key.replace('_', ' ').title()}</td><td>{value}</td></tr>")
158
+ html_lines.append("</table></body></html>")
129
159
  with open(f"{filename}.html", 'w') as f:
130
- f.write("<html><head><title>Function Profiling Report</title>")
131
- f.write("""
132
- <style>
133
- body { font-family: Arial, sans-serif; }
134
- table { border-collapse: collapse; width: 60%; margin: 20px 0; }
135
- th, td { border: 1px solid #dddddd; text-align: left; padding: 8px; }
136
- th { background-color: #f2f2f2; }
137
- </style>
138
- """)
139
- f.write("</head><body>")
140
- f.write(f"<h1>Function Profiling Report: {profiling_data['function_name']}</h1>")
141
- f.write("<table>")
142
- f.write("<tr><th>Metric</th><th>Value</th></tr>")
143
- for key, value in profiling_data.items():
144
- f.write(f"<tr><td>{key.replace('_', ' ').title()}</td><td>{value}</td></tr>")
145
- f.write("</table>")
146
- f.write("</body></html>")
160
+ f.write("\n".join(html_lines))
147
161
 
148
162
  elif export_format == "xml":
149
163
  root = ET.Element("FunctionProfile")
@@ -212,6 +226,19 @@ def line_by_line_profile(
212
226
  log_filename = f"lbl_profiler_logs_{time.strftime('%Y%m%d')}_{time.strftime('%H%M%S')}.txt"
213
227
 
214
228
  def decorator(func: Callable) -> Callable:
229
+ # Pre-fetch metadata
230
+ func_name = func.__name__
231
+ target_code = func.__code__
232
+ try:
233
+ filepath = inspect.getfile(func)
234
+ except (TypeError, ValueError):
235
+ filepath = "unknown"
236
+ try:
237
+ source_lines, starting_line = inspect.getsourcelines(func)
238
+ except (IOError, TypeError):
239
+ source_lines, starting_line = [], 0
240
+ docstring = func.__doc__
241
+
215
242
  @functools.wraps(func)
216
243
  def wrapper(*args, **kwargs):
217
244
  if not enabled:
@@ -220,36 +247,46 @@ def line_by_line_profile(
220
247
  line_execution_times: Dict[int, float] = {}
221
248
  line_memory_usage: Dict[int, float] = {}
222
249
  current_line_start_time: Optional[float] = None
250
+ last_lineno: Optional[int] = None
223
251
  timer = time.perf_counter
224
252
  tracemalloc.start()
225
253
 
226
- # Prepare shared logging if enabled
227
- log_file = None
254
+ # Prepare shared logging buffer if enabled
255
+ log_buffer = []
228
256
  if shared_log:
229
- log_file = open(log_filename, 'a')
230
- log_file.write(f"Profiling log for {func.__name__}\n")
231
- log_file.write(f"Date: {time.strftime('%Y-%m-%d')}\n")
232
- log_file.write(f"Time: {time.strftime('%H:%M:%S')}\n\n")
257
+ now = time.localtime()
258
+ timestamp = time.strftime('%Y-%m-%d %H:%M:%S', now)
259
+ log_buffer.append(f"Profiling log for {func_name}\n")
260
+ log_buffer.append(f"Date: {timestamp[:10]}\n")
261
+ log_buffer.append(f"Time: {timestamp[11:]}\n\n")
233
262
 
234
263
  def trace_lines(frame, event, arg):
235
- nonlocal current_line_start_time
236
- if frame.f_code.co_name == func.__name__:
237
- lineno = frame.f_lineno
264
+ nonlocal current_line_start_time, last_lineno
265
+ if frame.f_code is target_code:
238
266
  if event == 'line':
239
- current_memory = tracemalloc.get_traced_memory()[1] / 10**6 # Convert to MB
240
- if current_line_start_time is not None:
267
+ now = timer()
268
+ lineno = frame.f_lineno
269
+ if current_line_start_time is not None and last_lineno is not None:
270
+ elapsed_time = now - current_line_start_time
271
+ line_execution_times[last_lineno] = line_execution_times.get(last_lineno, 0.0) + elapsed_time
272
+
273
+ current_memory = tracemalloc.get_traced_memory()[1] / 10**6 # Convert to MB
274
+ line_memory_usage[last_lineno] = current_memory
275
+
276
+ # Buffer the profiling data
277
+ if shared_log:
278
+ log_buffer.append(f"Line {last_lineno}: Execution Time: {elapsed_time:.12f}s, Memory Usage: {current_memory:.12f}MB\n")
279
+
280
+ current_line_start_time = now
281
+ last_lineno = lineno
282
+ elif event == 'return':
283
+ if current_line_start_time is not None and last_lineno is not None:
241
284
  elapsed_time = timer() - current_line_start_time
242
- if lineno in line_execution_times:
243
- line_execution_times[lineno] += elapsed_time
244
- else:
245
- line_execution_times[lineno] = elapsed_time
246
- line_memory_usage[lineno] = current_memory
247
-
248
- # Log the profiling data
249
- if shared_log and log_file:
250
- log_file.write(f"Line {lineno}: Execution Time: {elapsed_time:.12f}s, Memory Usage: {current_memory:.12f}MB\n")
251
-
252
- current_line_start_time = timer()
285
+ line_execution_times[last_lineno] = line_execution_times.get(last_lineno, 0.0) + elapsed_time
286
+ current_memory = tracemalloc.get_traced_memory()[1] / 10**6
287
+ line_memory_usage[last_lineno] = current_memory
288
+ if shared_log:
289
+ log_buffer.append(f"Line {last_lineno}: Execution Time: {elapsed_time:.12f}s, Memory Usage: {current_memory:.12f}MB\n")
253
290
  return trace_lines
254
291
 
255
292
  sys.settrace(trace_lines)
@@ -260,52 +297,39 @@ def line_by_line_profile(
260
297
  tracemalloc.stop()
261
298
 
262
299
  # Print profiling data
263
- if log_level == "info":
264
- print(f"\nLine-by-Line Profiling for '{func.__name__}':")
265
- source_lines, starting_line = inspect.getsourcelines(func)
300
+ if log_level in ("info", "debug"):
301
+ prefix = "[DEBUG] " if log_level == "debug" else ""
302
+ print(f"\n{prefix}Line-by-Line Profiling for '{func_name}':")
266
303
  for line_no in sorted(line_execution_times.keys()):
267
304
  actual_line = line_no - starting_line + 1
268
- source_line = source_lines[actual_line - 1].strip()
305
+ source_line = source_lines[actual_line - 1].strip() if 0 < actual_line <= len(source_lines) else "???"
269
306
  exec_time = line_execution_times[line_no]
270
307
  mem_usage = line_memory_usage.get(line_no, 0)
271
- print(f"Line {line_no} ({source_line}): "
272
- f"Execution Time: {exec_time:.12f}s, "
273
- f"Memory Usage: {mem_usage:.12f}MB")
274
- elif log_level == "debug":
275
- print(f"\n[DEBUG] Line-by-Line Profiling for '{func.__name__}':")
276
- source_lines, starting_line = inspect.getsourcelines(func)
277
- for line_no in sorted(line_execution_times.keys()):
278
- actual_line = line_no - starting_line + 1
279
- source_line = source_lines[actual_line - 1].strip()
280
- exec_time = line_execution_times[line_no]
281
- mem_usage = line_memory_usage.get(line_no, 0)
282
- print(f"[DEBUG] Line {line_no} ({source_line}): "
308
+ print(f"{prefix}Line {line_no} ({source_line}): "
283
309
  f"Execution Time: {exec_time:.12f}s, "
284
310
  f"Memory Usage: {mem_usage:.12f}MB")
285
311
 
286
- # Collect the profiling data for the report
287
- profiling_data = {
288
- "function_name": func.__name__,
289
- "timestamp": time.strftime('%Y-%m-%d %H:%M:%S'),
290
- "arguments": str({'args': args, 'kwargs': kwargs}),
291
- "return_value": str(result),
292
- "filepath": inspect.getfile(func),
293
- "docstring": func.__doc__,
294
- "line_execution_times": line_execution_times,
295
- "line_memory_usage": line_memory_usage
296
- }
312
+ # Handle shared logging
313
+ if shared_log:
314
+ log_buffer.append("\n" + "-" * 40 + "\n\n")
315
+ with open(log_filename, 'a') as log_file:
316
+ log_file.write("".join(log_buffer))
297
317
 
318
+ # Collect the profiling data for the report
298
319
  if export_format:
299
- file = filename or f"{func.__name__}_lblprofile_report"
320
+ profiling_data = {
321
+ "function_name": func_name,
322
+ "timestamp": time.strftime('%Y-%m-%d %H:%M:%S'),
323
+ "arguments": str({'args': args, 'kwargs': kwargs}),
324
+ "return_value": str(result),
325
+ "filepath": filepath,
326
+ "docstring": docstring,
327
+ "line_execution_times": line_execution_times,
328
+ "line_memory_usage": line_memory_usage
329
+ }
330
+ file = filename or f"{func_name}_lblprofile_report"
300
331
  export_profiling_data(profiling_data, func, export_format, file)
301
332
 
302
- # Close the log file if shared logging was enabled
303
- if shared_log and log_file:
304
- log_file.write("\n") # Add a new line for separation
305
- log_file.write("-" * 40 + "\n") # Separator between calls
306
- log_file.write("\n") # Add a new line for separation
307
- log_file.close()
308
-
309
333
  return result
310
334
 
311
335
  return wrapper
@@ -385,45 +409,48 @@ def export_profiling_data(
385
409
 
386
410
  elif export_format == 'html':
387
411
  output_path = f"{filename}.html"
388
- file_mode = 'a' if os.path.exists(output_path) else 'w'
389
- if file_mode == 'w':
390
- html_content = f"""
391
- <html>
392
- <head>
393
- <title>Line-by-Line Profiling Report</title>
394
- <style>
395
- body {{ font-family: Arial, sans-serif; }}
396
- table {{ border-collapse: collapse; width: 100%; }}
397
- th, td {{ border: 1px solid #dddddd; text-align: left; padding: 8px; }}
398
- th {{ background-color: #f2f2f2; }}
399
- </style>
400
- </head>
401
- <body>
402
- <h2>Line-by-Line Profiling Report for {func.__name__}</h2>
403
- <table>
404
- <tr>
405
- <th>Line Number</th>
406
- <th>Source Code</th>
407
- <th>Execution Time (s)</th>
408
- <th>Memory Usage (MB)</th>
409
- </tr>
410
- """
411
- else:
412
+ html_lines = []
413
+ if os.path.exists(output_path):
412
414
  with open(output_path, 'r') as f:
413
- html_content = f.read().replace("</table></body></html>", "")
415
+ html_content = f.read()
416
+ # If it's a valid HTML report, insert before </table>
417
+ if "</table></body></html>" in html_content:
418
+ html_parts = html_content.split("</table></body></html>")
419
+ html_lines.append(html_parts[0])
420
+ else:
421
+ html_lines.append(html_content)
422
+ else:
423
+ html_lines.append(f"""<html>
424
+ <head>
425
+ <title>Line-by-Line Profiling Report</title>
426
+ <style>
427
+ body {{ font-family: Arial, sans-serif; }}
428
+ table {{ border-collapse: collapse; width: 100%; }}
429
+ th, td {{ border: 1px solid #dddddd; text-align: left; padding: 8px; }}
430
+ th {{ background-color: #f2f2f2; }}
431
+ </style>
432
+ </head>
433
+ <body>
434
+ <h2>Line-by-Line Profiling Report for {func.__name__}</h2>
435
+ <table>
436
+ <tr>
437
+ <th>Line Number</th>
438
+ <th>Source Code</th>
439
+ <th>Execution Time (s)</th>
440
+ <th>Memory Usage (MB)</th>
441
+ </tr>""")
414
442
 
415
443
  for data in export_data:
416
- html_content += f"""
417
- <tr>
418
- <td>{data['Line Number']}</td>
419
- <td>{data['Source Code']}</td>
420
- <td>{data['Execution Time (s)']}</td>
421
- <td>{data['Memory Usage (MB)']}</td>
422
- </tr>
423
- """
424
- html_content += "</table></body></html>"
444
+ html_lines.append(f""" <tr>
445
+ <td>{data['Line Number']}</td>
446
+ <td>{data['Source Code']}</td>
447
+ <td>{data['Execution Time (s)']}</td>
448
+ <td>{data['Memory Usage (MB)']}</td>
449
+ </tr>""")
450
+
451
+ html_lines.append(" </table>\n</body>\n</html>")
425
452
  with open(output_path, 'w') as f:
426
- f.write(html_content)
453
+ f.write("\n".join(html_lines))
427
454
  print(f"[PROFILER] HTML report generated at: {output_path}")
428
455
 
429
456
  elif export_format == 'xml':
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: funcprofiler
3
- Version: 1.1.0
3
+ Version: 1.2.0
4
4
  Summary: An open-source Python library for finding bottlenecks in code. Includes function profiling, data exports, logging, and even line-by-line profiling, for more control.
5
5
  Home-page: https://github.com/infinitode/funcprofiler
6
6
  Author: Infinitode Pty Ltd
@@ -38,6 +38,14 @@ Dynamic: summary
38
38
 
39
39
  An open-source Python library for identifying bottlenecks in code. It includes function profiling, data exports, logging, and line-by-line profiling for more granular control.
40
40
 
41
+ ## Changelog (v.1.2.0):
42
+ - Significant performance improvements for both function and line-by-line profiling.
43
+ - Cached function metadata at decoration time to reduce overhead during execution.
44
+ - Optimized line-by-line tracing using code object identity comparison.
45
+ - Implemented buffered logging for shared logs to minimize frequent file I/O.
46
+ - Improved trace accuracy by properly capturing the final line's execution time.
47
+ - Refactored export logic for more efficient report generation.
48
+
41
49
  ## Changelog (v.1.1.0):
42
50
  - Added support for 2 new export formats: `yaml` and `toml`.
43
51
  - Exports now include more information: peak memory usage, timestamp, arguments, return value, filepath, line number, and docstring.
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name='funcprofiler',
8
- version='1.1.0',
8
+ version='v1.2.0',
9
9
  author='Infinitode Pty Ltd',
10
10
  author_email='infinitode.ltd@gmail.com',
11
11
  description='An open-source Python library for finding bottlenecks in code. Includes function profiling, data exports, logging, and even line-by-line profiling, for more control.',
File without changes
File without changes