Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
a simple tutorial for optimizing go program by some useful tools
| Date | Stars |
|---|---|
| 2026-07-24 | 263 |
| 2026-07-25 | 263 |
| 2026-07-28 | 263 |
| 2026-07-30 | 263 |
| 2026-08-06 | 263 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
0.0
growth rate 0.00%/day
# Tutorial for optimizing golang program
> Inspired By cch123's article [pprof 和火焰图](http://xargin.com/pprof-he-huo-yan-tu/)
There're lots of powerful tools for optimizing golang program and some of those you may have already known but still not used it.So let's get start from a very simple demo,the `main.go`
*Follow the article and change the `main.go` yourself when needed*
But first of all let's have a glimpse of it:
``` go
package main
import (
"bytes"
"io/ioutil"
"log"
"math/rand"
"net/http"
_ "net/http/pprof"
)
func main() {
http.HandleFunc("/test", handler)
log.Fatal(http.ListenAndServe(":9876", nil))
}
func handler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if nil != err {
w.Write([]byte(err.Error()))
return
}
log.Println(r.Form)
doSomeThingOne(10000)
buff := genSomeBytes()
b, err := ioutil.ReadAll(buff)
if nil != err {
w.Write([]byte(err.Error()))
return
}
w.Write(b)
}
func doSomeThingOne(times int) {
for i := 0; i < times; i++ {
for j := 0; j < times; j++ {
}
}
}
func genSomeBytes() *bytes.Buffer {
var buff bytes.Buffer
for i := 1; i < 20000; i++ {
buff.Write([]byte{'0' + byte(rand.Intn(10))})
}
return &buff
}
```
Make sure you have imported the `net/http/pprof` at the top of the code.
The demo above is very simple, it sets up a server and handles the request with the function, `handler`.The handler does three things:
* ParseForm
* doSomeThingOne
* genSomeBytes
all of them are aiming to simulate the reality.
Then you can run `go run main.go` to start the server.
## Mock Requests
Actually, to optimize your system you should know the bottleneck of it therefore you need put the server into a very busy condition just as in production.That's what [wrk](https://github.com/wg/wrk) does.
For more detail about the `wrk`,see its [github page](https://github.com/wg/wrk)
#### Install wrk
To install the `wrk`,you need only:
* git clone https://github.com/wg/wrk.git
* cd wrk
* make
**wrk relies on the openssl and luajit, learn more from its github page**
#### Generating requests
Our demo is listening on the port *9876*,so let's generate some requests for that.
`./wrk -c400 -t8 -d5m http://localhost:9876/test`
* `-c400` means we have 400 connections to keep open
* `-t8` means we use 8 threads to build requests
* `-d5m` means the duration of the test will last for 5 minutes
#### pprof
Our server is very busy now and we can see some information via browser.
Input `localhost:9876/debug/pprof` you will see:

The information in this page can't help you find the bottleneck or the bug directly.
* If you think your system is not as fast as you expect, see `localhost:9876/debug/pprof/profile`.
* If you want to optimize the memory, see `localhost:9876/debug/pprof/heap`
In this article I'll just show you how to find the bottleneck in profile,but you can apply the same way into finding the bottleneck of memory.
Now open your terminal and run `go tool pprof http://localhost:9876/debug/pprof/profile` and see what will happen.
Mostly you have to wait for 30s to see the output because pprof need time to do the sampling.The output maybe look like:

*Tip: Input `help` if you don't know what to do*
One of the most important commands in pprof is `top x`(x is a number and default 10).

The report of `top 10` is divided into six rows
* `flat`: How much time was spent to run the function which is showed in the last column.
* `cum`: How much time was spend to run the function and functions invoked by it.
* `sum%`: sum%(line(i)) = flat%(line(i)) + sum%(line(i-1))
*The latest output from pprof is different from what you maybe learned before from [Profiling Go Programs](https://blog.golang.org/profiling-go-programs), but it doesn't matter*
You can dope out the meaning of other fields.
### Analyse
You can learn easily from the report that our server spent 79.25s out of 86.73s Excerpt of 8,848 characters
Read on GitHubWould you bet a product on this? Bounded 0–100 and slow moving.
matched fp:f89c2dd8040803f6, topic:tutorial, desc:tutorial, readme:tutorial