GithubHelp home page GithubHelp logo

profiling-samples's Introduction

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

profiling-samples's People

Contributors

rafaelhl avatar

Stargazers

José Antonio avatar William Queiroz avatar  avatar Horácio Dias Baptista Neto avatar Diego R. Santos avatar Davi Araújo avatar Sandys Nunes avatar Juliana Barbosa avatar  avatar Paloma Ribeiro avatar felipe.silvestri@mercadolivre.com avatar Felipe Caputo avatar Fernando Lira avatar Samuel Santos da Silva avatar Gustavo França avatar  avatar

Watchers

Vanderson Campanholi avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.