mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Added consume cpu function to Resource Consumer
This commit is contained in:
parent
0ded91c521
commit
e9b6b00ade
@ -1,5 +1,6 @@
|
|||||||
FROM scratch
|
FROM scratch
|
||||||
MAINTAINER Ewa Socala <socaa@google.com>
|
MAINTAINER Ewa Socala <socaa@google.com>
|
||||||
ADD resource-consumer /resource-consumer
|
ADD consumer /consumer
|
||||||
|
ADD consume-cpu /consume-cpu
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
ENTRYPOINT ["/resource-consumer"]
|
ENTRYPOINT ["/consumer"]
|
||||||
|
@ -1,7 +1,17 @@
|
|||||||
all: resource_consumer
|
all: clean consumer
|
||||||
|
|
||||||
resource_consumer:
|
TAG = alpha
|
||||||
CGO_ENABLED=0 go build -a -installsuffix cgo --ldflags '-w' .
|
|
||||||
|
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:
|
clean:
|
||||||
rm -f resource-consumer
|
rm -f consumer
|
||||||
|
rm -f consume-cpu/consume-cpu
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -26,6 +26,7 @@ import (
|
|||||||
var port = flag.Int("port", 8080, "Port number.")
|
var port = flag.Int("port", 8080, "Port number.")
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
var resourceConsumerHandler ResourceConsumerHandler
|
var resourceConsumerHandler ResourceConsumerHandler
|
||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), resourceConsumerHandler))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), resourceConsumerHandler))
|
||||||
}
|
}
|
||||||
|
@ -77,10 +77,11 @@ func (handler ResourceConsumerHandler) handleConsumeCPU(w http.ResponseWriter, q
|
|||||||
http.Error(w, incorrectFunctionArgument, http.StatusBadRequest)
|
http.Error(w, incorrectFunctionArgument, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ConsumeCPU(milicores, durationSec)
|
go ConsumeCPU(milicores, durationSec)
|
||||||
fmt.Fprintln(w, consumeCPUAddress[1:])
|
fmt.Fprintln(w, consumeCPUAddress[1:])
|
||||||
fmt.Fprintln(w, milicores, milicoresQuery)
|
fmt.Fprintln(w, milicores, milicoresQuery)
|
||||||
fmt.Fprintln(w, durationSec, durationSecQuery)
|
fmt.Fprintln(w, durationSec, durationSecQuery)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -101,6 +102,7 @@ func (handler ResourceConsumerHandler) handleConsumeMem(w http.ResponseWriter, q
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
ConsumeMem(megabytes, durationSec)
|
ConsumeMem(megabytes, durationSec)
|
||||||
|
fmt.Fprintln(w, "Warning: not implemented!")
|
||||||
fmt.Fprintln(w, consumeMemAddress[1:])
|
fmt.Fprintln(w, consumeMemAddress[1:])
|
||||||
fmt.Fprintln(w, megabytes, megabytesQuery)
|
fmt.Fprintln(w, megabytes, megabytesQuery)
|
||||||
fmt.Fprintln(w, durationSec, durationSecQuery)
|
fmt.Fprintln(w, durationSec, durationSecQuery)
|
||||||
@ -109,5 +111,6 @@ func (handler ResourceConsumerHandler) handleConsumeMem(w http.ResponseWriter, q
|
|||||||
|
|
||||||
func (handler ResourceConsumerHandler) handleGetCurrentStatus(w http.ResponseWriter) {
|
func (handler ResourceConsumerHandler) handleGetCurrentStatus(w http.ResponseWriter) {
|
||||||
GetCurrentStatus()
|
GetCurrentStatus()
|
||||||
|
fmt.Fprintln(w, "Warning: not implemented!")
|
||||||
fmt.Fprint(w, getCurrentStatusAddress[1:])
|
fmt.Fprint(w, getCurrentStatusAddress[1:])
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,20 @@ limitations under the License.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const consumeCPUBinary = "./consume-cpu/consume-cpu"
|
||||||
|
|
||||||
func ConsumeCPU(milicores int, durationSec int) {
|
func ConsumeCPU(milicores int, durationSec int) {
|
||||||
log.Printf("ConsumeCPU milicores: %v, durationSec: %v", milicores, durationSec)
|
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) {
|
func ConsumeMem(megabytes int, durationSec int) {
|
||||||
|
Loading…
Reference in New Issue
Block a user