暫無描述

main.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package main
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. _ "net/http/pprof"
  9. "os"
  10. "github.com/gin-gonic/gin"
  11. "github.com/google/pprof/pkg/driver"
  12. "github.com/google/pprof/profile"
  13. "github.com/sirupsen/logrus"
  14. )
  15. func main() {
  16. r := gin.Default()
  17. r.POST("/upload", upload)
  18. r.GET("/pprof/:hash/:method", mapping)
  19. r.Run(":8090")
  20. }
  21. func mapping(c *gin.Context) {
  22. hash := c.Param("hash")
  23. if hash == "" {
  24. c.AbortWithStatus(http.StatusNotFound)
  25. return
  26. }
  27. filename := fmt.Sprintf("%s.pprof", hash)
  28. f, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
  29. if err != nil {
  30. c.AbortWithError(http.StatusInternalServerError, err)
  31. return
  32. }
  33. defer f.Close()
  34. prof, err := profile.Parse(f)
  35. if err != nil {
  36. c.AbortWithError(http.StatusInternalServerError, err)
  37. return
  38. }
  39. ui, err := driver.MakeWebInterface(prof, nil)
  40. if err != nil {
  41. c.AbortWithError(http.StatusInternalServerError, err)
  42. return
  43. }
  44. method := c.Param("method")
  45. switch method {
  46. case "", "dot":
  47. ui.Dot(c.Writer, c.Request)
  48. case "top":
  49. ui.Top(c.Writer, c.Request)
  50. case "disasm":
  51. ui.Disasm(c.Writer, c.Request)
  52. case "source":
  53. ui.Source(c.Writer, c.Request)
  54. case "peek":
  55. ui.Peek(c.Writer, c.Request)
  56. case "flamegraph":
  57. ui.Flamegraph(c.Writer, c.Request)
  58. case "saveconfig":
  59. ui.SaveConfig(c.Writer, c.Request)
  60. case "deleteconfig":
  61. ui.DeleteConfig(c.Writer, c.Request)
  62. default:
  63. c.AbortWithStatus(http.StatusNotFound)
  64. }
  65. }
  66. func upload(c *gin.Context) {
  67. f, _, err := c.Request.FormFile("file")
  68. if err != nil {
  69. logrus.Errorln("upload file error:", err)
  70. c.AbortWithError(http.StatusInternalServerError, err)
  71. return
  72. }
  73. fdata, _ := ioutil.ReadAll(f)
  74. f.Close()
  75. rhash := hash(fdata)
  76. if fileExist(rhash) { // 文件已存在
  77. c.Redirect(http.StatusFound, fmt.Sprintf("/pprof/%s/flamegraph", rhash))
  78. return
  79. }
  80. fileDst := fmt.Sprintf("%s.pprof", rhash)
  81. file, err := os.Create(fileDst)
  82. if err != nil {
  83. c.AbortWithError(http.StatusInternalServerError, err)
  84. return
  85. }
  86. defer file.Close()
  87. if _, err := file.Write(fdata); err != nil {
  88. c.AbortWithError(http.StatusInternalServerError, err)
  89. return
  90. }
  91. c.JSONP(http.StatusOK, map[string]interface{}{
  92. "code": 0,
  93. "msg": "ok",
  94. "data": rhash,
  95. })
  96. }
  97. func hash(data []byte) string {
  98. h := md5.New()
  99. h.Write(data)
  100. return hex.EncodeToString(h.Sum(nil))
  101. }
  102. func fileExist(file string) bool {
  103. return false
  104. }