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

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"math"
     6  	"math/rand"
     7  	"net/http"
     8  	_ "net/http/pprof"
     9  )
    10  
    11  func isPrime(n int64) bool {
    12  	for i := int64(2); i <= n; i++ {
    13  		if i*i > n {
    14  			return true
    15  		}
    16  		if n%i == 0 {
    17  			return false
    18  		}
    19  	}
    20  	return false
    21  }
    22  
    23  func slow(n int64) int64 {
    24  	sum := int64(0)
    25  	for i := int64(1); i <= n; i++ {
    26  		sum += i
    27  	}
    28  	return sum
    29  }
    30  
    31  func fast(n int64) int64 {
    32  	sum := int64(0)
    33  	root := int64(math.Sqrt(float64(n)))
    34  	for a := int64(1); a <= n; a += root {
    35  		b := a + root - 1
    36  		if n < b {
    37  			b = n
    38  		}
    39  		sum += (b - a + 1) * (a + b) / 2
    40  	}
    41  	return sum
    42  }
    43  
    44  func run() {
    45  	base := rand.Int63n(1000000) + 1
    46  	for i := int64(0); i < 40000000; i++ {
    47  		n := rand.Int63n(10000) + 1
    48  		if isPrime(base + i) {
    49  			slow(n)
    50  		} else {
    51  			fast(n)
    52  		}
    53  	}
    54  }
    55  
    56  func main() {
    57  	go func() {
    58  		log.Println(http.ListenAndServe("localhost:6060", nil))
    59  	}()
    60  	run()
    61  }