Skip to content

Commit

Permalink
Merge pull request #1 from crossoverJie/feature/gui-20211115
Browse files Browse the repository at this point in the history
Feature/gui 20211115
  • Loading branch information
crossoverJie authored Nov 27, 2021
2 parents f16ad6a + 556c258 commit 29eca97
Show file tree
Hide file tree
Showing 27 changed files with 2,417 additions and 359 deletions.
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ BINARY=ptg
GOBUILD=go build -ldflags "-s -w" -o ${BINARY}
GOCLEAN=go clean
RMTARGZ=rm -rf *.gz
VERSION=0.0.1
VERSION=0.0.2

# Build
build:
Expand Down Expand Up @@ -39,4 +39,16 @@ release:
$(GOCLEAN)
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD).exe
tar czvf ${BINARY}-win64-${VERSION}.tar.gz ./${BINARY}.exe
$(GOCLEAN)
$(GOCLEAN)


gen-go-proto:
@protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
reflect/gen/user.proto

pkg-win:
fyne package -os windows -src gui/ -icon pic/gopher.png -name ${BINARY} -appVersion $(VERSION)

pkg-macos:
fyne package -os darwin -src gui/ -icon pic/gopher.png -name ${BINARY} -appVersion $(VERSION)
38 changes: 38 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package client

import (
"github.com/crossoverJie/ptg/meta"
"net/http"
"time"
)

type (
Client interface {
Request() (*meta.Response, error)
}
)

const (
Http = "http"
Grpc = "grpc"
)

func NewClient(method, url, requestBody string, meta *meta.Meta) Client {
if meta.Protocol() == Http {
return &httpClient{
Method: method,
Url: url,
RequestBody: requestBody,
httpClient: &http.Client{
Transport: &http.Transport{
ResponseHeaderTimeout: time.Millisecond * time.Duration(1000),
DisableKeepAlives: true,
},
},
meta: meta,
}
} else {
return NewGrpcClient(meta)
}

}
79 changes: 79 additions & 0 deletions client/grpc_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package client

import (
"context"
"fmt"
"github.com/crossoverJie/ptg/meta"
"github.com/crossoverJie/ptg/reflect"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/dynamic/grpcdynamic"
"google.golang.org/grpc"
"sync"
"time"
)

var (
one sync.Once
g *grpcClient
)

type grpcClient struct {
stub grpcdynamic.Stub
mds *desc.MethodDescriptor
meta *meta.Meta
parseReflect *reflect.ParseReflect
}

func NewGrpcClient(meta *meta.Meta) Client {
one.Do(func() {
if g == nil {
var (
opts []grpc.DialOption
)

g = &grpcClient{
meta: meta,
}
opts = append(opts, grpc.WithInsecure())
conn, err := grpc.DialContext(context.Background(), meta.Target(), opts...)
if err != nil {
panic(fmt.Sprintf("create grpc connection err %v", err))
}
g.stub = grpcdynamic.NewStub(conn)

parse, err := reflect.NewParse(meta.ProtocolFile())
if err != nil {
panic(fmt.Sprintf("create grpc parse reflect err %v", err))
}
g.parseReflect = parse
serviceName, methodName, err := reflect.ParseServiceMethod(meta.Fqn())
if err != nil {
panic(fmt.Sprintf("parse MethodDescriptor err %v", err))
}
g.mds, err = g.parseReflect.MethodDescriptor(serviceName, methodName)
if err != nil {
panic(fmt.Sprintf("create grpc MethodDescriptor err %v", err))
}

}
})
return g
}

func (g *grpcClient) Request() (*meta.Response, error) {
//fmt.Printf("%p \n", &*g)\
start := time.Now()
rpc, err := g.parseReflect.InvokeRpc(context.Background(), g.stub, g.mds, g.meta.Body())
r := &meta.Response{
RequestTime: time.Since(start),
}
//SlowRequestTime = r.slowRequest()
//FastRequestTime = r.fastRequest()
meta.GetResult().SetSlowRequestTime(r.SlowRequest()).
SetFastRequestTime(r.FastRequest())
if err != nil {
return nil, err
}
r.ResponseSize = len(rpc.String())
return r, nil
}
71 changes: 16 additions & 55 deletions client.go → client/http_client.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,25 @@
package main
package client

import (
"errors"
"fmt"
"github.com/crossoverJie/ptg/meta"
"github.com/pkg/errors"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
)

type (
Client interface {
Request() (*Response, error)
}

httpClient struct {
Method string
Url string
RequestBody string
httpClient *http.Client
}

Response struct {
RequestTime time.Duration
ResponseSize int
}
)

