forked from x/icebergs
opt pprof
This commit is contained in:
parent
ab691cb3a8
commit
fe694db0fb
4
base.go
4
base.go
@ -1,13 +1,12 @@
|
||||
package ice
|
||||
|
||||
import (
|
||||
"github.com/shylinux/toolkits"
|
||||
kit "github.com/shylinux/toolkits"
|
||||
"github.com/shylinux/toolkits/conf"
|
||||
"github.com/shylinux/toolkits/log"
|
||||
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@ -148,7 +147,6 @@ func Run(arg ...string) string {
|
||||
Pulse.Option("cache.limit", "30")
|
||||
Pulse.Option("begin_time", Pulse.Time())
|
||||
|
||||
runtime.GOMAXPROCS(1)
|
||||
if frame.Begin(Pulse.Spawns(), arg...).Start(Pulse, arg...) {
|
||||
frame.Close(Pulse.Spawns(), arg...)
|
||||
}
|
||||
|
@ -11,33 +11,38 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var RUNTIME = ice.Name("runtime", nil)
|
||||
|
||||
var Index = &ice.Context{Name: "cli", Help: "命令模块",
|
||||
Caches: map[string]*ice.Cache{},
|
||||
Configs: map[string]*ice.Config{
|
||||
ice.CLI_RUNTIME: {Name: "runtime", Help: "运行环境", Value: kit.Dict()},
|
||||
RUNTIME: {Name: "runtime", Help: "运行环境", Value: kit.Dict()},
|
||||
},
|
||||
Commands: map[string]*ice.Command{
|
||||
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||
m.Load()
|
||||
|
||||
// 启动配置
|
||||
m.Conf(ice.CLI_RUNTIME, "conf.ctx_self", os.Getenv("ctx_self"))
|
||||
m.Conf(ice.CLI_RUNTIME, "conf.ctx_dev", os.Getenv("ctx_dev"))
|
||||
m.Conf(ice.CLI_RUNTIME, "conf.ctx_shy", os.Getenv("ctx_shy"))
|
||||
m.Conf(ice.CLI_RUNTIME, "conf.ctx_pid", os.Getenv("ctx_pid"))
|
||||
m.Conf(RUNTIME, "conf.ctx_self", os.Getenv("ctx_self"))
|
||||
m.Conf(RUNTIME, "conf.ctx_dev", os.Getenv("ctx_dev"))
|
||||
m.Conf(RUNTIME, "conf.ctx_shy", os.Getenv("ctx_shy"))
|
||||
m.Conf(RUNTIME, "conf.ctx_pid", os.Getenv("ctx_pid"))
|
||||
|
||||
// 主机信息
|
||||
m.Conf(ice.CLI_RUNTIME, "host.GOARCH", runtime.GOARCH)
|
||||
m.Conf(ice.CLI_RUNTIME, "host.GOOS", runtime.GOOS)
|
||||
m.Conf(ice.CLI_RUNTIME, "host.pid", os.Getpid())
|
||||
m.Conf(RUNTIME, "host.GOARCH", runtime.GOARCH)
|
||||
m.Conf(RUNTIME, "host.GOOS", runtime.GOOS)
|
||||
m.Conf(RUNTIME, "host.pid", os.Getpid())
|
||||
|
||||
n := kit.Int(kit.Select("20", m.Conf(RUNTIME, "host.GOMAXPROCS")))
|
||||
m.Logs("host", "gomaxprocs", n)
|
||||
runtime.GOMAXPROCS(n)
|
||||
|
||||
// 启动信息
|
||||
if name, e := os.Hostname(); e == nil {
|
||||
m.Conf(ice.CLI_RUNTIME, "boot.hostname", kit.Select(name, os.Getenv("HOSTNAME")))
|
||||
m.Conf(RUNTIME, "boot.hostname", kit.Select(name, os.Getenv("HOSTNAME")))
|
||||
}
|
||||
if user, e := user.Current(); e == nil {
|
||||
m.Conf(ice.CLI_RUNTIME, "boot.username", path.Base(kit.Select(user.Name, os.Getenv("USER"))))
|
||||
m.Cmd(ice.AAA_ROLE, "root", m.Conf(ice.CLI_RUNTIME, "boot.username"))
|
||||
m.Conf(RUNTIME, "boot.username", path.Base(kit.Select(user.Name, os.Getenv("USER"))))
|
||||
m.Cmd(ice.AAA_ROLE, "root", m.Conf(RUNTIME, "boot.username"))
|
||||
}
|
||||
if name, e := os.Getwd(); e == nil {
|
||||
name = path.Base(kit.Select(name, os.Getenv("PWD")))
|
||||
@ -45,25 +50,25 @@ var Index = &ice.Context{Name: "cli", Help: "命令模块",
|
||||
name = ls[len(ls)-1]
|
||||
ls = strings.Split(name, "\\")
|
||||
name = ls[len(ls)-1]
|
||||
m.Conf(ice.CLI_RUNTIME, "boot.pathname", name)
|
||||
m.Conf(RUNTIME, "boot.pathname", name)
|
||||
}
|
||||
|
||||
// 启动记录
|
||||
count := m.Confi(ice.CLI_RUNTIME, "boot.count") + 1
|
||||
m.Conf(ice.CLI_RUNTIME, "boot.count", count)
|
||||
count := m.Confi(RUNTIME, "boot.count") + 1
|
||||
m.Conf(RUNTIME, "boot.count", count)
|
||||
|
||||
// 节点信息
|
||||
m.Conf(ice.CLI_RUNTIME, "node.time", m.Time())
|
||||
m.Conf(ice.CLI_RUNTIME, "node.type", ice.WEB_WORKER)
|
||||
m.Conf(ice.CLI_RUNTIME, "node.name", m.Conf(ice.CLI_RUNTIME, "boot.pathname"))
|
||||
m.Log("info", "runtime %v", kit.Formats(m.Confv(ice.CLI_RUNTIME)))
|
||||
m.Conf(RUNTIME, "node.time", m.Time())
|
||||
m.Conf(RUNTIME, "node.type", ice.WEB_WORKER)
|
||||
m.Conf(RUNTIME, "node.name", m.Conf(RUNTIME, "boot.pathname"))
|
||||
m.Log("info", "runtime %v", kit.Formats(m.Confv(RUNTIME)))
|
||||
}},
|
||||
ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||
m.Save(ice.CLI_RUNTIME, ice.CLI_SYSTEM)
|
||||
m.Save(RUNTIME, SYSTEM)
|
||||
}},
|
||||
|
||||
ice.CLI_RUNTIME: {Name: "runtime", Help: "运行环境", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||
m.Cmdy(ice.CTX_CONFIG, ice.CLI_RUNTIME, arg)
|
||||
RUNTIME: {Name: "runtime", Help: "运行环境", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
|
||||
m.Cmdy(ice.CTX_CONFIG, RUNTIME, arg)
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
var SYSTEM = ice.Name("system", Index)
|
||||
var SYSTEM = ice.Name("system", nil)
|
||||
|
||||
const (
|
||||
CMD_STDOUT = "cmd_stdout"
|
||||
|
@ -1,9 +1,13 @@
|
||||
package code
|
||||
|
||||
import (
|
||||
"github.com/shylinux/icebergs"
|
||||
"github.com/shylinux/toolkits"
|
||||
"time"
|
||||
|
||||
ice "github.com/shylinux/icebergs"
|
||||
kit "github.com/shylinux/toolkits"
|
||||
"github.com/shylinux/toolkits/log"
|
||||
"github.com/shylinux/toolkits/util/bench"
|
||||
"github.com/shylinux/toolkits/util/bench/redis"
|
||||
|
||||
"io"
|
||||
"net/http"
|
||||
@ -55,6 +59,13 @@ func _bench_show(m *ice.Message, nconn, nreq int64, list []*http.Request) {
|
||||
m.Echo(s.Show())
|
||||
m.Echo("body: %d\n", body)
|
||||
}
|
||||
func _bench_redis(m *ice.Message, nconn, nreq int64, hosts []string, cmds []string) {
|
||||
m.Logs("info", "hosts", hosts, "cmds", cmds, nreq, nconn)
|
||||
s, e := redis.Redis(nconn, nreq, hosts, cmds, func(cmd string, arg []interface{}, reply interface{}) {
|
||||
})
|
||||
m.Assert(e)
|
||||
m.Echo("cmds: %s QPS: %.2f n/s AVG: %s time: %s \n", cmds, s.QPS, log.FmtDuration(s.Cost/time.Duration(s.NReq)), log.FmtDuration(s.EndTime.Sub(s.BeginTime)))
|
||||
}
|
||||
func _bench_modify(m *ice.Message, zone, id, pro, set, old string) {
|
||||
m.Richs(BENCH, nil, zone, func(key string, val map[string]interface{}) {
|
||||
switch pro {
|
||||
@ -110,7 +121,7 @@ func init() {
|
||||
_bench_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) {
|
||||
if len(arg) < 2 {
|
||||
if len(arg) < 4 {
|
||||
m.Richs(BENCH, nil, m.Option(kit.MDB_ZONE), func(key string, val map[string]interface{}) {
|
||||
m.Grows(BENCH, kit.Keys(kit.MDB_HASH, key), kit.MDB_ID, m.Option(kit.MDB_ID), func(index int, value map[string]interface{}) {
|
||||
arg = kit.Simple(value[kit.MDB_TYPE], value[kit.MDB_NAME], value[kit.MDB_TEXT], value[kit.MDB_EXTRA])
|
||||
@ -118,14 +129,30 @@ func init() {
|
||||
})
|
||||
}
|
||||
if len(arg) > 2 {
|
||||
m.Option(kit.MDB_NAME, arg[1])
|
||||
m.Option(kit.MDB_TEXT, arg[2])
|
||||
for i := 3; i < len(arg)-1; i++ {
|
||||
m.Option(arg[i], arg[i+1])
|
||||
}
|
||||
}
|
||||
|
||||
list := []*http.Request{}
|
||||
target := kit.Select(m.Option(kit.MDB_TEXT), arg, 2)
|
||||
if strings.HasPrefix(target, "redis://") {
|
||||
hosts := []string{}
|
||||
cmds := strings.Split((m.Option(kit.MDB_NAME)), ",")
|
||||
for _, v := range strings.Split(target, ",") {
|
||||
hosts = append(hosts, strings.TrimPrefix(v, "redis://"))
|
||||
}
|
||||
nconn := kit.Int64(kit.Select("10", m.Option(NCONN)))
|
||||
nreqs := kit.Int64(kit.Select("5000", m.Option(NREQS)))
|
||||
m.Echo("nconn: %d nreqs: %d\n", nconn, nreqs*nconn)
|
||||
for _, cmd := range cmds {
|
||||
_bench_redis(m, nconn, nreqs, hosts, []string{cmd})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
list := []*http.Request{}
|
||||
for _, v := range strings.Split(target, ",") {
|
||||
switch ls := kit.Split(v); ls[0] {
|
||||
case http.MethodPost:
|
||||
|
@ -1,10 +1,10 @@
|
||||
package code
|
||||
|
||||
import (
|
||||
"github.com/shylinux/icebergs"
|
||||
ice "github.com/shylinux/icebergs"
|
||||
"github.com/shylinux/icebergs/base/cli"
|
||||
"github.com/shylinux/icebergs/base/web"
|
||||
"github.com/shylinux/toolkits"
|
||||
kit "github.com/shylinux/toolkits"
|
||||
"github.com/shylinux/toolkits/task"
|
||||
|
||||
"net/http"
|
||||
@ -43,7 +43,7 @@ func _pprof_list(m *ice.Message, zone string, id string, field ...interface{}) {
|
||||
}
|
||||
})
|
||||
}
|
||||
func _pprof_show(m *ice.Message, zone string, id string, seconds string) {
|
||||
func _pprof_show(m *ice.Message, zone string, id string) {
|
||||
favor := m.Conf(PPROF, kit.Keys(kit.MDB_META, web.FAVOR))
|
||||
|
||||
m.Richs(PPROF, nil, zone, func(key string, val map[string]interface{}) {
|
||||
@ -69,7 +69,7 @@ func _pprof_show(m *ice.Message, zone string, id string, seconds string) {
|
||||
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))
|
||||
msg := m.Cmd(ice.WEB_SPIDE, "self", "cache", "GET", kit.Select("/code/pprof/profile", val[SERVICE]), "seconds", kit.Select("5", kit.Format(val[SECONDS])))
|
||||
web.FavorInsert(m, favor, PPROF, msg.Append(kit.MDB_TEXT), kit.Keys(zone, "pd.gz"))
|
||||
|
||||
// 结果摘要
|
||||
@ -165,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(kit.MDB_ID), m.Option(SECONDS))
|
||||
_pprof_show(m, m.Option(kit.MDB_ZONE), m.Option(kit.MDB_ID))
|
||||
}},
|
||||
}, 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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user