github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/examples/adhoc/adhoc-push.py (about)

     1  #!/usr/bin/env python
     2  from random import randint
     3  from math import sqrt
     4  
     5  import pyroscope
     6  
     7  
     8  def is_prime(n):
     9      for i in range(2, n):
    10          if i * i > n:
    11              return True
    12          if n % i == 0:
    13              return False
    14      return False
    15  
    16  
    17  def slow(n):
    18      sum = 0
    19      for i in range(1, n+1):
    20          sum += i
    21      return sum
    22  
    23  
    24  def fast(n):
    25      sum = 0
    26      root = int(sqrt(n))
    27      for a in range(1, n+1, root):
    28          b = min(a + root - 1, n)
    29          sum += (b - a + 1) * (a + b) // 2
    30      return sum
    31  
    32  
    33  def run():
    34      base = randint(1, 10**6)
    35      for i in range(2*10**6):
    36          [fast, slow][is_prime(base + i)](randint(1, 10**4))
    37  
    38  
    39  if __name__ == '__main__':
    40      # No need to modify existing settings,
    41      # pyroscope will override the server address
    42      pyroscope.configure(
    43          app_name="adhoc.push.python",
    44          server_address="http://pyroscope:4040",
    45      )
    46      run()