douzhangli9563 2015-11-05 16:11
浏览 23

Golang概要分析简单示例,用于培训

I'm doing Go training in my company and I want to present in best way pprof package. I think that My example isn't good enough to simply present power of pprof. I want to use only go std library. Code should give me simplier call graph with some bottleneck.

My current workflow is as below: 1. include pprof in webserver 2. run webserver (it'll gather data for 30 seconds (default)) 3. run some curl shots to webserver 4. present pprof graphs

My code looks like below:

package main

import (
    "fmt"
    "net/http"
    _ "net/http/pprof"
)

func handler(w http.ResponseWriter, r *http.Request) {
    for i := 0; i < 100000000; i++ {
        w.Write([]byte(fmt.Sprintf("%d - %d, ", i, i)))
    }
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

Pprof without filtering

ppfof all

Pprof for handler() function only

enter image description here

  • 写回答

1条回答 默认 最新

  • drwf69817 2015-11-05 16:27
    关注

    How about string concatenation? It produces a fairly linear graph on my machine. Besides, this is a good way to teach people why they should build strings by appenting to a []byte rather than concatenating strings, creating tonnes of garbage.

    func handler(w http.ResponseWriter, r *http.Request) {
        var s string
        for i := 0; i < 100000; i++ {
            s += strconv.Itoa(i)
        }
        w.Write([]byte(s))
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用