func NewClient(method, url, requestBody string) Client {
if protocol == Http {
return &httpClient{
Method: method,
Url: url,
RequestBody: requestBody,
httpClient: &http.Client{
Transport: &http.Transport{
ResponseHeaderTimeout: time.Millisecond * time.Duration(1000),
DisableKeepAlives: true,
},
},
}
} else {
return NewGrpcClient()
}

type httpClient struct {
Method string
Url string
RequestBody string
httpClient *http.Client
meta *meta.Meta
}

func (c *httpClient) Request() (*Response, error) {
func (c *httpClient) Request() (*meta.Response, error) {
var payload io.Reader
if len(c.RequestBody) > 0 {
payload = strings.NewReader(`{"name":"abc"}`)
Expand All @@ -59,7 +31,7 @@ func (c *httpClient) Request() (*Response, error) {
return nil, err
}
req.Header.Add("User-Agent", "ptg")
for k, v := range headerMap {
for k, v := range c.meta.HeaderMap() {
req.Header.Add(k, v)
}

Expand All @@ -72,11 +44,13 @@ func (c *httpClient) Request() (*Response, error) {

start := time.Now()
response, err := c.httpClient.Do(req)
r := &Response{
r := &meta.Response{
RequestTime: time.Since(start),
}
SlowRequestTime = r.slowRequest()
FastRequestTime = r.fastRequest()
//SlowRequestTime = r.slowRequest()
//FastRequestTime = r.fastRequest()
meta.GetResult().SetSlowRequestTime(r.SlowRequest()).
SetFastRequestTime(r.FastRequest())
if err != nil {
return nil, err
}
Expand All @@ -100,16 +74,3 @@ func (c *httpClient) Request() (*Response, error) {
r.ResponseSize = len(body)
return r, nil
}

func (r *Response) fastRequest() time.Duration {
if r.RequestTime < FastRequestTime {
return r.RequestTime
}
return FastRequestTime
}
func (r *Response) slowRequest() time.Duration {
if r.RequestTime > SlowRequestTime {
return r.RequestTime
}
return SlowRequestTime
}
19 changes: 8 additions & 11 deletions client_test.go → client/http_client_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package main
package client

import (
"fmt"
"github.com/crossoverJie/ptg/meta"
"github.com/docker/go-units"
"testing"
)

func Test_client_Request(t *testing.T) {
httpClient := NewClient("GET", "http://gobyexample.com", "")
request, err := httpClient.Request()
if err != nil {
fmt.Println(err)
}
fmt.Println(request)
}
func Test_client_Request2(t *testing.T) {
httpClient := NewClient("POST", "http://localhost:8080/post", `{"name":"abc"}`)
func Test_httpClient_Request(t *testing.T) {
meta.NewResult()
httpClient := NewClient("GET",
"http://gobyexample.com", "",
meta.NewMeta("http://gobyexample.com", "GET", "", "", Http, "",
"", 1, 1, nil, nil))
request, err := httpClient.Request()
if err != nil {
fmt.Println(err)
Expand Down
49 changes: 28 additions & 21 deletions count.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package main

import (
ptgclient "github.com/crossoverJie/ptg/client"
"github.com/crossoverJie/ptg/meta"
"github.com/crossoverJie/ptg/model"
"github.com/docker/go-units"
"github.com/fatih/color"
"os"
"sync"
"sync/atomic"
"time"
)

type CountModel struct {
wait sync.WaitGroup
count int
workCh chan *Job
workCh chan *model.Job
start time.Time
result *meta.Result
meta *meta.Meta
}

func NewCountModel(count int) Model {
return &CountModel{count: count, start: time.Now()}
func NewCountModel(count int) model.Model {
return &CountModel{count: count, start: time.Now(), result: meta.GetResult(), meta: meta.GetMeta()}
}

func (c *CountModel) Init() {
c.wait.Add(c.count)
c.workCh = make(chan *Job, c.count)
c.workCh = make(chan *model.Job, c.count)
for i := 0; i < c.count; i++ {
go func() {
c.workCh <- &Job{
thread: thread,
duration: duration,
count: c.count,
target: target,
c.workCh <- &model.Job{
Thread: thread,
Duration: duration,
Count: c.count,
Target: target,
}
}()
}
Expand All @@ -43,12 +47,13 @@ func (c *CountModel) Run() {
if !ok {
return
}
httpClient := NewClient(method, job.target, body)
httpClient := ptgclient.NewClient(method, job.Target, body, c.meta)
response, err := httpClient.Request()
respCh <- response
c.meta.RespCh() <- response
if err != nil {
color.Red("request err %v\n", err)
atomic.AddInt32(&ErrorCount, 1)
//atomic.AddInt32(&ErrorCount, 1)
c.result.IncrementErrorCount()
}
Bar.Increment()
c.wait.Done()
Expand All @@ -62,10 +67,12 @@ func (c *CountModel) Run() {
func (c *CountModel) Finish() {
for i := 0; i < c.count; i++ {
select {
case res := <-respCh:
case res := <-c.meta.RespCh():
if res != nil {
totalRequestTime += res.RequestTime
totalResponseSize += res.ResponseSize
//totalRequestTime += res.RequestTime
//totalResponseSize += res.ResponseSize
meta.GetResult().SetTotalRequestTime(res.RequestTime).
SetTotalResponseSize(res.ResponseSize)
}
}
}
Expand All @@ -79,9 +86,9 @@ func (c *CountModel) Shutdown() {
}

func (c *CountModel) PrintSate() {
color.Green("%v requests in %v, %v read, and cost %v.\n", c.count, units.HumanDuration(totalRequestTime), units.HumanSize(float64(totalResponseSize)), units.HumanDuration(time.Since(c.start)))
color.Green("Avg Req Time:\t\t%v\n", totalRequestTime/time.Duration(c.count))
color.Green("Fastest Request:\t%v\n", FastRequestTime)
color.Green("Slowest Request:\t%v\n", SlowRequestTime)
color.Green("Number of Errors:\t%v\n", ErrorCount)
color.Green("%v requests in %v, %v read, and cost %v.\n", c.count, units.HumanDuration(c.result.TotalRequestTime()), units.HumanSize(float64(c.result.TotalResponseSize())), units.HumanDuration(time.Since(c.start)))
color.Green("Avg Req Time:\t\t%v\n", c.result.TotalRequestTime()/time.Duration(c.count))
color.Green("Fastest Request:\t%v\n", c.result.FastRequestTime())
color.Green("Slowest Request:\t%v\n", c.result.SlowRequestTime())
color.Green("Number of Errors:\t%v\n", c.result.ErrorCount())
}
Loading

0 comments on commit 29eca97

Please sign in to comment.