github.com/grafana/pyroscope@v1.18.0/examples/language-sdk-instrumentation/rust/rideshare/load-generator.py (about)

     1  import random
     2  import requests
     3  import time
     4  import threading
     5  
     6  HOSTS = [
     7      'us-east',
     8      'eu-north',
     9      'ap-south',
    10  ]
    11  
    12  VEHICLES = [
    13      'bike',
    14      'scooter',
    15      'car',
    16  ]
    17  
    18  def generate_load(host, vehicle):
    19      while True:
    20          start_time = time.time()
    21          print(f"requesting {vehicle} from {host}")
    22          
    23          try:
    24              resp = requests.get(f'http://{host}:5000/{vehicle}')
    25              resp.raise_for_status()
    26              duration = time.time() - start_time
    27              print(f"received {resp} in {duration:.2f}s from {host}/{vehicle}")
    28              
    29              # Sleep to complete the 4-second cycle
    30              sleep_time = max(4 - duration, 0)
    31              if sleep_time > 0:
    32                  time.sleep(sleep_time)
    33                  
    34          except BaseException as e:
    35              print(f"http error for {host}/{vehicle}: {e}")
    36              # On error, still maintain the 10-second cycle
    37              time.sleep(4)
    38  
    39  if __name__ == "__main__":
    40      print(f"starting load generator with thread per region-vehicle combination")
    41      time.sleep(3)
    42      
    43      threads = []
    44      # Create one thread per host-vehicle combination
    45      for host in HOSTS:
    46          for vehicle in VEHICLES:
    47              thread = threading.Thread(target=generate_load, args=(host, vehicle))
    48              thread.daemon = True
    49              threads.append(thread)
    50              thread.start()
    51              print(f"Started thread for {host}/{vehicle}")
    52      
    53      # Keep the main thread running
    54      try:
    55          while True:
    56              time.sleep(1)
    57      except KeyboardInterrupt:
    58          print("Shutting down load generator")