github.com/grafana/pyroscope@v1.18.0/examples/language-sdk-instrumentation/python/rideshare/fastapi/lib/utility/utility.py (about)

     1  import time
     2  import pyroscope
     3  import os
     4  from datetime import datetime
     5  
     6  # How much time mutex_lock() takes relative to search_radius()
     7  MUTEX_LOCK_MULTIPLIER = 2
     8  
     9  # How much time check_driver_availability() takes relative to search_radius()
    10  DRIVER_AVAILABILITY_MULTIPLIER = 0.5
    11  
    12  def mutex_lock(n):
    13      i = 0
    14      start_time = time.time()
    15      while time.time() - start_time < n * MUTEX_LOCK_MULTIPLIER:
    16          i += 1
    17  
    18  def check_driver_availability(n):
    19      i = 0
    20      start_time = time.time()
    21      while time.time() - start_time < n * DRIVER_AVAILABILITY_MULTIPLIER:
    22          i += 1
    23  
    24      # Every 4 minutes this will artificially create make requests in eu-north region slow
    25      # this is just for demonstration purposes to show how performance impacts show up in the
    26      # flamegraph
    27  
    28      force_mutex_lock = datetime.today().minute * 4 % 8 == 0
    29      if os.getenv("REGION") == "eu-north" and force_mutex_lock:
    30          mutex_lock(n)
    31  
    32  
    33  def find_nearest_vehicle(n, vehicle):
    34      with pyroscope.tag_wrapper({ "vehicle": vehicle}):
    35          i = 0
    36          start_time = time.time()
    37          while time.time() - start_time < n:
    38              i += 1
    39          if vehicle == "car":
    40              check_driver_availability(n)