Debian Codegraph @

asv_bench_memray/benchmarks/raymem.py

raw
1 # SPDX-License-Identifier: MIT
2 # Copyright (c) 2023--present Rohit Goswami <rgoswami[at]ieee[dot]org>
3 # For full license text, see LICENSE file in the root directory of this source tree
4 # or visit https://opensource.org/licenses/MIT
5 import re
6 import tempfile
7 import uuid
8 from pathlib import Path
9
10 from asv_runner.benchmarks._base import Benchmark
11 from asv_runner.benchmarks._exceptions import NotRequired
12
13 try:
14 import memray
15 from memray import FileReader
16 except ImportError:
17 raise NotRequired("RayMemBenchmark not requested or memray not found")
18
19
20 class RayMemBenchmark(Benchmark):
21 """
22 A single benchmark for tracking the memory of an object with memray
23 """
24
25 name_regex = re.compile("^(Ray[A-Z_].+)|(ray_.+)$")
26
27 def __init__(self, name, func, attr_sources):
28 Benchmark.__init__(self, name, func, attr_sources)
29 self.type = "memory"
30 self.unit = "bytes"
31 pass
32
33 def run(self, *param):
34 u_id = uuid.uuid4()
35 temp_dir = tempfile.gettempdir()
36 tfile_loc = Path(f"{temp_dir}/{u_id}.bin")
37 with memray.Tracker(
38 destination=memray.FileDestination(tfile_loc, overwrite=True)
39 ):
40 self.func(*param)
41 freader = FileReader(str(tfile_loc))
42 return freader.metadata.peak_memory
43
44
45 export_as_benchmark = [RayMemBenchmark]