Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/rafaelhl/profiling-samples

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/rafaelhl/profiling-samples

  • v0.0.0-20220920113722-e5df7e4472ad
  • Source
  • Go
  • Socket score

Version published
Created
Source

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

FAQs

Package last updated on 20 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc