Skip to content

Project created to validate Go Profiling tool with samples

Notifications You must be signed in to change notification settings

rafaelhl/profiling-samples

Repository files navigation

Go Profiling Samples

Some samples implementations to illustrate how to use Go Profiling to improve a Go code.

Samples

This project has three implementations with issues performance common in a Go code.

JSON Unmarshalling sample

Simple implementation that will do a unmarshall multiple times as defined on param.

GET /json/unmarshall?times={times}
func Unmarshall[T any](times int) (result T) {
	file, err := os.ReadFile(fmt.Sprintf("%stest.json", Path))
	if err != nil {
		panic(err)
	}

	for i := 0; i <= times; i++ {
		err = json.NewDecoder(bytes.NewReader(file)).Decode(&result)
		if err != nil {
			panic(err)
		}
	}

	return
}

samples/json.go

Load location with timezone sample

Simple implementation that will load a location with tz multiple times as defined on param.

GET /timezone?times={times}
func Timezone(times int) (tz *time.Location) {
	for i := 0; i <= times; i++ {
		var err error
		tz, err = time.LoadLocation("America/Sao_Paulo")
		if err != nil {
			panic(err)
		}
	}

	return
}

samples/timezone.go

Allocations inside a loop sample

Simple implementation that will create an object and append on slice and iterate it multiple times as defined on param.

GET /aloc?times={times}
func Aloc[O any, T []O](obj O, times int) (result T) {

	for i := 0; i <= times; i++ {
		result = append(result, obj)
	}

	for _, o := range result {
		fmt.Printf("%+v\n", o)
	}

	return
}

samples/aloc.go

Using pprof

On this project we will use the HTTP pkg to run the pprof.

Starting the app

First we will start the app.

go run .

Running the pprof

To generate the pprof data and render it on a web page we have to execute passing the expected endpoint available with follow args:

go tool pprof -http=localhost: http://localhost:7777/debug/pprof/profile?seconds=60

Simulating HTTP requests

Now for the pprof data has some metrics we have to simulate some parallel requests immediately after run the pprof.

curl "http://localhost:7777/json/unmarshall?times=10000000" &
curl "http://localhost:7777/timezone?times=100000" &
curl "http://localhost:7777/aloc?times=1000000" &

Expected web page

After 60 seconds we have to be redirected to a web page as below:

Go tool pprof web interactive

https://youtu.be/hmD9zBjuc74 "Go tool pprof web interactive"

Pull requests with improvements of the samples

About

Project created to validate Go Profiling tool with samples

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages