1
0
forked from x/ContextOS
This commit is contained in:
shaoying 2019-07-09 22:21:40 +08:00
parent 10cf54168f
commit f953bc7bc0
4 changed files with 212 additions and 268 deletions

View File

@ -143,6 +143,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
"action": &ctx.Config{Name: "action", Value: map[string]interface{}{}, Help: "交互任务"}, "action": &ctx.Config{Name: "action", Value: map[string]interface{}{}, Help: "交互任务"},
"project": &ctx.Config{Name: "project", Value: map[string]interface{}{ "project": &ctx.Config{Name: "project", Value: map[string]interface{}{
// vim git golang
"ubuntu": map[string]interface{}{
},
"github": "https://github.com/shylinux/context", "github": "https://github.com/shylinux/context",
"env": map[string]interface{}{ "env": map[string]interface{}{
"GOPATH": "https://github.com/shylinux/context", "GOPATH": "https://github.com/shylinux/context",
@ -613,9 +616,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
defer func() { defer func() {
os.Exit(kit.Int(code)) os.Exit(kit.Int(code))
}() }()
time.Sleep(time.Second * 1)
m.Cmd("cli._exit") m.Cmd("cli._exit")
m.Cmd("nfs._exit") m.Cmd("nfs._exit")
time.Sleep(time.Second * 1)
}) })
return return
}}, }},
@ -628,6 +631,8 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
"project": &ctx.Command{Name: "project", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { "project": &ctx.Command{Name: "project", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
switch arg[0] { switch arg[0] {
case "prepare":
case "init": case "init":
if _, e := os.Stat(".git"); e == nil { if _, e := os.Stat(".git"); e == nil {
m.Cmdp(0, []string{"git update"}, []string{"cli.system", "git"}, [][]string{ m.Cmdp(0, []string{"git update"}, []string{"cli.system", "git"}, [][]string{

View File

@ -4,5 +4,9 @@ var version = struct {
host string host string
self int self int
}{ }{
<<<<<<< Updated upstream
"2019-07-08 23:23:11", "com.mac", 57, "2019-07-08 23:23:11", "com.mac", 57,
=======
"2019-07-09 15:35:16", "ZYB-20190522USI", 123,
>>>>>>> Stashed changes
} }

View File

@ -1,11 +1,12 @@
package main package main
import ( import (
"encoding/json"
"bufio" "bufio"
"bytes" "bytes"
"sync/atomic" "encoding/json"
"contexts/ctx"
"fmt" "fmt"
"contexts/ctx"
kit "toolkit"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -13,313 +14,245 @@ import (
"path" "path"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
kit "toolkit"
) )
var Index = &ctx.Context{Name: "test", Help: "接口测试工具", func Input(m *ctx.Message, file string, input chan[]string) {
f, e := os.Open(file)
m.Assert(e)
defer f.Close()
nline := 0
for bio := bufio.NewScanner(f); nline < kit.Int(m.Confx("limit")) && bio.Scan(); {
word := strings.Split(bio.Text(), " ")
if len(word) != 2 {
continue
}
uri := word[0][len(m.Conf("prefix0")) : len(word[0])-1]
arg := word[1][len(m.Conf("prefix1")) : len(word[1])-1]
if len(arg) > m.Confi("nskip") {
continue
}
input <- []string{fmt.Sprintf("%d", nline), uri, arg}
if nline++; nline % kit.Int(m.Confx("nsleep")) == 0 {
fmt.Printf("nline:%d sleep 1s...\n", nline)
time.Sleep(time.Second)
}
}
close(input)
}
func Output(m *ctx.Message, output <-chan[]string) {
os.MkdirAll(m.Conf("outdir"), 0777)
files := map[string]*os.File{}
for {
data, ok := <-output
if !ok {
break
}
uri := data[1]
if files[uri] == nil {
f, _ := os.Create(path.Join(m.Conf("outdir"), strings.Replace(uri, "/", "_", -1)+".txt"))
defer f.Close()
files[uri] = f
}
for _, v:= range data {
fmt.Fprintf(files[uri], v)
}
}
}
func Process(m *ctx.Message, file string, cb func(*ctx.Message, *http.Client, []string, chan []string)) (int32, time.Duration) {
nline, begin := int32(0), time.Now()
input := make(chan []string, kit.Int(m.Confx("nread")))
output := make(chan []string, kit.Int(m.Confx("nwrite")))
go Input(m, file, input)
go Output(m, output)
wg := sync.WaitGroup{}
for i := 0; i < m.Confi("nwork"); i++ {
go func(msg *ctx.Message) {
wg.Add(1)
defer wg.Done()
for {
word, ok := <-input
if !ok {
break
}
atomic.AddInt32(&nline, 1)
cb(msg, &http.Client{Timeout:kit.Duration(m.Conf("timeout"))}, word, output)
}
}(m.Spawn())
}
wg.Wait()
close(output)
return nline, time.Since(begin)
}
func Compare(b1 []byte, b2 []byte) bool {
if len(b1) != len(b2) {
return false
}
if bytes.Compare(b1, b2) == 0 {
return true
}
var d1, d2 interface{}
json.Unmarshal(b1, &d1)
json.Unmarshal(b2, &d2)
s1, _ := json.Marshal(d1)
s2, _ := json.Marshal(d2)
if bytes.Compare(s1, s2) == 0 {
return true
}
return false
}
var Index = &ctx.Context{Name: "test", Help: "测试工具",
Caches: map[string]*ctx.Cache{}, Caches: map[string]*ctx.Cache{},
Configs: map[string]*ctx.Config{ Configs: map[string]*ctx.Config{
"nread": {Name: "nread", Help: "读取Channel长度", Value: 1}, "nread": {Name: "nread", Help: "读取Channel长度", Value: 1},
"nwork": {Name: "nwork", Help: "协程数量", Value: 20}, "nwork": {Name: "nwork", Help: "协程数量", Value: 20},
"limit":{Name: "limit", Help: "请求数量", Value: 100}, "limit":{Name: "limit", Help: "请求数量", Value: 100},
"nskip": {Name: "nskip", Help: "请求限长", Value: 100}, "nskip": {Name: "nskip", Help: "请求限长", Value: 100},
"nsleep": {Name: "nsleep", Help: "阻塞时长", Value: "10000"},
"nwrite":{Name: "nwrite", Help: "输出Channel长度", Value: 1}, "nwrite":{Name: "nwrite", Help: "输出Channel长度", Value: 1},
"outdir": {Name: "outdir", Help: "输出目录", Value: "tmp"}, "outdir": {Name: "outdir", Help: "输出目录", Value: "tmp"},
"timeout": {Name: "timeout", Help: "请求超时", Value: "10s"},
"prefix0": {Name: "prefix0", Help: "请求前缀", Value: "uri["}, "prefix0": {Name: "prefix0", Help: "请求前缀", Value: "uri["},
"prefix1": {Name: "prefix1", Help: "参数前缀", Value: "request_param["}, "prefix1": {Name: "prefix1", Help: "参数前缀", Value: "request_param["},
"timeout": {Name: "timeout", Help: "请求超时", Value: "10s"},
"nsleep": {Name: "nsleep", Help: "阻塞时长", Value: "10000"},
}, },
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"diff": {Name: "diff file server1 server2", "diff": {Name: "diff file server1 server2", Form: map[string]int{"nsleep": 1}, Help:"接口对比工具", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
Form: map[string]int{"nsleep": 1}, if len(arg) < 3 {
Help:"接口对比工具", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { m.Echo("usage: %v", "diff file server1 server2")
os.MkdirAll(m.Conf("outdir"), 0777) return
}
f, e := os.Open(arg[0]) var diff, same, error int32 = 0, 0, 0
m.Assert(e)
defer f.Close()
http.DefaultClient.Timeout = kit.Duration(m.Conf("timeout"))
input := make(chan []string, kit.Int(m.Confx("nread")))
output := make(chan []string, kit.Int(m.Confx("nwrite")))
s, e := f.Stat()
m.Assert(e)
total, count, nline := int(s.Size()), 0, 0
mu := sync.Mutex{}
wg := sync.WaitGroup{}
var all, skip, diff, same, error int32 = 0, 0, 0, 0, 0
begin := time.Now()
api := map[string]int{} api := map[string]int{}
go func() { mu := sync.Mutex{}
for bio := bufio.NewScanner(f); bio.Scan(); {
text := bio.Text()
count += len(text)+1
// fmt.Printf("line: %d %d%% \n", nline, int(count*100.0/total))
word := strings.Split(text, " ") nline, cost := Process(m, arg[0], func(msg *ctx.Message, client *http.Client, word []string, output chan []string) {
if len(word) != 2 { key, uri, args := word[0], word[1], word[2]
continue mu.Lock()
} api[uri]++
uri := word[0][len(m.Conf("prefix0")) : len(word[0])-1] mu.Unlock()
arg := word[1][len(m.Conf("prefix1")) : len(word[1])-1]
if len(arg) > m.Confi("nskip") {
skip++
continue
}
nline++ begin := time.Now()
input <- []string{uri, arg, fmt.Sprintf("%d", nline)} res1, e1 := client.Post(arg[1]+uri, "application/json", bytes.NewReader([]byte(args)))
api[uri]++ t1 := time.Since(begin)
if nline % kit.Int(m.Confx("nsleep")) == 0 {
time.Sleep(time.Second)
fmt.Printf("sleep 1s...\n")
}
}
close(input)
wg.Wait()
close(output)
}()
for i := 0; i < m.Confi("nwork"); i++ { begin = time.Now()
go func(msg *ctx.Message) { res2, e2 := client.Post(arg[2]+uri, "application/json", bytes.NewReader([]byte(args)))
wg.Add(1) t2 := time.Since(begin)
defer func() {
wg.Done()
}()
for { size1, size2 := 0, 0
word, ok := <-input result := "error"
if !ok { var t3, t4 time.Duration
break if e1 != nil || e2 != nil {
} atomic.AddInt32(&error, 1)
uri, args, key := word[0], word[1], word[2] fmt.Printf("%v %d cost: %v/%v error: %v %v\n", key, error, t1, t2, e1, e2)
begin := time.Now() } else if res1.StatusCode != http.StatusOK || res2.StatusCode != http.StatusOK {
res1, e1 := http.Post(arg[1]+uri, "application/json", bytes.NewReader([]byte(args))) atomic.AddInt32(&error, 1)
t1 := time.Since(begin) fmt.Printf("%v %d %s %s cost: %v/%v error: %v %v\n", key, error, "error", uri, t1, t2, res1.StatusCode, res2.StatusCode)
begin = time.Now() } else {
res2, e2 := http.Post(arg[2]+uri, "application/json", bytes.NewReader([]byte(args))) begin = time.Now()
t2 := time.Since(begin) b1, _ := ioutil.ReadAll(res1.Body)
b2, _ := ioutil.ReadAll(res2.Body)
t3 = time.Since(begin)
size1, size2 := 0, 0 begin = time.Now()
result := "error" var num int32
atomic.AddInt32(&all, 1) if Compare(b1, b2) {
var t3, t4 time.Duration atomic.AddInt32(&same, 1)
if e1 != nil || e2 != nil { num = same
atomic.AddInt32(&error, 1) result = "same"
fmt.Printf("%d%% %d cost: %v/%v error: %v %v\n", int(count*100.0/total), error, t1, t2, e1, e2)
} else if res1.StatusCode != http.StatusOK || res2.StatusCode != http.StatusOK { } else {
atomic.AddInt32(&error, 1) atomic.AddInt32(&diff, 1)
fmt.Printf("%d%% %d %s %s cost: %v/%v error: %v %v\n", int(count*100.0/total), error, "error", uri, t1, t2, res1.StatusCode, res2.StatusCode) num = diff
} else { result = "diff"
begin = time.Now() result1 := []string{
b1, _ := ioutil.ReadAll(res1.Body) fmt.Sprintf("index:%v uri:", key),
b2, _ := ioutil.ReadAll(res2.Body) fmt.Sprintf(uri),
t3 = time.Since(begin) fmt.Sprintf(" arguments:"),
fmt.Sprintf(args),
begin = time.Now()
var d1, d2 interface{}
json.Unmarshal(b1, &d1)
json.Unmarshal(b2, &d2)
s1, _ := json.Marshal(d1)
s2, _ := json.Marshal(d2)
var num int32
if bytes.Compare(s1, s2) == 0 {
atomic.AddInt32(&same, 1)
num = same
result = "same"
} else {
atomic.AddInt32(&diff, 1)
num = diff
result = "diff"
result1 := []string{
fmt.Sprintf("index:%v uri:", key),
fmt.Sprintf(uri),
fmt.Sprintf(" arguments:"),
fmt.Sprintf(args),
}
result1 = append(result1, "\n")
result1 = append(result1, "\n")
result1 = append(result1, "result0:")
result1 = append(result1, string(b1))
result1 = append(result1, "\n")
result1 = append(result1, "\n")
result1 = append(result1, "result1:")
result1 = append(result1, string(b2))
result1 = append(result1, "\n")
result1 = append(result1, "\n")
output <- result1
}
size1 = len(b1)
size2 = len(b2)
t4 = time.Since(begin)
fmt.Printf("%d%% %d %s %s size: %v/%v cost: %v/%v diff: %v/%v\n", int(count*100.0/total), num, result, uri, len(b1), len(b2), t1, t2, t3, t4)
} }
mu.Lock() result1 = append(result1, "\n")
m.Add("append", "uri", uri) result1 = append(result1, "\n")
m.Add("append", "time1", t1/1000000) result1 = append(result1, "result0:")
m.Add("append", "time2", t2/1000000) result1 = append(result1, string(b1))
m.Add("append", "time3", t3/1000000) result1 = append(result1, "\n")
m.Add("append", "time4", t4/1000000) result1 = append(result1, "\n")
m.Add("append", "size1", size1) result1 = append(result1, "result1:")
m.Add("append", "size2", size2) result1 = append(result1, string(b2))
m.Add("append", "action", result) result1 = append(result1, "\n")
mu.Unlock() result1 = append(result1, "\n")
output <- result1
} }
}(m.Spawn()) size1 = len(b1)
} size2 = len(b2)
t4 = time.Since(begin)
files := map[string]*os.File{} fmt.Printf("%v %d %s %s size: %v/%v cost: %v/%v diff: %v/%v\n", key, num, result, uri, len(b1), len(b2), t1, t2, t3, t4)
for {
data, ok := <-output
if !ok {
break
} }
uri := data[1] mu.Lock()
if files[uri] == nil { m.Add("append", "uri", uri)
f, _ := os.Create(path.Join(m.Conf("outdir"), strings.Replace(uri, "/", "_", -1)+".txt")) m.Add("append", "time1", t1/1000000)
defer f.Close() m.Add("append", "time2", t2/1000000)
files[uri] = f m.Add("append", "time3", t3/1000000)
} m.Add("append", "time4", t4/1000000)
for _, v:= range data { m.Add("append", "size1", size1)
fmt.Fprintf(files[uri], v) m.Add("append", "size2", size2)
} m.Add("append", "action", result)
} mu.Unlock()
})
fmt.Printf("\n\nnuri: %v nreq: %v skip: %v same: %v diff: %v error: %v cost: %v\n\n", len(api), nline, skip, same, diff, error, time.Since(begin)) fmt.Printf("\n\nnuri: %v nreq: %v same: %v diff: %v error: %v cost: %v\n\n", len(api), nline, same, diff, error, cost)
m.Sort("time1", "int").Table() m.Sort("time1", "int").Table()
return return
}}, }},
"cost": {Name: "cost file server nroute", Help:"接口耗时测试", "cost": {Name: "cost file server nroute", Help:"接口耗时测试", Form: map[string]int{"nwork": 1, "limit": 1, "nsleep": 1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
Form: map[string]int{"nwork": 1, "limit": 1}, var success int32 = 0
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
os.MkdirAll(m.Conf("outdir"), 0777)
f, e := os.Open(arg[0])
m.Assert(e)
defer f.Close()
input := make(chan []string, kit.Int(m.Confx("nread")))
output := make(chan []string, kit.Int(m.Confx("nwrite")))
count, nline := 0, 0
wg := sync.WaitGroup{}
limit := kit.Int(m.Confx("limit"))
var skip, success int32 = 0, 0
begin := time.Now()
api := map[string]int{}
go func() {
for bio := bufio.NewScanner(f); bio.Scan() && nline < limit; {
text := bio.Text()
count += len(text)+1
word := strings.Split(text, " ")
if len(word) != 2 {
continue
}
uri := word[0][len(m.Conf("prefix0")) : len(word[0])-1]
arg := word[1][len(m.Conf("prefix1")) : len(word[1])-1]
if len(arg) > m.Confi("nskip") {
skip++
continue
}
// fmt.Printf("line: %d %d%% %v\n", nline, int(nline/limit), uri)
nline++
input <- []string{uri, arg, fmt.Sprintf("%d", nline)}
api[uri]++
}
close(input)
wg.Wait()
close(output)
}()
var times time.Duration var times time.Duration
for i := 0; i < kit.Int(m.Confx("nwork")); i++ { limit := kit.Int(m.Confx("limit"))
go func(msg *ctx.Message) { nline, cost := Process(m, arg[0], func(msg *ctx.Message, client *http.Client, word []string, output chan []string) {
wg.Add(1) key, uri, args := word[0], word[1], word[2]
defer func() { fmt.Printf("%v/%v post: %v\t%v\n", key, limit, arg[1]+uri, args)
wg.Done()
}()
client := http.Client{Timeout:kit.Duration(m.Conf("timeout"))}
for { begin := time.Now()
word, ok := <-input res, err := client.Post(arg[1]+uri, "application/json", bytes.NewReader([]byte(args)))
if !ok { if res.StatusCode == http.StatusOK {
break io.Copy(ioutil.Discard, res.Body)
} atomic.AddInt32(&success, 1)
uri, args, key := word[0], word[1], word[2] } else {
fmt.Printf("%v/%v error: %v\n", key, limit, err)
fmt.Printf("%v/%v\tpost: %v\t%v\n", key, limit, arg[1]+uri, args)
begin := time.Now()
res, err := client.Post(arg[1]+uri, "application/json", bytes.NewReader([]byte(args)))
if res.StatusCode == http.StatusOK {
io.Copy(ioutil.Discard, res.Body)
atomic.AddInt32(&success, 1)
} else {
fmt.Printf("%v/%v\terror: %v\n", key, limit, err)
}
times += time.Since(begin)
}
}(m.Spawn())
}
files := map[string]*os.File{}
for {
data, ok := <-output
if !ok {
break
} }
times += time.Since(begin)
})
uri := data[1] m.Echo("\n\nnclient: %v nreq: %v success: %v time: %v average: %vms",
if files[uri] == nil { m.Confx("nwork"), nline, success, cost, int(times)/int(nline)/1000000)
f, _ := os.Create(path.Join(m.Conf("outdir"), strings.Replace(uri, "/", "_", -1)+".txt"))
defer f.Close()
files[uri] = f
}
for _, v:= range data {
fmt.Fprintf(files[uri], v)
}
}
m.Echo("\n\nnclient: %v skip: %v nreq: %v success: %v time: %v average: %vms",
m.Confx("nwork"), skip, nline, success, time.Since(begin), int(times)/nline/1000000)
return return
}}, }},
"cmd": {Name: "cmd file", Help:"生成测试命令", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { "cmd": {Name: "cmd file", Help:"生成测试命令", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
f, e := os.Open(arg[0]) str := kit.Select("./hey -n 10000 -c 100 -m POST -T \"application/json\" -d '%s' http://127.0.0.1:6363%s\n", arg, 1)
m.Assert(e) Process(m, arg[0], func(msg *ctx.Message, client *http.Client, word []string, output chan []string) {
defer f.Close() m.Echo(str, word[2], word[1])
for bio := bufio.NewScanner(f); bio.Scan(); { })
text := bio.Text()
word := strings.Split(text, " ")
if len(word) != 2 {
continue
}
uri := word[0][len(m.Conf("prefix0")) : len(word[0])-1]
arg := word[1][len(m.Conf("prefix1")) : len(word[1])-1]
if len(arg) > m.Confi("nskip") {
continue
}
m.Echo("./hey -n 10000 -c 100 -m POST -T \"application/json\" -d '%s' http://127.0.0.1:6363%s\n", arg, uri)
}
return return
}}, }},
}, },

View File

@ -664,6 +664,8 @@ function Plugin(page, pane, field) {
item.ondblclick = function(event) { item.ondblclick = function(event) {
action.value = kit.History.get("txt", -1).data.trim() action.value = kit.History.get("txt", -1).data.trim()
} }
item.autocomplete = "off"
} }
args && count < args.length && (item.value = args[count++]||item.value||"") args && count < args.length && (item.value = args[count++]||item.value||"")
item.className = "args" item.className = "args"