From ab691cb3a89ac51c75a05f6cdfcff4a3c3dfcaee Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 16 Jun 2020 15:47:43 +0800 Subject: [PATCH] opt pprof --- base/tcp/tcp.go | 3 +- base/web/cache.go | 3 +- core/code/bench.go | 1 - core/code/pprof.go | 85 +++++++++++++++++++++++++++------------------- meta.go | 10 +++--- 5 files changed, 59 insertions(+), 43 deletions(-) diff --git a/base/tcp/tcp.go b/base/tcp/tcp.go index 38459f31..a75b5be8 100644 --- a/base/tcp/tcp.go +++ b/base/tcp/tcp.go @@ -2,6 +2,7 @@ package tcp import ( "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/cli" "github.com/shylinux/toolkits" "bufio" @@ -24,7 +25,7 @@ func _tcp_port(m *ice.Message) { current = kit.Int(m.Conf(GETPORT, "meta.begin")) } for i := current; i < end; i++ { - if m.Cmd(ice.CLI_SYSTEM, "lsof", "-i", kit.Format(":%d", i)).Append("code") != "0" { + if m.Cmd(cli.SYSTEM, "lsof", "-i", kit.Format(":%d", i)).Append(cli.CMD_CODE) != "0" { m.Conf(GETPORT, "meta.current", i) m.Log_CREATE(GETPORT, i) m.Echo("%d", i) diff --git a/base/web/cache.go b/base/web/cache.go index c096f0cf..e4abef2d 100644 --- a/base/web/cache.go +++ b/base/web/cache.go @@ -158,7 +158,8 @@ func _cache_download(m *ice.Message, r *http.Response, arg ...string) []string { } func CacheCatch(m *ice.Message, kind, name string) *ice.Message { - _cache_catch(m, "catch", kind, name) + arg := _cache_catch(m, "catch", kind, name) + _cache_save(m, arg[0], arg[1], arg[2], arg[3], arg[4:]...) return m } func init() { diff --git a/core/code/bench.go b/core/code/bench.go index 2d9b0aa1..7718eb04 100644 --- a/core/code/bench.go +++ b/core/code/bench.go @@ -142,7 +142,6 @@ func init() { list = append(list, req) } } - m.Echo("%s \n", target) _bench_show(m, kit.Int64(kit.Select(m.Option(NCONN))), kit.Int64(kit.Select(m.Option(NREQS))), list) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { diff --git a/core/code/pprof.go b/core/code/pprof.go index dedb00a7..b4aa6da5 100644 --- a/core/code/pprof.go +++ b/core/code/pprof.go @@ -5,14 +5,16 @@ import ( "github.com/shylinux/icebergs/base/cli" "github.com/shylinux/icebergs/base/web" "github.com/shylinux/toolkits" + "github.com/shylinux/toolkits/task" "net/http" _ "net/http/pprof" "strings" ) +var PPROF = ice.Name("pprof", Index) + const ( - PPROF = "pprof" BINNARY = "binnary" SERVICE = "service" SECONDS = "seconds" @@ -26,62 +28,78 @@ func _pprof_list(m *ice.Message, zone string, id string, field ...interface{}) { // 列表信息 m.Push("操作", ``) m.Push(zone, value, []string{ - kit.MDB_ZONE, kit.MDB_ID, kit.MDB_TYPE, - kit.MDB_NAME, kit.MDB_TEXT, SECONDS, BINNARY, SERVICE, + kit.MDB_ZONE, kit.MDB_ID, + kit.MDB_TYPE, kit.MDB_NAME, kit.MDB_TEXT, + SECONDS, BINNARY, SERVICE, }, val) }) - return + } else { + m.Grows(PPROF, kit.Keys(kit.MDB_HASH, key), kit.MDB_ID, id, func(index int, value map[string]interface{}) { + // 详细信息 + m.Push("detail", value) + m.Push(kit.MDB_KEY, "操作") + m.Push(kit.MDB_VALUE, ``) + }) } - m.Grows(PPROF, kit.Keys(kit.MDB_HASH, key), kit.MDB_ID, id, func(index int, value map[string]interface{}) { - // 详细信息 - m.Push("detail", value) - m.Push(kit.MDB_KEY, "操作") - m.Push(kit.MDB_VALUE, ``) - }) }) } -func _pprof_show(m *ice.Message, zone string, seconds string) { +func _pprof_show(m *ice.Message, zone string, id string, seconds string) { favor := m.Conf(PPROF, kit.Keys(kit.MDB_META, web.FAVOR)) m.Richs(PPROF, nil, zone, func(key string, val map[string]interface{}) { val = val[kit.MDB_META].(map[string]interface{}) - m.Gos(m.Spawn(), func(msg *ice.Message) { - m.Sleep("1s").Grows(PPROF, kit.Keys(kit.MDB_HASH, key), "", "", func(index int, value map[string]interface{}) { + list := []string{} + m.Grows(PPROF, kit.Keys(kit.MDB_HASH, key), "", "", func(index int, value map[string]interface{}) { + task.Put(val, func(task *task.Task) error { // 压测命令 - m.Log_EXPORT(kit.MDB_META, PPROF, kit.MDB_ZONE, zone, kit.MDB_VALUE, kit.Format(value)) - res := web.FavorShow(m.Spawn(), kit.Format(value[kit.MDB_TYPE]), kit.Format(value[kit.MDB_NAME]), kit.Format(value[kit.MDB_TEXT]), kit.Simple(value[kit.MDB_EXTRA])...).Result() - web.FavorInsert(m, favor, kit.Format(value[kit.MDB_TYPE]), kit.Format(value[kit.MDB_TEXT]), res) + m.Sleep("1s").Log_EXPORT(kit.MDB_META, PPROF, kit.MDB_ZONE, zone, kit.MDB_VALUE, kit.Format(value)) + cmd := kit.Format(value[kit.MDB_TYPE]) + arg := kit.Format(value[kit.MDB_TEXT]) + res := web.FavorShow(m.Spawn(), cmd, kit.Format(value[kit.MDB_NAME]), + arg, kit.Simple(value[kit.MDB_EXTRA])...).Result() + web.FavorInsert(m, favor, cmd, arg, res) + list = append(list, cmd+": "+arg, res) + return nil }) }) // 收藏程序 - res := web.CacheCatch(m.Spawn(), kit.MIME_FILE, kit.Format(val[BINNARY])).Append(kit.MDB_DATA) - web.FavorInsert(m, favor, kit.MIME_FILE, val[BINNARY], res) + bin := web.CacheCatch(m.Spawn(), kit.MIME_FILE, kit.Format(val[BINNARY])).Append(kit.MDB_TEXT) + web.FavorInsert(m, favor, kit.MIME_FILE, bin, val[BINNARY]) // 性能分析 msg := m.Cmd(ice.WEB_SPIDE, "self", "cache", "GET", kit.Select("/code/pprof/profile", val[SERVICE]), "seconds", kit.Select("5", seconds)) - m.Cmd(ice.WEB_FAVOR, favor, PPROF, kit.Keys(zone, "pd.gz"), msg.Append(kit.MDB_DATA)) + web.FavorInsert(m, favor, PPROF, msg.Append(kit.MDB_TEXT), kit.Keys(zone, "pd.gz")) // 结果摘要 cmd := kit.Simple(m.Confv(PPROF, "meta.pprof"), "-text", val[BINNARY], msg.Append(kit.MDB_TEXT)) - ls := strings.Split(m.Cmdx(ice.CLI_SYSTEM, cmd), "\n") - if len(ls) > 20 { - ls = ls[:20] + res := strings.Split(m.Cmdx(ice.CLI_SYSTEM, cmd), "\n") + if len(res) > 20 { + res = res[:20] } - web.FavorInsert(m, favor, kit.MIME_FILE, val[BINNARY], res) - web.FavorInsert(m, favor, ice.TYPE_SHELL, strings.Join(cmd, " "), strings.Join(ls, "\n")) + web.FavorInsert(m, favor, ice.TYPE_SHELL, strings.Join(cmd, " "), strings.Join(res, "\n")) + list = append(list, ice.TYPE_SHELL+": "+strings.Join(cmd, " "), strings.Join(res, "\n")) // 结果展示 p := kit.Format("%s:%s", m.Conf(ice.WEB_SHARE, "meta.host"), m.Cmdx("tcp.getport")) m.Option(cli.CMD_STDOUT, "var/daemon/stdout") m.Option(cli.CMD_STDERR, "var/daemon/stderr") m.Cmd(cli.DAEMON, m.Confv(PPROF, "meta.pprof"), "-http="+p, val[BINNARY], msg.Append(kit.MDB_TEXT)) - web.FavorInsert(m, favor, ice.TYPE_SPIDE, msg.Append(kit.MDB_TEXT), "http://"+p+"/ui/top") - m.Echo(p) + + url := "http://" + p + "/ui/top" + web.FavorInsert(m, favor, ice.TYPE_SPIDE, url, msg.Append(kit.MDB_TEXT)) + m.Set(ice.MSG_RESULT).Echo(url).Echo(" \n").Echo("\n") + m.Echo(strings.Join(list, "\n")).Echo("\n") + + m.Push("url", url) + m.Push(PPROF, msg.Append(kit.MDB_TEXT)) + m.Push(SERVICE, strings.Replace(kit.Format(val[SERVICE]), "profile", "", -1)) + m.Push("bin", bin) }) } func _pprof_modify(m *ice.Message, zone, id, pro, set, old string) { + pro = kit.Select(pro, m.Option(kit.MDB_KEY)) m.Richs(PPROF, nil, kit.Select(kit.MDB_FOREACH, zone), func(key string, val map[string]interface{}) { switch pro { case kit.MDB_ZONE, kit.MDB_ID, kit.MDB_TIME: @@ -115,20 +133,17 @@ func _pprof_insert(m *ice.Message, zone, kind, name, text string, arg ...string) }) } func _pprof_create(m *ice.Message, zone string, binnary, service string, seconds string, arg ...string) { - if m.Richs(PPROF, nil, zone, nil) == nil { - m.Rich(PPROF, nil, kit.Data(kit.MDB_ZONE, zone, - // 添加信息 - BINNARY, binnary, SERVICE, service, SECONDS, seconds, arg)) - m.Log_CREATE(kit.MDB_ZONE, zone) - } + m.Rich(PPROF, nil, kit.Data(kit.MDB_ZONE, zone, + // 添加信息 + BINNARY, binnary, SERVICE, service, SECONDS, seconds, arg)) + m.Log_CREATE(kit.MDB_ZONE, zone) } func init() { Index.Merge(&ice.Context{ Configs: map[string]*ice.Config{ PPROF: {Name: "pprof", Help: "性能分析", Value: kit.Data(kit.MDB_SHORT, kit.MDB_ZONE, - PPROF, []string{"go", "tool", "pprof"}, - web.FAVOR, "pprof", + web.FAVOR, "pprof", PPROF, []string{"go", "tool", "pprof"}, )}, }, Commands: map[string]*ice.Command{ @@ -150,7 +165,7 @@ func init() { _pprof_modify(m, m.Option(kit.MDB_ZONE), m.Option(kit.MDB_ID), arg[0], arg[1], kit.Select("", arg, 2)) }}, kit.MDB_SHOW: {Name: "show type name text arg...", Help: "运行", Hand: func(m *ice.Message, arg ...string) { - _pprof_show(m, m.Option(kit.MDB_ZONE), m.Option(SECONDS)) + _pprof_show(m, m.Option(kit.MDB_ZONE), m.Option(kit.MDB_ID), m.Option(SECONDS)) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { _pprof_list(m, kit.Select(kit.MDB_FOREACH, arg, 0), kit.Select("", arg, 1)) diff --git a/meta.go b/meta.go index 59efbfea..f91f68c6 100644 --- a/meta.go +++ b/meta.go @@ -84,12 +84,12 @@ func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Messa } fallthrough default: - if v = kit.Value(value, k); v == nil { - v = kit.Value(value, kit.Keys(kit.MDB_EXTRA, k)) + if v = value[k]; v == nil { + v = value["extra."+k] } - if v == nil && val != nil { - if v = kit.Value(val, k); v == nil { - v = kit.Value(val, kit.Keys(kit.MDB_EXTRA, k)) + if v == nil { + if v = val[k]; v == nil { + v = val["extra."+k] } } }