Added consume cpu function to Resource Consumer

pull/6/head
Ewa Socala 2015-08-11 09:43:29 +02:00
parent 0ded91c521
commit e9b6b00ade
6 changed files with 87 additions and 8 deletions

View File

@ -1,5 +1,6 @@
FROM scratch
MAINTAINER Ewa Socala <socaa@google.com>
ADD resource-consumer /resource-consumer
ADD consumer /consumer
ADD consume-cpu /consume-cpu
EXPOSE 8080
ENTRYPOINT ["/resource-consumer"]
ENTRYPOINT ["/consumer"]

View File

@ -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

View File

@ -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)
}
}
}

View File

@ -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))
}

View File

@ -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:])
}

View File

@ -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) {