golang 走起(五) Profile 的应用
发布时间:2020-12-16 18:32:26 所属栏目:大数据 来源:网络整理
导读:golang 走起(五) Profile 的应用 本地 代码: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() in
golang 走起(五) Profile 的应用
代码: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)
}
}
编译 运行 等待complete 日志打出,Ctrl+c 运行退出程序 运行 结果 这里输入top,接可以查看了
代码: 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")
}
编译 运行 在另一terminal中,运行 在浏览器中运行 结果如下 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |