How to use _libc_time method in pytest-benchmark

Best Python code snippet using pytest-benchmark

pep418.py

Source:pep418.py Github

copy

Full Screen

...167 if hasattr(libc, 'time'):168 _libc__time = libc.time169 _libc__time.argtypes = (time_t_p,)170 _libc__time.restype = time_t171 def _libc_time():172 return _libc__time(None)173 has_libc_time = True174 if sys.platform.startswith(("freebsd", "openbsd")):175 librt_name = libc_name176 else:177 librt_name = ctypes.util.find_library('rt')178 if librt_name:179 librt = ctypes.CDLL(librt_name, use_errno=True)180 if hasattr(librt, 'clock_gettime'):181 clockid_t = ctypes.c_int182 class timespec(ctypes.Structure):183 _fields_ = (184 ('tv_sec', time_t),185 ('tv_nsec', ctypes.c_long),186 )187 timespec_p = POINTER(timespec)188 _clock_gettime = librt.clock_gettime189 _clock_gettime.argtypes = (clockid_t, timespec_p)190 _clock_gettime.restype = ctypes.c_int191 def clock_gettime(clk_id):192 ts = timespec()193 err = _clock_gettime(clk_id, byref(ts))194 if err:195 raise ctypes_oserror()196 return ts.tv_sec + ts.tv_nsec * 1e-9197 has_clock_gettime = True198 _clock_settime = librt.clock_settime199 _clock_settime.argtypes = (clockid_t, timespec_p)200 _clock_settime.restype = ctypes.c_int201 def clock_settime(clk_id, value):202 ts = timespec()203 ts.tv_sec = int(value)204 ts.tv_nsec = int(float(abs(value)) % 1.0 * 1e9)205 err = _clock_settime(clk_id, byref(ts))206 if err:207 raise ctypes_oserror()208 return ts.tv_sec + ts.tv_nsec * 1e-9209 _clock_getres = librt.clock_getres210 _clock_getres.argtypes = (clockid_t, timespec_p)211 _clock_getres.restype = ctypes.c_int212 def clock_getres(clk_id):213 ts = timespec()214 err = _clock_getres(clk_id, byref(ts))215 if err:216 raise ctypes_oserror()217 return ts.tv_sec + ts.tv_nsec * 1e-9218 if sys.platform.startswith("linux"):219 CLOCK_REALTIME = 0220 CLOCK_MONOTONIC = 1221 CLOCK_PROCESS_CPUTIME_ID = 2222 elif sys.platform.startswith("freebsd"):223 CLOCK_REALTIME = 0224 CLOCK_PROF = 2225 CLOCK_MONOTONIC = 4226 elif sys.platform.startswith("openbsd"):227 CLOCK_REALTIME = 0228 CLOCK_MONOTONIC = 3229 elif sys.platform.startswith("sunos"):230 CLOCK_REALTIME = 3231 CLOCK_HIGHRES = 4232 # clock_gettime(CLOCK_PROCESS_CPUTIME_ID) fails with errno 22233 # on OpenSolaris234 # CLOCK_PROCESS_CPUTIME_ID = 5235def _clock_gettime_info(use_info, clk_id):236 value = clock_gettime(clk_id)237 if use_info:238 name = {239 CLOCK_MONOTONIC: 'CLOCK_MONOTONIC',240 CLOCK_PROF: 'CLOCK_PROF',241 CLOCK_HIGHRES: 'CLOCK_HIGHRES',242 CLOCK_PROCESS_CPUTIME_ID: 'CLOCK_PROCESS_CPUTIME_ID',243 CLOCK_REALTIME: 'CLOCK_REALTIME',244 }[clk_id]245 try:246 resolution = clock_getres(clk_id)247 except OSError:248 resolution = 1e-9249 info = {250 'implementation': 'clock_gettime(%s)' % name,251 'resolution': resolution,252 }253 if clk_id in (CLOCK_MONOTONIC, CLOCK_PROF, CLOCK_HIGHRES, CLOCK_PROCESS_CPUTIME_ID):254 info['monotonic'] = True255 info['adjustable'] = False256 elif clk_id in (CLOCK_REALTIME,):257 info['monotonic'] = False258 info['adjustable'] = True259 else:260 info = None261 return (value, info)262has_monotonic = False263if os.name == 'nt':264 # GetTickCount64() requires Windows Vista, Server 2008 or later265 if has_GetTickCount64:266 def _monotonic(use_info):267 value = GetTickCount64() * 1e-3268 if use_info:269 info = {270 'implementation': "GetTickCount64()",271 "monotonic": True,272 "resolution": 1e-3,273 "adjustable": False,274 }275 # FIXME: call GetSystemTimeAdjustment() to get the resolution276 else:277 info = None278 return (value, info)279 has_monotonic = True280 else:281 def _monotonic(use_info):282 ticks = GetTickCount()283 if ticks < _monotonic.last:284 # Integer overflow detected285 _monotonic.delta += 2**32286 _monotonic.last = ticks287 value = (ticks + _monotonic.delta) * 1e-3288 if use_info:289 info = {290 'implementation': "GetTickCount()",291 "monotonic": True,292 "resolution": 1e-3,293 "adjustable": False,294 }295 # FIXME: call GetSystemTimeAdjustment() to get the resolution296 else:297 info = None298 return (value, info)299 _monotonic.last = 0300 _monotonic.delta = 0301 has_monotonic = True302elif has_mach_absolute_time:303 def _monotonic(use_info):304 if _monotonic.factor is None:305 timebase = mach_timebase_info()306 _monotonic.factor = timebase[0] / timebase[1] * 1e-9307 value = mach_absolute_time() * _monotonic.factor308 if use_info:309 info = {310 'implementation': "mach_absolute_time()",311 'resolution': _monotonic.factor,312 'monotonic': True,313 'adjustable': False,314 }315 else:316 info = None317 return (value, info)318 _monotonic.factor = None319 has_monotonic = True320elif has_clock_gettime and CLOCK_HIGHRES is not None:321 def _monotonic(use_info):322 return _clock_gettime_info(use_info, CLOCK_HIGHRES)323 has_monotonic = True324elif has_clock_gettime and CLOCK_MONOTONIC is not None:325 def _monotonic(use_info):326 return _clock_gettime_info(use_info, CLOCK_MONOTONIC)327 has_monotonic = True328if has_monotonic:329 def monotonic():330 return _monotonic(False)[0]331def _perf_counter(use_info):332 info = None333 if _perf_counter.use_performance_counter:334 if _perf_counter.performance_frequency is None:335 value, info = _win_perf_counter(use_info)336 if value is not None:337 return (value, info)338 if _perf_counter.use_monotonic:339 # The monotonic clock is preferred over the system time340 try:341 return _monotonic(use_info)342 except (OSError, WindowsError):343 _perf_counter.use_monotonic = False344 return _time(use_info)345_perf_counter.use_performance_counter = (os.name == 'nt')346if _perf_counter.use_performance_counter:347 _perf_counter.performance_frequency = None348_perf_counter.use_monotonic = has_monotonic349def perf_counter():350 return _perf_counter(False)[0]351if os.name == 'nt':352 def _process_time(use_info):353 handle = GetCurrentProcess()354 process_times = GetProcessTimes(handle)355 value = (process_times[2] + process_times[3]) * 1e-7356 if use_info:357 info = {358 "implementation": "GetProcessTimes()",359 "resolution": 1e-7,360 "monotonic": True,361 "adjustable": False,362 # FIXME: call GetSystemTimeAdjustment() to get the resolution363 }364 else:365 info = None366 return (value, info)367else:368 import os369 try:370 import resource371 except ImportError:372 has_resource = False373 else:374 has_resource = True375 def _process_time(use_info):376 info = None377 if _process_time.clock_id is not None:378 try:379 return _clock_gettime_info(use_info, _process_time.clock_id)380 except OSError:381 _process_time.clock_id = None382 if _process_time.use_getrusage:383 try:384 usage = resource.getrusage(resource.RUSAGE_SELF)385 value = usage[0] + usage[1]386 except OSError:387 _process_time.use_getrusage = False388 else:389 if use_info:390 info = {391 "implementation": "getrusage(RUSAGE_SELF)",392 "resolution": 1e-6,393 "monotonic": True,394 "adjustable": False,395 }396 return (value, info)397 if _process_time.use_times:398 try:399 times = os.times()400 value = times[0] + times[1]401 except OSError:402 _process_time.use_getrusage = False403 else:404 if use_info:405 try:406 ticks_per_second = os.sysconf("SC_CLK_TCK")407 except ValueError:408 ticks_per_second = 60 # FIXME: get HZ constant409 info = {410 "implementation": "times()",411 "resolution": 1.0 / ticks_per_second,412 "monotonic": True,413 "adjustable": False,414 }415 return (value, info)416 return _libc_clock_info(use_info)417 if has_clock_gettime and CLOCK_PROCESS_CPUTIME_ID is not None:418 _process_time.clock_id = CLOCK_PROCESS_CPUTIME_ID419 elif has_clock_gettime and CLOCK_PROF is not None:420 _process_time.clock_id = CLOCK_PROF421 else:422 _process_time.clock_id = None423 _process_time.use_getrusage = has_resource424 # On OS/2, only the 5th field of os.times() is set, others are zeros425 _process_time.use_times = (hasattr(os, 'times') and os.name != 'os2')426def process_time():427 return _process_time(False)[0]428if os.name == "nt":429 def _time(use_info):430 value = GetSystemTimeAsFileTime() * 1e-7431 if use_info:432 info = {433 'implementation': 'GetSystemTimeAsFileTime',434 'resolution': 1e-7,435 'monotonic': False,436 # FIXME: call GetSystemTimeAdjustment() to get the resolution437 # and adjustable438 }439 else:440 info = None441 return (value, info)442else:443 def _time(use_info):444 info = None445 if has_clock_gettime and CLOCK_REALTIME is not None:446 try:447 return _clock_gettime_info(use_info, CLOCK_REALTIME)448 except OSError:449 # CLOCK_REALTIME is not supported (unlikely)450 pass451 if has_gettimeofday:452 try:453 tv = gettimeofday()454 except OSError:455 # gettimeofday() should not fail456 pass457 else:458 if use_info:459 info = {460 'monotonic': False,461 "implementation": "gettimeofday()",462 "resolution": 1e-6,463 'monotonic': False,464 'adjustable': True,465 }466 value = tv.tv_sec + tv.tv_usec * 1e-6467 return (value, info)468 # FIXME: implement ftime()469 if has_ftime:470 if use_info:471 info = {472 "implementation": "ftime()",473 "resolution": 1e-3,474 'monotonic': False,475 'adjustable': True,476 }477 value = ftime()478 elif has_libc_time:479 if use_info:480 info = {481 "implementation": "time()",482 "resolution": 1.0,483 'monotonic': False,484 'adjustable': True,485 }486 value = float(_libc_time())487 else:488 if use_info:489 info = {490 "implementation": "time.time()",491 'monotonic': False,492 'adjustable': True,493 }494 if os.name == "nt":495 # On Windows, time.time() uses ftime()496 info["resolution"] = 1e-3497 else:498 # guess that time.time() uses gettimeofday()499 info["resolution"] = 1e-6500 value = python_time.time()...

Full Screen

Full Screen

4a91107d34c429454e341349d8e18063_pep418.py

Source:4a91107d34c429454e341349d8e18063_pep418.py Github

copy

Full Screen

...161 if hasattr(libc, 'time'):162 _libc__time = libc.time163 _libc__time.argtypes = (time_t_p,)164 _libc__time.restype = time_t165 def _libc_time():166 return _libc__time(None)167 has_libc_time = True168 if sys.platform.startswith(("freebsd", "openbsd")):169 librt_name = libc_name170 else:171 librt_name = ctypes.util.find_library('rt')172 if librt_name:173 librt = ctypes.CDLL(librt_name, use_errno=True)174 if hasattr(librt, 'clock_gettime'):175 clockid_t = ctypes.c_int176 class timespec(ctypes.Structure):177 _fields_ = (178 ('tv_sec', time_t),179 ('tv_nsec', ctypes.c_long),180 )181 timespec_p = POINTER(timespec)182 _clock_gettime = librt.clock_gettime183 _clock_gettime.argtypes = (clockid_t, timespec_p)184 _clock_gettime.restype = ctypes.c_int185 def clock_gettime(clk_id):186 ts = timespec()187 err = _clock_gettime(clk_id, byref(ts))188 if err:189 raise ctypes_oserror()190 return ts.tv_sec + ts.tv_nsec * 1e-9191 has_clock_gettime = True192 _clock_settime = librt.clock_settime193 _clock_settime.argtypes = (clockid_t, timespec_p)194 _clock_settime.restype = ctypes.c_int195 def clock_settime(clk_id, value):196 ts = timespec()197 ts.tv_sec = int(value)198 ts.tv_nsec = int(float(abs(value)) % 1.0 * 1e9)199 err = _clock_settime(clk_id, byref(ts))200 if err:201 raise ctypes_oserror()202 return ts.tv_sec + ts.tv_nsec * 1e-9203 _clock_getres = librt.clock_getres204 _clock_getres.argtypes = (clockid_t, timespec_p)205 _clock_getres.restype = ctypes.c_int206 def clock_getres(clk_id):207 ts = timespec()208 err = _clock_getres(clk_id, byref(ts))209 if err:210 raise ctypes_oserror()211 return ts.tv_sec + ts.tv_nsec * 1e-9212 if sys.platform.startswith("linux"):213 CLOCK_REALTIME = 0214 CLOCK_MONOTONIC = 1215 CLOCK_PROCESS_CPUTIME_ID = 2216 elif sys.platform.startswith("freebsd"):217 CLOCK_REALTIME = 0218 CLOCK_PROF = 2219 CLOCK_MONOTONIC = 4220 elif sys.platform.startswith("openbsd"):221 CLOCK_REALTIME = 0222 CLOCK_MONOTONIC = 3223 elif sys.platform.startswith("sunos"):224 CLOCK_REALTIME = 3225 CLOCK_HIGHRES = 4226 # clock_gettime(CLOCK_PROCESS_CPUTIME_ID) fails with errno 22227 # on OpenSolaris228 # CLOCK_PROCESS_CPUTIME_ID = 5229def _clock_gettime_info(use_info, clk_id):230 value = clock_gettime(clk_id)231 if use_info:232 name = {233 CLOCK_MONOTONIC: 'CLOCK_MONOTONIC',234 CLOCK_PROF: 'CLOCK_PROF',235 CLOCK_HIGHRES: 'CLOCK_HIGHRES',236 CLOCK_PROCESS_CPUTIME_ID: 'CLOCK_PROCESS_CPUTIME_ID',237 CLOCK_REALTIME: 'CLOCK_REALTIME',238 }[clk_id]239 try:240 resolution = clock_getres(clk_id)241 except OSError:242 resolution = 1e-9243 info = {244 'implementation': 'clock_gettime(%s)' % name,245 'resolution': resolution,246 }247 if clk_id in (CLOCK_MONOTONIC, CLOCK_PROF, CLOCK_HIGHRES, CLOCK_PROCESS_CPUTIME_ID):248 info['monotonic'] = True249 info['adjustable'] = False250 elif clk_id in (CLOCK_REALTIME,):251 info['monotonic'] = False252 info['adjustable'] = True253 else:254 info = None255 return (value, info)256has_monotonic = False257if os.name == 'nt':258 # GetTickCount64() requires Windows Vista, Server 2008 or later259 if has_GetTickCount64:260 def _monotonic(use_info):261 value = GetTickCount64() * 1e-3262 if use_info:263 info = {264 'implementation': "GetTickCount64()",265 "monotonic": True,266 "resolution": 1e-3,267 "adjustable": False,268 }269 # FIXME: call GetSystemTimeAdjustment() to get the resolution270 else:271 info = None272 return (value, info)273 has_monotonic = True274 else:275 def _monotonic(use_info):276 ticks = GetTickCount()277 if ticks < _monotonic.last:278 # Integer overflow detected279 _monotonic.delta += 2**32280 _monotonic.last = ticks281 value = (ticks + _monotonic.delta) * 1e-3282 if use_info:283 info = {284 'implementation': "GetTickCount()",285 "monotonic": True,286 "resolution": 1e-3,287 "adjustable": False,288 }289 # FIXME: call GetSystemTimeAdjustment() to get the resolution290 else:291 info = None292 return (value, info)293 _monotonic.last = 0294 _monotonic.delta = 0295 has_monotonic = True296elif has_mach_absolute_time:297 def _monotonic(use_info):298 if _monotonic.factor is None:299 timebase = mach_timebase_info()300 _monotonic.factor = timebase[0] / timebase[1] * 1e-9301 value = mach_absolute_time() * _monotonic.factor302 if use_info:303 info = {304 'implementation': "mach_absolute_time()",305 'resolution': _monotonic.factor,306 'monotonic': True,307 'adjustable': False,308 }309 else:310 info = None311 return (value, info)312 _monotonic.factor = None313 has_monotonic = True314elif has_clock_gettime and CLOCK_HIGHRES is not None:315 def _monotonic(use_info):316 return _clock_gettime_info(use_info, CLOCK_HIGHRES)317 has_monotonic = True318elif has_clock_gettime and CLOCK_MONOTONIC is not None:319 def _monotonic(use_info):320 return _clock_gettime_info(use_info, CLOCK_MONOTONIC)321 has_monotonic = True322if has_monotonic:323 def monotonic():324 return _monotonic(False)[0]325def _perf_counter(use_info):326 info = None327 if _perf_counter.use_performance_counter:328 if _perf_counter.performance_frequency is None:329 value, info = _win_perf_counter(use_info)330 if value is not None:331 return (value, info)332 if _perf_counter.use_monotonic:333 # The monotonic clock is preferred over the system time334 try:335 return _monotonic(use_info)336 except (OSError, WindowsError):337 _perf_counter.use_monotonic = False338 return _time(use_info)339_perf_counter.use_performance_counter = (os.name == 'nt')340if _perf_counter.use_performance_counter:341 _perf_counter.performance_frequency = None342_perf_counter.use_monotonic = has_monotonic343def perf_counter():344 return _perf_counter(False)[0]345if os.name == 'nt':346 def _process_time(use_info):347 handle = GetCurrentProcess()348 process_times = GetProcessTimes(handle)349 value = (process_times[2] + process_times[3]) * 1e-7350 if use_info:351 info = {352 "implementation": "GetProcessTimes()",353 "resolution": 1e-7,354 "monotonic": True,355 "adjustable": False,356 # FIXME: call GetSystemTimeAdjustment() to get the resolution357 }358 else:359 info = None360 return (value, info)361else:362 import os363 try:364 import resource365 except ImportError:366 has_resource = False367 else:368 has_resource = True369 def _process_time(use_info):370 info = None371 if _process_time.clock_id is not None:372 try:373 return _clock_gettime_info(use_info, _process_time.clock_id)374 except OSError:375 _process_time.clock_id = None376 if _process_time.use_getrusage:377 try:378 usage = resource.getrusage(resource.RUSAGE_SELF)379 value = usage[0] + usage[1]380 except OSError:381 _process_time.use_getrusage = False382 else:383 if use_info:384 info = {385 "implementation": "getrusage(RUSAGE_SELF)",386 "resolution": 1e-6,387 "monotonic": True,388 "adjustable": False,389 }390 return (value, info)391 if _process_time.use_times:392 try:393 times = os.times()394 value = times[0] + times[1]395 except OSError:396 _process_time.use_getrusage = False397 else:398 if use_info:399 try:400 ticks_per_second = os.sysconf("SC_CLK_TCK")401 except ValueError:402 ticks_per_second = 60 # FIXME: get HZ constant403 info = {404 "implementation": "times()",405 "resolution": 1.0 / ticks_per_second,406 "monotonic": True,407 "adjustable": False,408 }409 return (value, info)410 return _libc_clock_info(use_info)411 if has_clock_gettime and CLOCK_PROCESS_CPUTIME_ID is not None:412 _process_time.clock_id = CLOCK_PROCESS_CPUTIME_ID413 elif has_clock_gettime and CLOCK_PROF is not None:414 _process_time.clock_id = CLOCK_PROF415 else:416 _process_time.clock_id = None417 _process_time.use_getrusage = has_resource418 # On OS/2, only the 5th field of os.times() is set, others are zeros419 _process_time.use_times = (hasattr(os, 'times') and os.name != 'os2')420def process_time():421 return _process_time(False)[0]422if os.name == "nt":423 def _time(use_info):424 value = GetSystemTimeAsFileTime() * 1e-7425 if use_info:426 info = {427 'implementation': 'GetSystemTimeAsFileTime',428 'resolution': 1e-7,429 'monotonic': False,430 # FIXME: call GetSystemTimeAdjustment() to get the resolution431 # and adjustable432 }433 else:434 info = None435 return (value, info)436else:437 def _time(use_info):438 info = None439 if has_clock_gettime and CLOCK_REALTIME is not None:440 try:441 return _clock_gettime_info(use_info, CLOCK_REALTIME)442 except OSError:443 # CLOCK_REALTIME is not supported (unlikely)444 pass445 if has_gettimeofday:446 try:447 tv = gettimeofday()448 except OSError:449 # gettimeofday() should not fail450 pass451 else:452 if use_info:453 info = {454 'monotonic': False,455 "implementation": "gettimeofday()",456 "resolution": 1e-6,457 'monotonic': False,458 'adjustable': True,459 }460 value = tv.tv_sec + tv.tv_usec * 1e-6461 return (value, info)462 # FIXME: implement ftime()463 if has_ftime:464 if use_info:465 info = {466 "implementation": "ftime()",467 "resolution": 1e-3,468 'monotonic': False,469 'adjustable': True,470 }471 value = ftime()472 elif has_libc_time:473 if use_info:474 info = {475 "implementation": "time()",476 "resolution": 1.0,477 'monotonic': False,478 'adjustable': True,479 }480 value = float(_libc_time())481 else:482 if use_info:483 info = {484 "implementation": "time.time()",485 'monotonic': False,486 'adjustable': True,487 }488 if os.name == "nt":489 # On Windows, time.time() uses ftime()490 info["resolution"] = 1e-3491 else:492 # guess that time.time() uses gettimeofday()493 info["resolution"] = 1e-6494 value = python_time.time()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pytest-benchmark automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful