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

     1  package main
     2  
     3  import (
     4  	"math"
     5  	"math/rand"
     6  
     7  	"github.com/pyroscope-io/pyroscope/pkg/agent/profiler"
     8  )
     9  
    10  func isPrime(n int64) bool {
    11  	for i := int64(2); i <= n; i++ {
    12  		if i*i > n {
    13  			return true
    14  		}
    15  		if n%i == 0 {
    16  			return false
    17  		}
    18  	}
    19  	return false
    20  }
    21  
    22  func slow(n int64) int64 {
    23  	sum := int64(0)
    24  	for i := int64(1); i <= n; i++ {
    25  		sum += i
    26  	}
    27  	return sum
    28  }
    29  
    30  func fast(n int64) int64 {
    31  	sum := int64(0)
    32  	root := int64(math.Sqrt(float64(n)))
    33  	for a := int64(1); a <= n; a += root {
    34  		b := a + root - 1
    35  		if n < b {
    36  			b = n
    37  		}
    38  		sum += (b - a + 1) * (a + b) / 2
    39  	}
    40  	return sum
    41  }
    42  
    43  func run() {
    44  	base := rand.Int63n(1000000) + 1
    45  	for i := int64(0); i < 40000000; i++ {
    46  		n := rand.Int63n(10000) + 1
    47  		if isPrime(base + i) {
    48  			slow(n)
    49  		} else {
    50  			fast(n)
    51  		}
    52  	}
    53  }
    54  
    55  func main() {
    56  	// No need to modify existing settings,
    57  	// pyroscope will override the server address
    58  	profiler.Start(profiler.Config{
    59  		ApplicationName: "adhoc.example.go",
    60  		ServerAddress:   "http://pyroscope:4040",
    61  	})
    62  	run()
    63  }