From e9b6b00adec2c4be7625f466f9d6ddac4c182d57 Mon Sep 17 00:00:00 2001 From: Ewa Socala Date: Tue, 11 Aug 2015 09:43:29 +0200 Subject: [PATCH] Added consume cpu function to Resource Consumer --- .../for-tests/resource-consumer/Dockerfile | 5 +- contrib/for-tests/resource-consumer/Makefile | 18 ++++-- .../consume-cpu/consume_cpu.go | 56 +++++++++++++++++++ .../resource-consumer/resource_consumer.go | 1 + .../resource_consumer_handler.go | 5 +- contrib/for-tests/resource-consumer/utils.go | 10 +++- 6 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 contrib/for-tests/resource-consumer/consume-cpu/consume_cpu.go diff --git a/contrib/for-tests/resource-consumer/Dockerfile b/contrib/for-tests/resource-consumer/Dockerfile index cadde00e487..4b2c4c78d1f 100644 --- a/contrib/for-tests/resource-consumer/Dockerfile +++ b/contrib/for-tests/resource-consumer/Dockerfile @@ -1,5 +1,6 @@ FROM scratch MAINTAINER Ewa Socala -ADD resource-consumer /resource-consumer +ADD consumer /consumer +ADD consume-cpu /consume-cpu EXPOSE 8080 -ENTRYPOINT ["/resource-consumer"] +ENTRYPOINT ["/consumer"] diff --git a/contrib/for-tests/resource-consumer/Makefile b/contrib/for-tests/resource-consumer/Makefile index 871c1248b62..b843a6fab51 100644 --- a/contrib/for-tests/resource-consumer/Makefile +++ b/contrib/for-tests/resource-consumer/Makefile @@ -1,7 +1,17 @@ -all: resource_consumer +all: clean consumer -resource_consumer: - CGO_ENABLED=0 go build -a -installsuffix cgo --ldflags '-w' . +TAG = alpha + +consumer: + CGO_ENABLED=0 go build -a -installsuffix cgo --ldflags '-w' -o consume-cpu/consume-cpu ./consume-cpu/consume_cpu.go + CGO_ENABLED=0 go build -a -installsuffix cgo --ldflags '-w' -o consumer . + +container: + sudo docker build -t gcr.io/google_containers/resource_consumer:$(TAG) . + +run_container: + docker run --publish=8080:8080 gcr.io/google_containers/resource_consumer:$(TAG) clean: - rm -f resource-consumer + rm -f consumer + rm -f consume-cpu/consume-cpu diff --git a/contrib/for-tests/resource-consumer/consume-cpu/consume_cpu.go b/contrib/for-tests/resource-consumer/consume-cpu/consume_cpu.go new file mode 100644 index 00000000000..6f7c6c6233d --- /dev/null +++ b/contrib/for-tests/resource-consumer/consume-cpu/consume_cpu.go @@ -0,0 +1,56 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "flag" + "math" + "time" + + "bitbucket.org/bertimus9/systemstat" +) + +const sleep = time.Duration(10) * time.Millisecond + +func doSomething() { + for i := 1; i < 10000000; i++ { + x := float64(0) + x += math.Sqrt(0) + } +} + +var ( + milicores = flag.Int("milicores", 0, "milicores number") + durationSec = flag.Int("duration-sec", 0, "duration time in seconds") +) + +func main() { + flag.Parse() + // converte milicores to percentage + milicoresPct := float64(*milicores) / float64(10) + duration := time.Duration(*durationSec) * time.Second + start := time.Now() + first := systemstat.GetProcCPUSample() + for time.Now().Sub(start) < duration { + cpu := systemstat.GetProcCPUAverage(first, systemstat.GetProcCPUSample(), systemstat.GetUptime().Uptime) + if cpu.TotalPct < milicoresPct { + doSomething() + } else { + time.Sleep(sleep) + } + } +} diff --git a/contrib/for-tests/resource-consumer/resource_consumer.go b/contrib/for-tests/resource-consumer/resource_consumer.go index 503e49928ff..61cf558164d 100644 --- a/contrib/for-tests/resource-consumer/resource_consumer.go +++ b/contrib/for-tests/resource-consumer/resource_consumer.go @@ -26,6 +26,7 @@ import ( var port = flag.Int("port", 8080, "Port number.") func main() { + flag.Parse() var resourceConsumerHandler ResourceConsumerHandler log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), resourceConsumerHandler)) } diff --git a/contrib/for-tests/resource-consumer/resource_consumer_handler.go b/contrib/for-tests/resource-consumer/resource_consumer_handler.go index f181b8532a2..f6f7fc32366 100644 --- a/contrib/for-tests/resource-consumer/resource_consumer_handler.go +++ b/contrib/for-tests/resource-consumer/resource_consumer_handler.go @@ -77,10 +77,11 @@ func (handler ResourceConsumerHandler) handleConsumeCPU(w http.ResponseWriter, q http.Error(w, incorrectFunctionArgument, http.StatusBadRequest) return } - ConsumeCPU(milicores, durationSec) + go ConsumeCPU(milicores, durationSec) fmt.Fprintln(w, consumeCPUAddress[1:]) fmt.Fprintln(w, milicores, milicoresQuery) fmt.Fprintln(w, durationSec, durationSecQuery) + } } @@ -101,6 +102,7 @@ func (handler ResourceConsumerHandler) handleConsumeMem(w http.ResponseWriter, q return } ConsumeMem(megabytes, durationSec) + fmt.Fprintln(w, "Warning: not implemented!") fmt.Fprintln(w, consumeMemAddress[1:]) fmt.Fprintln(w, megabytes, megabytesQuery) fmt.Fprintln(w, durationSec, durationSecQuery) @@ -109,5 +111,6 @@ func (handler ResourceConsumerHandler) handleConsumeMem(w http.ResponseWriter, q func (handler ResourceConsumerHandler) handleGetCurrentStatus(w http.ResponseWriter) { GetCurrentStatus() + fmt.Fprintln(w, "Warning: not implemented!") fmt.Fprint(w, getCurrentStatusAddress[1:]) } diff --git a/contrib/for-tests/resource-consumer/utils.go b/contrib/for-tests/resource-consumer/utils.go index ca52b1dedea..96f3a7e7e9f 100644 --- a/contrib/for-tests/resource-consumer/utils.go +++ b/contrib/for-tests/resource-consumer/utils.go @@ -17,12 +17,20 @@ limitations under the License. package main import ( + "fmt" "log" + "os/exec" ) +const consumeCPUBinary = "./consume-cpu/consume-cpu" + func ConsumeCPU(milicores int, durationSec int) { log.Printf("ConsumeCPU milicores: %v, durationSec: %v", milicores, durationSec) - // not implemented + // creating new consume cpu process + arg1 := fmt.Sprintf("-milicores=%d", milicores) + arg2 := fmt.Sprintf("-duration-sec=%d", durationSec) + consumeCPU := exec.Command(consumeCPUBinary, arg1, arg2) + consumeCPU.Start() } func ConsumeMem(megabytes int, durationSec int) {