github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/hacks/metrics-comparison/golang/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/pyroscope-io/pyroscope/pkg/agent/profiler"
     7  	"os"
     8  )
     9  
    10  //go:noinline
    11  func work(n int) {
    12  	// revive:disable:empty-block this is fine because this is a example app, not real production code
    13  	for i := 0; i < n; i++ {
    14  	}
    15  	// revive:enable:empty-block
    16  }
    17  
    18  func fastFunction() {
    19  	work(2000)
    20  }
    21  
    22  func slowFunction() {
    23  	work(8000)
    24  }
    25  
    26  func main() {
    27  	// allow overwrite pyroscope server url
    28  	pyroscopeURL := os.Getenv("PYROSCOPE_URL")
    29  	if pyroscopeURL == "" {
    30  		pyroscopeURL = "pyroscope:4040" // this will run inside docker-compose, hence `pyroscope` for hostname
    31  	}
    32  
    33  	profiler.Start(profiler.Config{
    34  		ApplicationName: "simple.golang.app",
    35  		ServerAddress:   pyroscopeURL,
    36  	})
    37  
    38  	log.Println("test")
    39  	for {
    40  		fastFunction()
    41  		slowFunction()
    42  	}
    43  }