Import_3ds: Improved distance cue chunk import
[blender-addons.git] / ant_landscape / stats.py
blobd2d0946ec1afe8b5f2b5479a52dafb58f7106ea0
1 # SPDX-FileCopyrightText: 2017-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 from time import time
7 try:
8 import psutil
9 # print('psutil available')
10 psutil_available = True
11 except ImportError:
12 psutil_available = False
15 class Stats:
16 def __init__(self):
17 self.memstats_available = False
18 if psutil_available:
19 self.process = psutil.Process()
20 self.memstats_available = True
21 self.reset()
23 def reset(self):
24 self.lasttime = self._gettime()
25 self.lastmem = self._getmem()
26 self.basemem = self.lastmem
27 self.maxmem = 0
28 self.elapsedtime = 0
30 def _gettime(self):
31 """return the time in seconds used by the current process."""
32 if psutil_available:
33 """ Handle psutil API change. """
34 if hasattr(self.process, "get_cpu_times"):
35 m = self.process.get_cpu_times()
36 else:
37 m = self.process.cpu_times()
38 return m.user + m.system
39 return time()
41 def _getmem(self):
42 """return the resident set size in bytes used by the current process."""
43 if psutil_available:
44 """ Handle psutil API change. """
45 if hasattr(self.process, "get_memory_info"):
46 m = self.process.get_memory_info()
47 else:
48 m = self.process.memory_info()
49 return m.rss
50 return 0
52 def time(self):
53 """return the time since the last call in seconds used by the current process."""
54 old = self.lasttime
55 self.lasttime = self._gettime()
56 self.elapsedtime = self.lasttime - old
57 return self.elapsedtime
59 def memory(self):
60 """return the maximum resident set size since the first call in bytes used by the current process."""
61 self.lastmem = self._getmem()
62 d = self.lastmem - self.basemem
63 if d > self.maxmem:
64 self.maxmem = d
65 return self.maxmem