Best Python code snippet using pytest-benchmark
systimes.py
Source:systimes.py
...35# values, ie. systimesres().36#37### Choose an implementation38SYSTIMES_IMPLEMENTATION = None39USE_CTYPES_GETPROCESSTIMES = 'ctypes GetProcessTimes() wrapper'40USE_WIN32PROCESS_GETPROCESSTIMES = 'win32process.GetProcessTimes()'41USE_RESOURCE_GETRUSAGE = 'resource.getrusage()'42USE_PROCESS_TIME_CLOCK = 'time.clock() (process time)'43USE_WALL_TIME_CLOCK = 'time.clock() (wall-clock)'44USE_WALL_TIME_TIME = 'time.time() (wall-clock)'45if sys.platform[:3] == 'win':46 # Windows platform47 try:48 import win32process49 except ImportError:50 try:51 import ctypes52 except ImportError:53 # Use the wall-clock implementation time.clock(), since this54 # is the highest resolution clock available on Windows55 SYSTIMES_IMPLEMENTATION = USE_WALL_TIME_CLOCK56 else:57 SYSTIMES_IMPLEMENTATION = USE_CTYPES_GETPROCESSTIMES58 else:59 SYSTIMES_IMPLEMENTATION = USE_WIN32PROCESS_GETPROCESSTIMES60else:61 # All other platforms62 try:63 import resource64 except ImportError:65 pass66 else:67 SYSTIMES_IMPLEMENTATION = USE_RESOURCE_GETRUSAGE68# Fall-back solution69if SYSTIMES_IMPLEMENTATION is None:70 # Check whether we can use time.clock() as approximation71 # for systimes()72 start = time.clock()73 time.sleep(0.1)74 stop = time.clock()75 if stop - start < 0.001:76 # Looks like time.clock() is usable (and measures process77 # time)78 SYSTIMES_IMPLEMENTATION = USE_PROCESS_TIME_CLOCK79 else:80 # Use wall-clock implementation time.time() since this provides81 # the highest resolution clock on most systems82 SYSTIMES_IMPLEMENTATION = USE_WALL_TIME_TIME83### Implementations84def getrusage_systimes():85 return resource.getrusage(resource.RUSAGE_SELF)[:2]86def process_time_clock_systimes():87 return (time.clock(), 0.0)88def wall_clock_clock_systimes():89 return (time.clock(), 0.0)90def wall_clock_time_systimes():91 return (time.time(), 0.0)92# Number of clock ticks per second for the values returned93# by GetProcessTimes() on Windows.94#95# Note: Ticks returned by GetProcessTimes() are 100ns intervals on96# Windows XP. However, the process times are only updated with every97# clock tick and the frequency of these is somewhat lower: depending98# on the OS version between 10ms and 15ms. Even worse, the process99# time seems to be allocated to process currently running when the100# clock interrupt arrives, ie. it is possible that the current time101# slice gets accounted to a different process.102WIN32_PROCESS_TIMES_TICKS_PER_SECOND = 1e7103def win32process_getprocesstimes_systimes():104 d = win32process.GetProcessTimes(win32process.GetCurrentProcess())105 return (d['UserTime'] / WIN32_PROCESS_TIMES_TICKS_PER_SECOND,106 d['KernelTime'] / WIN32_PROCESS_TIMES_TICKS_PER_SECOND)107def ctypes_getprocesstimes_systimes():108 creationtime = ctypes.c_ulonglong()109 exittime = ctypes.c_ulonglong()110 kerneltime = ctypes.c_ulonglong()111 usertime = ctypes.c_ulonglong()112 rc = ctypes.windll.kernel32.GetProcessTimes(113 ctypes.windll.kernel32.GetCurrentProcess(),114 ctypes.byref(creationtime),115 ctypes.byref(exittime),116 ctypes.byref(kerneltime),117 ctypes.byref(usertime))118 if not rc:119 raise TypeError('GetProcessTimes() returned an error')120 return (usertime.value / WIN32_PROCESS_TIMES_TICKS_PER_SECOND,121 kerneltime.value / WIN32_PROCESS_TIMES_TICKS_PER_SECOND)122# Select the default for the systimes() function123if SYSTIMES_IMPLEMENTATION is USE_RESOURCE_GETRUSAGE:124 systimes = getrusage_systimes125elif SYSTIMES_IMPLEMENTATION is USE_PROCESS_TIME_CLOCK:126 systimes = process_time_clock_systimes127elif SYSTIMES_IMPLEMENTATION is USE_WALL_TIME_CLOCK:128 systimes = wall_clock_clock_systimes129elif SYSTIMES_IMPLEMENTATION is USE_WALL_TIME_TIME:130 systimes = wall_clock_time_systimes131elif SYSTIMES_IMPLEMENTATION is USE_WIN32PROCESS_GETPROCESSTIMES:132 systimes = win32process_getprocesstimes_systimes133elif SYSTIMES_IMPLEMENTATION is USE_CTYPES_GETPROCESSTIMES:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!