From c7b117c06397e7590a2b69b835b60050176175fe Mon Sep 17 00:00:00 2001 From: kubeway Date: Sat, 15 Jun 2019 10:59:03 +0800 Subject: [PATCH] fix(Dockerfile): fix Dockerfile --- build/Dockerfile | 13 ++--- build/build-image.sh | 2 +- cmd/app/app.go | 12 ++--- pkg/httpload/testrun.go | 80 ---------------------------- pkg/{httpload => knload}/draw.go | 4 +- pkg/{httpload => knload}/httpload.go | 20 +++---- 6 files changed, 26 insertions(+), 105 deletions(-) delete mode 100644 pkg/httpload/testrun.go rename pkg/{httpload => knload}/draw.go (97%) rename pkg/{httpload => knload}/httpload.go (94%) diff --git a/build/Dockerfile b/build/Dockerfile index cb2f28e..c97fa3b 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,19 +1,20 @@ # Build the manager binary -FROM golang:1.12 as builder +FROM registry.cn-hangzhou.aliyuncs.com/knative-sample/golang:1.12 as builder # Copy in the go src -WORKDIR /go/src/github.com/knative-samples/knload/ +WORKDIR /go/src/github.com/knative-sample/knload/ COPY cmd/ cmd/ COPY pkg/ pkg/ COPY vendor/ vendor/ # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o bin/httpload code.aliyun.com/knative-samples/http-client/cmd/ +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o bin/knload github.com/knative-sample/knload/cmd/ + +FROM registry.cn-hangzhou.aliyuncs.com/knative-sample/alpine-sh:3.9 -FROM alpine:3.9 WORKDIR /app/ RUN mkdir -p /app/bin/ -COPY --from=builder /go/src/code.aliyun.com/knative-samples/http-client/bin/httpload /app/bin/httpload -ENTRYPOINT ["/app/bin/httpload"] +COPY --from=builder /go/src/github.com/knative-sample/knload/bin/knload /app/bin/knload +ENTRYPOINT ["/app/bin/knload"] diff --git a/build/build-image.sh b/build/build-image.sh index 2d8e314..2e2c98f 100755 --- a/build/build-image.sh +++ b/build/build-image.sh @@ -5,7 +5,7 @@ ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -NAME="httpload" +NAME="knload" GIT_COMMIT="$(git rev-parse --verify HEAD)" GIT_BRANCH=`git branch | grep \* | cut -d ' ' -f2` TAG="$(date +%Y''%m''%d''%H''%M''%S)" diff --git a/cmd/app/app.go b/cmd/app/app.go index 2228aa3..1cfb627 100644 --- a/cmd/app/app.go +++ b/cmd/app/app.go @@ -10,7 +10,7 @@ import ( "os" - "github.com/knative-sample/knload/pkg/httpload" + "github.com/knative-sample/knload/pkg/knload" "github.com/golang/glog" ) @@ -53,9 +53,9 @@ func run(stopCh <-chan struct{}, ops *options.Options) { } ss := strings.Split(ops.Stages, ",") - var stages []*httpload.Stage + var stages []*knload.Stage for _, stageStr := range ss { - stage := &httpload.Stage{} + stage := &knload.Stage{} _ss := strings.Split(stageStr, ":") if len(_ss) != 2 { continue @@ -81,16 +81,16 @@ func run(stopCh <-chan struct{}, ops *options.Options) { stages = append(stages, stage) } - hl := &httpload.HttpLoad{ + kl := &knload.Knload{ Namespace: ops.Namespace, LabelSelector: ops.LabelSelector, GatewayAddress: ops.GatewayAddress, SavePath: ops.SavePath, ServiceUrl: ops.ServiceUrl, Stages: stages, - ResultChan: make(chan *httpload.Result, 1000), + ResultChan: make(chan *knload.Result, 1000), } - hl.Run() + kl.Run() os.Exit(0) <-stopCh diff --git a/pkg/httpload/testrun.go b/pkg/httpload/testrun.go deleted file mode 100644 index 6c50573..0000000 --- a/pkg/httpload/testrun.go +++ /dev/null @@ -1,80 +0,0 @@ -package httpload - -import ( - "fmt" - "math" - "math/rand" - "sync" - "time" -) - -var startTime = time.Now() - -func (s *Stage) runtest(resultChan chan *Result) { - for ii := 1; ii <= s.Duration; ii++ { - var wg sync.WaitGroup - wg.Add(s.Concurrent) - startTime = startTime.Add(time.Second) - _rc := make(chan *Result, s.Concurrent) - for i := 0; i < s.Concurrent; i++ { - r := &Result{ - //StartTime: time.Now(), - //StartTime: startTime, - Concurrent: s.Concurrent, - } - rand.Seed(time.Now().UnixNano()) - responseTime := float64(rand.Intn(999)) / 1000 - fmt.Sprintf("responseTime:%f ", responseTime) - if ii < 4 { - responseTime = responseTime + float64(3) - } - go func() { - defer wg.Done() - s.doTest(r, responseTime, _rc) - }() - time.Sleep(time.Microsecond) - } - wg.Wait() - close(_rc) - - // TODO 对当前秒求平均值 - var responseTimeTotal float64 - var podNumTotal float64 - for result := range _rc { - responseTimeTotal += result.ResponseTime - podNumTotal += result.PodNum - } - - resultChan <- &Result{ - Concurrent: s.Concurrent, - PodNum: podNumTotal / float64(s.Concurrent), - ResponseTime: responseTimeTotal / float64(s.Concurrent), - } - } -} - -func (s *Stage) doTest(result *Result, responseTime float64, resultChan chan *Result) { - podNumChan := make(chan float64, 1) - go func(pm chan float64) { - // TODO get pod Num - //pm <- rand.Intn(result.Concurrent) - pm <- math.Ceil(float64(result.Concurrent) / 3) - - }(podNumChan) - - // TODO do request - if responseTime != 0.0 { - //result.EndTime = result.StartTime.Add(time.Duration(float64(time.Second) * responseTime)) - } else { - //r := rand.Intn(5000) - //timelength := time.Microsecond * time.Duration(r) - //result.EndTime = time.Now().Add(timelength) - } - result.ResponseTime = responseTime - - result.PodNum = <-podNumChan - - resultChan <- result - // TODO log result - //result.log() -} diff --git a/pkg/httpload/draw.go b/pkg/knload/draw.go similarity index 97% rename from pkg/httpload/draw.go rename to pkg/knload/draw.go index 5653534..caebf2d 100644 --- a/pkg/httpload/draw.go +++ b/pkg/knload/draw.go @@ -1,4 +1,4 @@ -package httpload +package knload import ( "encoding/base64" @@ -11,7 +11,7 @@ import ( "github.com/golang/glog" ) -func (hl *HttpLoad) Draw() { +func (hl *Knload) Draw() { var wg sync.WaitGroup var concurrentList []int var responseTimeList []float64 diff --git a/pkg/httpload/httpload.go b/pkg/knload/httpload.go similarity index 94% rename from pkg/httpload/httpload.go rename to pkg/knload/httpload.go index a0a1eee..d934b6c 100644 --- a/pkg/httpload/httpload.go +++ b/pkg/knload/httpload.go @@ -1,4 +1,4 @@ -package httpload +package knload import ( "context" @@ -32,7 +32,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" ) -type HttpLoad struct { +type Knload struct { Debug bool Namespace string LabelSelector string @@ -85,7 +85,7 @@ func init() { // * In-cluster config if running in cluster // // * $HOME/.kube/config if exists -func (hl *HttpLoad) getKubeconfig() (*rest.Config, error) { +func (hl *Knload) getKubeconfig() (*rest.Config, error) { // If a flag is specified with the config location, use that if len(kubeconfig) > 0 { return clientcmd.BuildConfigFromFlags(masterURL, kubeconfig) @@ -109,7 +109,7 @@ func (hl *HttpLoad) getKubeconfig() (*rest.Config, error) { return nil, fmt.Errorf("could not locate a kubeconfig") } -func (hl *HttpLoad) Run() { +func (hl *Knload) Run() { var wg sync.WaitGroup var wg1 sync.WaitGroup var wg2 sync.WaitGroup @@ -226,7 +226,7 @@ type DrawHtml struct { C3CSS interface{} } -func (hl *HttpLoad) run(s *Stage, resultChan chan *Result) { +func (hl *Knload) run(s *Stage, resultChan chan *Result) { var wg1 sync.WaitGroup wg1.Add(s.Duration + 1) rm := map[int]*Result{} @@ -311,7 +311,7 @@ func (hl *HttpLoad) run(s *Stage, resultChan chan *Result) { close(resultChan) } -func (hl *HttpLoad) doRequest(resultChan chan float64) { +func (hl *Knload) doRequest(resultChan chan float64) { // do request responseTime, err := hl.getResponseTime() if err != nil { @@ -322,7 +322,7 @@ func (hl *HttpLoad) doRequest(resultChan chan float64) { resultChan <- responseTime } -func (hl *HttpLoad) getResponseTime() (responseTime float64, err error) { +func (hl *Knload) getResponseTime() (responseTime float64, err error) { u, err := url.Parse(fmt.Sprintf("http://%s", hl.ServiceUrl)) if err != nil { glog.Error(err) @@ -410,11 +410,11 @@ func (hl *HttpLoad) getResponseTime() (responseTime float64, err error) { return float64(t4.Sub(t1)) / float64(time.Second), nil } -func (hl *HttpLoad) logResponseTime(msg string, t2, t1 time.Time) { +func (hl *Knload) logResponseTime(msg string, t2, t1 time.Time) { glog.V(5).Infof("%s %s\n", msg, t2.Sub(t1)) } -func (hl *HttpLoad) dialContext(network string) func(ctx context.Context, network, addr string) (net.Conn, error) { +func (hl *Knload) dialContext(network string) func(ctx context.Context, network, addr string) (net.Conn, error) { return func(ctx context.Context, _, addr string) (net.Conn, error) { return (&net.Dialer{ Timeout: 10 * time.Second, @@ -424,7 +424,7 @@ func (hl *HttpLoad) dialContext(network string) func(ctx context.Context, networ } } -func (hl *HttpLoad) readResponseBody(resp *http.Response) string { +func (hl *Knload) readResponseBody(resp *http.Response) string { bodyBytes, err := ioutil.ReadAll(resp.Body) if err != nil { glog.Fatal(err)