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,127 +14,166 @@ 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) {
Caches: map[string]*ctx.Cache{}, f, e := os.Open(file)
Configs: map[string]*ctx.Config{
"nread": {Name: "nread", Help: "读取Channel长度", Value: 1},
"nwork": {Name: "nwork", Help: "协程数量", Value: 20},
"limit":{Name: "limit", Help: "请求数量", Value: 100},
"nskip": {Name: "nskip", Help: "请求限长", Value: 100},
"nwrite":{Name: "nwrite", Help: "输出Channel长度", Value: 1},
"outdir": {Name: "outdir", Help: "输出目录", Value: "tmp"},
"prefix0": {Name: "prefix0", Help: "请求前缀", Value: "uri["},
"prefix1": {Name: "prefix1", Help: "参数前缀", Value: "request_param["},
"timeout": {Name: "timeout", Help: "请求超时", Value: "10s"},
"nsleep": {Name: "nsleep", Help: "阻塞时长", Value: "10000"},
},
Commands: map[string]*ctx.Command{
"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) {
os.MkdirAll(m.Conf("outdir"), 0777)
f, e := os.Open(arg[0])
m.Assert(e) m.Assert(e)
defer f.Close() defer f.Close()
http.DefaultClient.Timeout = kit.Duration(m.Conf("timeout"))
input := make(chan []string, kit.Int(m.Confx("nread"))) nline := 0
output := make(chan []string, kit.Int(m.Confx("nwrite"))) for bio := bufio.NewScanner(f); nline < kit.Int(m.Confx("limit")) && bio.Scan(); {
word := strings.Split(bio.Text(), " ")
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{}
go func() {
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, " ")
if len(word) != 2 { if len(word) != 2 {
continue continue
} }
uri := word[0][len(m.Conf("prefix0")) : len(word[0])-1] uri := word[0][len(m.Conf("prefix0")) : len(word[0])-1]
arg := word[1][len(m.Conf("prefix1")) : len(word[1])-1] arg := word[1][len(m.Conf("prefix1")) : len(word[1])-1]
if len(arg) > m.Confi("nskip") { if len(arg) > m.Confi("nskip") {
skip++
continue continue
} }
nline++ input <- []string{fmt.Sprintf("%d", nline), uri, arg}
input <- []string{uri, arg, fmt.Sprintf("%d", nline)} if nline++; nline % kit.Int(m.Confx("nsleep")) == 0 {
api[uri]++ fmt.Printf("nline:%d sleep 1s...\n", nline)
if nline % kit.Int(m.Confx("nsleep")) == 0 {
time.Sleep(time.Second) time.Sleep(time.Second)
fmt.Printf("sleep 1s...\n")
} }
} }
close(input) close(input)
wg.Wait() }
close(output)
}()
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++ { for i := 0; i < m.Confi("nwork"); i++ {
go func(msg *ctx.Message) { go func(msg *ctx.Message) {
wg.Add(1) wg.Add(1)
defer func() { defer wg.Done()
wg.Done()
}()
for { for {
word, ok := <-input word, ok := <-input
if !ok { if !ok {
break break
} }
uri, args, key := word[0], word[1], word[2] 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)
}
begin := time.Now() func Compare(b1 []byte, b2 []byte) bool {
res1, e1 := http.Post(arg[1]+uri, "application/json", bytes.NewReader([]byte(args))) if len(b1) != len(b2) {
t1 := time.Since(begin) return false
}
begin = time.Now() if bytes.Compare(b1, b2) == 0 {
res2, e2 := http.Post(arg[2]+uri, "application/json", bytes.NewReader([]byte(args))) return true
t2 := time.Since(begin) }
size1, size2 := 0, 0
result := "error"
atomic.AddInt32(&all, 1)
var t3, t4 time.Duration
if e1 != nil || e2 != nil {
atomic.AddInt32(&error, 1)
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 {
atomic.AddInt32(&error, 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)
} else {
begin = time.Now()
b1, _ := ioutil.ReadAll(res1.Body)
b2, _ := ioutil.ReadAll(res2.Body)
t3 = time.Since(begin)
begin = time.Now()
var d1, d2 interface{} var d1, d2 interface{}
json.Unmarshal(b1, &d1) json.Unmarshal(b1, &d1)
json.Unmarshal(b2, &d2) json.Unmarshal(b2, &d2)
s1, _ := json.Marshal(d1) s1, _ := json.Marshal(d1)
s2, _ := json.Marshal(d2) s2, _ := json.Marshal(d2)
var num int32
if bytes.Compare(s1, s2) == 0 { if bytes.Compare(s1, s2) == 0 {
return true
}
return false
}
var Index = &ctx.Context{Name: "test", Help: "测试工具",
Caches: map[string]*ctx.Cache{},
Configs: map[string]*ctx.Config{
"nread": {Name: "nread", Help: "读取Channel长度", Value: 1},
"nwork": {Name: "nwork", Help: "协程数量", Value: 20},
"limit":{Name: "limit", Help: "请求数量", Value: 100},
"nskip": {Name: "nskip", Help: "请求限长", Value: 100},
"nsleep": {Name: "nsleep", Help: "阻塞时长", Value: "10000"},
"nwrite":{Name: "nwrite", Help: "输出Channel长度", Value: 1},
"outdir": {Name: "outdir", Help: "输出目录", Value: "tmp"},
"timeout": {Name: "timeout", Help: "请求超时", Value: "10s"},
"prefix0": {Name: "prefix0", Help: "请求前缀", Value: "uri["},
"prefix1": {Name: "prefix1", Help: "参数前缀", Value: "request_param["},
},
Commands: map[string]*ctx.Command{
"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) {
if len(arg) < 3 {
m.Echo("usage: %v", "diff file server1 server2")
return
}
var diff, same, error int32 = 0, 0, 0
api := map[string]int{}
mu := sync.Mutex{}
nline, cost := Process(m, arg[0], func(msg *ctx.Message, client *http.Client, word []string, output chan []string) {
key, uri, args := word[0], word[1], word[2]
mu.Lock()
api[uri]++
mu.Unlock()
begin := time.Now()
res1, e1 := client.Post(arg[1]+uri, "application/json", bytes.NewReader([]byte(args)))
t1 := time.Since(begin)
begin = time.Now()
res2, e2 := client.Post(arg[2]+uri, "application/json", bytes.NewReader([]byte(args)))
t2 := time.Since(begin)
size1, size2 := 0, 0
result := "error"
var t3, t4 time.Duration
if e1 != nil || e2 != nil {
atomic.AddInt32(&error, 1)
fmt.Printf("%v %d cost: %v/%v error: %v %v\n", key, error, t1, t2, e1, e2)
} else if res1.StatusCode != http.StatusOK || res2.StatusCode != http.StatusOK {
atomic.AddInt32(&error, 1)
fmt.Printf("%v %d %s %s cost: %v/%v error: %v %v\n", key, error, "error", uri, t1, t2, res1.StatusCode, res2.StatusCode)
} else {
begin = time.Now()
b1, _ := ioutil.ReadAll(res1.Body)
b2, _ := ioutil.ReadAll(res2.Body)
t3 = time.Since(begin)
begin = time.Now()
var num int32
if Compare(b1, b2) {
atomic.AddInt32(&same, 1) atomic.AddInt32(&same, 1)
num = same num = same
result = "same" result = "same"
@ -165,7 +205,7 @@ var Index = &ctx.Context{Name: "test", Help: "接口测试工具",
size1 = len(b1) size1 = len(b1)
size2 = len(b2) size2 = len(b2)
t4 = time.Since(begin) 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) 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)
} }
mu.Lock() mu.Lock()
@ -178,148 +218,41 @@ var Index = &ctx.Context{Name: "test", Help: "接口测试工具",
m.Add("append", "size2", size2) m.Add("append", "size2", size2)
m.Add("append", "action", result) m.Add("append", "action", result)
mu.Unlock() mu.Unlock()
} })
}(m.Spawn())
}
files := map[string]*os.File{} fmt.Printf("\n\nnuri: %v nreq: %v same: %v diff: %v error: %v cost: %v\n\n", len(api), nline, same, diff, error, cost)
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)
}
}
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))
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 {
word, ok := <-input
if !ok {
break
}
uri, args, key := word[0], word[1], word[2]
fmt.Printf("%v/%v\tpost: %v\t%v\n", key, limit, arg[1]+uri, args)
begin := time.Now() begin := time.Now()
res, err := client.Post(arg[1]+uri, "application/json", bytes.NewReader([]byte(args))) res, err := client.Post(arg[1]+uri, "application/json", bytes.NewReader([]byte(args)))
if res.StatusCode == http.StatusOK { if res.StatusCode == http.StatusOK {
io.Copy(ioutil.Discard, res.Body) io.Copy(ioutil.Discard, res.Body)
atomic.AddInt32(&success, 1) atomic.AddInt32(&success, 1)
} else { } else {
fmt.Printf("%v/%v\terror: %v\n", key, limit, err) fmt.Printf("%v/%v error: %v\n", key, limit, err)
} }
times += time.Since(begin) times += time.Since(begin)
} })
}(m.Spawn())
}
files := map[string]*os.File{} m.Echo("\n\nnclient: %v nreq: %v success: %v time: %v average: %vms",
for { m.Confx("nwork"), nline, success, cost, int(times)/int(nline)/1000000)
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)
}
}
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"