Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
666d159
commit ed270d9
Showing
5 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import psutil | ||
import sys | ||
|
||
|
||
def run(func, n): | ||
# Run func once so any static variables etc. are allocated | ||
try: | ||
func() | ||
except Exception: | ||
pass | ||
|
||
# Take a measurement | ||
before = psutil.Process().memory_info().vms | ||
|
||
# Run n times | ||
for i in range(n): | ||
try: | ||
func() | ||
except Exception: | ||
pass | ||
|
||
# Measure again and return success (less than 1% increase) | ||
after = psutil.Process().memory_info().vms | ||
|
||
print(f"VMS before: {before}, after: {after}", file=sys.stderr) | ||
return (after - before) / before < 0.01 | ||
|
||
|
||
if __name__ == "__main__": | ||
# Usage: python3 memory.py CODE [N] | ||
# CODE must define a function 'func'; it will be exec()'d! | ||
# N, if present, overrides the number of runs (default: 1000) | ||
exec_globals = {} | ||
exec(sys.argv[1], exec_globals) | ||
n = int(sys.argv[2]) if sys.argv[2:] else 1000 | ||
if not run(exec_globals['func'], n): | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters