golang 走起五 Profile 的应用

golang 走起(五) Profile 的应用

  1. 本地

代码:localprofile.go

package main

import (
    "flag"
    "log"
    "os"
    "runtime/pprof"

    "os/signal"
)

var cpuprofile = flag.String("cpuprofile","","write cpu profile to file")

func main() {
    flag.Parse()

    interrupt := make(chan os.Signal, 1)
    signal.Notify(interrupt,os.Interrupt)

    if *cpuprofile != "" {
        f,err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal(err)
        }
        pprof.StartcpuProfile(f)
        defer pprof.StopcpuProfile()
    }

    go func() {
        gagagagaga(30,"A","B","C")
        hahahahaha(30,"C")
        log.Println("complete")
    }()

    for {
        select {
        case <-interrupt:
            log.Println("interrupt")
            // To cleanly close a connection,a client should send a close
            // frame and wait for the server to close the connection.
            return
        }
    }
}

func gagagagaga(n int,A string,B string,C string) {
    // 把A盘里面的圆圈转移到C盘里面【A--C】。
    if n == 1 {
        //把最后一个圆环从起点盘移动到目标盘。
        //cout << "移动圆圈" << n << "从盘" << A << "盘" << C << endl;
    } else {
        // 把N-1个圆环从起点盘移动到(当前)没有任何圆环的过度盘;通过B、C盘在此函数调用调用位置的互换,来实现把N-1个圆环从A盘到B盘的转移【A--B】。
        gagagagaga(n-1,A,C,B)
        //cout << "移动圆圈" << n << "从盘" << A << "盘" << C << endl;
        // 把N-1个圆环从国度盘移动到目标盘(模仿1和2的操作方法来实现);通过A、B盘在此函数调用中位置的互换,来实现N-1个圆环从B盘到C盘的转移【B--C】。
        gagagagaga(n-1,B,C)
    }
}

func hahahahaha(n int,C string) {
    // 把A盘里面的圆圈转移到C盘里面【A--C】。
    if n == 1 {
        //把最后一个圆环从起点盘移动到目标盘。
        //cout << "移动圆圈" << n << "从盘" << A << "盘" << C << endl;
    } else {
        // 把N-1个圆环从起点盘移动到(当前)没有任何圆环的过度盘;通过B、C盘在此函数调用调用位置的互换,来实现把N-1个圆环从A盘到B盘的转移【A--B】。
        hahahahaha(n-1,B)
        //cout << "移动圆圈" << n << "从盘" << A << "盘" << C << endl;
        // 把N-1个圆环从国度盘移动到目标盘(模仿1和2的操作方法来实现);通过A、B盘在此函数调用中位置的互换,来实现N-1个圆环从B盘到C盘的转移【B--C】。
        hahahahaha(n-1,C)
    }
}

编译
go build localprofile.go

运行
./localprofile -cpuprofile=local.prof

等待complete 日志打出,Ctrl+c 运行退出程序

运行
go tool pprof localprofile local.prof

结果

这里输入top,接可以查看了
从结果上,看出hahahahaha和gagagagaga占用最多,并且ha是ga的2倍

  1. 服务器

代码

package main

import (
    "flag"
    "log"
    "net/http"

    _ "net/http/pprof"
    "os"

    "os/signal"
)

func main() {
    flag.Parse()

    interrupt := make(chan os.Signal,os.Interrupt)

    go func() {
        log.Println("begin connect")
        log.Println(http.ListenAndServe(*addr,nil))
    }()

    http.HandleFunc("/ha",ha)
    http.HandleFunc("/ga",ga)

    for {
        select {
        case <-interrupt:
            log.Println("interrupt")
            // To cleanly close a connection,C)
    }
}

var addr = flag.String("addr",":8080","http service address")

func ha(w http.ResponseWriter,r *http.Request) {
    log.Printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    hahahahaha(30,"C")
}

func ga(w http.ResponseWriter,r *http.Request) {
    log.Printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
    gagagagaga(30,"C")
}

编译
go build httpprofile.go

运行
./httpprofile

在另一terminal中,运行
go tool pprof http://127.0.0.1:8080/debug/pprof/profile

在浏览器中运行
http://127.0.0.1:8080/ha
http://127.0.0.1:8080/ga
自己对应ip地址

结果如下

相关文章

什么是Go的接口? 接口可以说是一种类型,可以粗略的理解为他...
1、Golang指针 在介绍Golang指针隐式间接引用前,先简单说下...
1、概述 1.1&#160;Protocol buffers定义 Protocol buffe...
判断文件是否存在,需要用到"os"包中的两个函数: os.Stat(...
1、编译环境 OS :Loongnix-Server Linux release 8.3 CPU指...
1、概述 Golang是一种强类型语言,虽然在代码中经常看到i:=1...