1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
import mmap
import os
import tempfile
from memray import AllocatorType
from memray import FileReader
try:
from memray._test import MemoryAllocator
except ImportError:
from memray import MemoryAllocator
from memray import Tracker
from .benchmarking.cases import async_tree_base
from .benchmarking.cases import fannkuch_base
from .benchmarking.cases import mdp_base
from .benchmarking.cases import pprint_format_base
from .benchmarking.cases import raytrace_base
LOOPS = 1000
class TracebackBenchmarks:
def setup(self):
self.tempfile = tempfile.NamedTemporaryFile()
allocator = MemoryAllocator()
def fac(n):
if n == 1:
allocator.valloc(1234)
allocator.free()
return 1
return n * fac(n - 1)
os.unlink(self.tempfile.name)
with Tracker(self.tempfile.name):
fac(300)
(self.record,) = [
record
for record in FileReader(self.tempfile.name).get_allocation_records()
if record.allocator == AllocatorType.VALLOC
]
def time_get_stack_trace(self):
self.record.stack_trace()
class AllocatorBenchmarks:
def setup(self):
self.tempfile = tempfile.NamedTemporaryFile()
self.allocator = MemoryAllocator()
def time_malloc(self):
os.unlink(self.tempfile.name)
with Tracker(self.tempfile.name):
for _ in range(LOOPS):
if self.allocator.malloc(1234):
self.allocator.free()
def time_posix_memalign(self):
os.unlink(self.tempfile.name)
with Tracker(self.tempfile.name):
for _ in range(LOOPS):
if self.allocator.posix_memalign(1234):
self.allocator.free()
def time_posix_realloc(self):
os.unlink(self.tempfile.name)
with Tracker(self.tempfile.name):
for _ in range(LOOPS):
if self.allocator.posix_memalign(1234):
self.allocator.free()
def time_calloc(self):
os.unlink(self.tempfile.name)
with Tracker(self.tempfile.name):
for _ in range(LOOPS):
if self.allocator.calloc(1234):
self.allocator.free()
def time_pvalloc(self):
os.unlink(self.tempfile.name)
with Tracker(self.tempfile.name):
for _ in range(LOOPS):
if self.allocator.pvalloc(1234):
self.allocator.free()
def time_valloc(self):
os.unlink(self.tempfile.name)
with Tracker(self.tempfile.name):
for _ in range(LOOPS):
if self.allocator.valloc(1234):
self.allocator.free()
def time_realloc(self):
os.unlink(self.tempfile.name)
with Tracker(self.tempfile.name):
for _ in range(LOOPS):
if self.allocator.realloc(1234):
self.allocator.free()
def time_mmap(self):
os.unlink(self.tempfile.name)
with Tracker(self.tempfile.name):
for _ in range(LOOPS):
with mmap.mmap(-1, length=2048, access=mmap.ACCESS_WRITE) as mmap_obj:
mmap_obj[0:100] = b"a" * 100
class ParserBenchmarks:
def setup(self):
self.tempfile = tempfile.NamedTemporaryFile()
self.allocator = MemoryAllocator()
os.unlink(self.tempfile.name)
self.tracker = Tracker(self.tempfile.name)
with self.tracker:
for _ in range(LOOPS):
self.allocator.valloc(1234)
self.allocator.free()
def time_end_to_end_parsing(self):
list(FileReader(self.tempfile.name).get_allocation_records())
def recursive(n, chunk_size):
"""Mimics generally-increasing but spiky usage"""
if not n:
return
allocator = MemoryAllocator()
allocator.valloc(n * chunk_size)
# Don't keep allocated memory when recursing, ~50% of the calls.
if n % 2:
allocator.free()
recursive(n - 1, chunk_size)
else:
recursive(n - 1, chunk_size)
allocator.free()
class HighWatermarkBenchmarks:
def setup(self):
self.tempfile = tempfile.NamedTemporaryFile()
os.unlink(self.tempfile.name)
self.tracker = Tracker(self.tempfile.name)
with self.tracker:
recursive(700, 99)
def time_high_watermark(self):
list(
FileReader(self.tempfile.name).get_high_watermark_allocation_records(
merge_threads=False
)
)
class MacroBenchmarksBase:
def __init_subclass__(cls) -> None:
for name in dir(cls):
if name.startswith("bench_"):
bench_name = name[len("bench_") :]
setattr(cls, f"time_{bench_name}", getattr(cls, name))
def bench_async_tree_cpu(self):
with self.tracker:
async_tree_base.run_benchmark("none")
def bench_async_tree_io(self):
with self.tracker:
async_tree_base.run_benchmark("io")
def bench_async_tree_memoization(self):
with self.tracker:
async_tree_base.run_benchmark("memoization")
def bench_async_tree_cpu_io_mixed(self):
with self.tracker:
async_tree_base.run_benchmark("cpu_io_mixed")
def bench_fannkuch(self):
with self.tracker:
fannkuch_base.run_benchmark()
def bench_mdp(self):
with self.tracker:
mdp_base.run_benchmark()
def bench_pprint_format(self):
with self.tracker:
pprint_format_base.run_benchmark()
def bench_raytrace(self):
with self.tracker:
raytrace_base.run_benchmark()
class MacroBenchmarksDefault(MacroBenchmarksBase):
def setup(self):
self.tracker_args = ("/dev/null",)
self.tracker_kwargs = {}
self.tracker = Tracker(*self.tracker_args, **self.tracker_kwargs)
class MacroBenchmarksPythonAllocators(MacroBenchmarksBase):
def setup(self):
self.tracker_args = ("/dev/null",)
self.tracker_kwargs = {"trace_python_allocators": True}
self.tracker = Tracker(*self.tracker_args, **self.tracker_kwargs)
class MacroBenchmarksPythonNative(MacroBenchmarksBase):
def setup(self):
self.tracker_args = ("/dev/null",)
self.tracker_kwargs = {"native_traces": True}
self.tracker = Tracker(*self.tracker_args, **self.tracker_kwargs)
class MacroBenchmarksPythonAll(MacroBenchmarksBase):
def setup(self):
self.tracker_args = ("/dev/null",)
self.tracker_kwargs = {"native_traces": True, "trace_python_allocators": True}
self.tracker = Tracker(*self.tracker_args, **self.tracker_kwargs)
class FileSizeBenchmarks:
def setup(self):
self.tempfile = tempfile.NamedTemporaryFile()
os.unlink(self.tempfile.name)
self.tracker = Tracker(self.tempfile.name)
def track_async_tree_cpu(self):
with self.tracker:
async_tree_base.run_benchmark("none")
return os.stat(self.tempfile.name).st_size
def track_async_tree_io(self):
with self.tracker:
async_tree_base.run_benchmark("io")
return os.stat(self.tempfile.name).st_size
def track_async_tree_memoization(self):
with self.tracker:
async_tree_base.run_benchmark("memoization")
return os.stat(self.tempfile.name).st_size
def track_async_tree_cpu_io_mixed(self):
with self.tracker:
async_tree_base.run_benchmark("cpu_io_mixed")
return os.stat(self.tempfile.name).st_size
def track_fannkuch(self):
with self.tracker:
fannkuch_base.run_benchmark()
return os.stat(self.tempfile.name).st_size
def track_mdp(self):
with self.tracker:
mdp_base.run_benchmark()
return os.stat(self.tempfile.name).st_size
def track_pprint_format(self):
with self.tracker:
pprint_format_base.run_benchmark()
return os.stat(self.tempfile.name).st_size
def track_raytrace(self):
with self.tracker:
raytrace_base.run_benchmark()
return os.stat(self.tempfile.name).st_size
|