Python time.perf_counter_ns() Examples

The following are 10 code examples of time.perf_counter_ns(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module time , or try the search function .
Example #1
Source File: apollotracing.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def resolve(
        self, next_: Resolver, parent: Any, info: GraphQLResolveInfo, **kwargs
    ):  # pylint: disable=invalid-overridden-method
        if not should_trace(info):
            result = next_(parent, info, **kwargs)
            return result

        start_timestamp = perf_counter_ns()
        record = {
            "path": format_path(info.path),
            "parentType": str(info.parent_type),
            "fieldName": info.field_name,
            "returnType": str(info.return_type),
            "startOffset": start_timestamp - self.start_timestamp,
        }
        self.resolvers.append(record)
        try:
            result = next_(parent, info, **kwargs)
            return result
        finally:
            end_timestamp = perf_counter_ns()
            record["duration"] = end_timestamp - start_timestamp 
Example #2
Source File: test_time.py    From android_universal with MIT License 6 votes vote down vote up
def test_time_ns_type(self):
        def check_ns(sec, ns):
            self.assertIsInstance(ns, int)

            sec_ns = int(sec * 1e9)
            # tolerate a difference of 50 ms
            self.assertLess((sec_ns - ns), 50 ** 6, (sec, ns))

        check_ns(time.time(),
                 time.time_ns())
        check_ns(time.monotonic(),
                 time.monotonic_ns())
        check_ns(time.perf_counter(),
                 time.perf_counter_ns())
        check_ns(time.process_time(),
                 time.process_time_ns())

        if hasattr(time, 'thread_time'):
            check_ns(time.thread_time(),
                     time.thread_time_ns())

        if hasattr(time, 'clock_gettime'):
            check_ns(time.clock_gettime(time.CLOCK_REALTIME),
                     time.clock_gettime_ns(time.CLOCK_REALTIME)) 
Example #3
Source File: tracing.py    From sentry-python with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(
        self,
        trace_id=None,  # type: Optional[str]
        span_id=None,  # type: Optional[str]
        parent_span_id=None,  # type: Optional[str]
        same_process_as_parent=True,  # type: bool
        sampled=None,  # type: Optional[bool]
        op=None,  # type: Optional[str]
        description=None,  # type: Optional[str]
        hub=None,  # type: Optional[sentry_sdk.Hub]
        status=None,  # type: Optional[str]
        transaction=None,  # type: Optional[str] # deprecated
    ):
        # type: (...) -> None
        self.trace_id = trace_id or uuid.uuid4().hex
        self.span_id = span_id or uuid.uuid4().hex[16:]
        self.parent_span_id = parent_span_id
        self.same_process_as_parent = same_process_as_parent
        self.sampled = sampled
        self.op = op
        self.description = description
        self.status = status
        self.hub = hub
        self._tags = {}  # type: Dict[str, str]
        self._data = {}  # type: Dict[str, Any]
        self.start_timestamp = datetime.utcnow()
        try:
            # TODO: For Python 3.7+, we could use a clock with ns resolution:
            # self._start_timestamp_monotonic = time.perf_counter_ns()

            # Python 3.3+
            self._start_timestamp_monotonic = time.perf_counter()
        except AttributeError:
            pass

        #: End timestamp of span
        self.timestamp = None  # type: Optional[datetime]

        self._span_recorder = None  # type: Optional[_SpanRecorder] 
Example #4
Source File: apollotracing.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def perf_counter_ns() -> int:
        return int(perf_counter() * NS_IN_SECOND) 
Example #5
Source File: apollotracing.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def request_started(self, context: ContextValue):
        self.start_date = datetime.datetime.utcnow()
        self.start_timestamp = perf_counter_ns() 
Example #6
Source File: apollotracing.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def resolve(
        self, next_: Resolver, parent: Any, info: GraphQLResolveInfo, **kwargs
    ):
        if not should_trace(info):
            result = next_(parent, info, **kwargs)
            if isawaitable(result):
                result = await result
            return result

        start_timestamp = perf_counter_ns()
        record = {
            "path": format_path(info.path),
            "parentType": str(info.parent_type),
            "fieldName": info.field_name,
            "returnType": str(info.return_type),
            "startOffset": start_timestamp - self.start_timestamp,
        }
        self.resolvers.append(record)
        try:
            result = next_(parent, info, **kwargs)
            if isawaitable(result):
                result = await result
            return result
        finally:
            end_timestamp = perf_counter_ns()
            record["duration"] = end_timestamp - start_timestamp 
Example #7
Source File: apollotracing.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_totals(self):
        return {
            "start": self.start_date,
            "end": datetime.datetime.utcnow(),
            "duration": perf_counter_ns() - self.start_timestamp,
            "resolvers": self.resolvers,
        } 
Example #8
Source File: _compat.py    From napari with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def perf_counter_ns():
        """Compatibility version for pre Python 3.7."""
        return int(time.perf_counter() * 1e9) 
Example #9
Source File: _util.py    From txaio with MIT License 5 votes vote down vote up
def perf_counter_ns():
        """
        Shim for standard library time.perf_counter for Python < 3.7.
        """
        return int(time.perf_counter() * 1000000000.) 
Example #10
Source File: profiler.py    From panoramix with MIT License 5 votes vote down vote up
def time_ns():
        return perf_counter_ns()