Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .editorconfig & cpu load test params #8

Merged
merged 4 commits into from Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions .editorconfig
@@ -0,0 +1,25 @@
# http://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.go]
indent_style = tab
indent_size = 2

[Makefile]
indent_style = tab
indent_size = 4

[.travis.yml]
indent_style = space
indent_size = 2

[*.json]
indent_style = space
indent_size = 2
27 changes: 17 additions & 10 deletions exec/os/bin/burncpu.go
@@ -1,25 +1,33 @@
package main

import (
"runtime"
"path"
"github.com/chaosblade-io/chaosblade/util"
"context"
"flag"
"fmt"
"time"
"github.com/chaosblade-io/chaosblade/exec"
"github.com/chaosblade-io/chaosblade/util"
"path"
"runtime"
"strings"
"flag"
"context"
"time"
)

var burnCpuStart, burnCpuStop, burnCpuNohup bool
var (
burnCpuStart, burnCpuStop, burnCpuNohup bool
numCPU int
)

func main() {
flag.BoolVar(&burnCpuStart, "start", false, "burn cpu")
flag.BoolVar(&burnCpuStop, "stop", false, "stop burn cpu")
flag.BoolVar(&burnCpuNohup, "nohup", false, "nohup to run burn cpu")
flag.IntVar(&numCPU, "numcpu", runtime.NumCPU(), "number of cpus")
flag.Parse()

if numCPU <= 0 || numCPU > runtime.NumCPU() {
numCPU = runtime.NumCPU()
}

if burnCpuStart {
startBurnCpu()
} else if burnCpuStop {
Expand All @@ -30,7 +38,6 @@ func main() {
}

func burnCpu() {
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU)

for i := 0; i < numCPU; i++ {
Expand All @@ -49,7 +56,8 @@ const burnCpuBin = "chaos_burncpu"

// startBurnCpu by invoke burnCpuBin with --nohup flag
func startBurnCpu() {
args := fmt.Sprintf(`%s --nohup > /dev/null 2>&1 &`, path.Join(util.GetProgramPath(), burnCpuBin))
args := fmt.Sprintf(`%s --nohup --numcpu %d > /dev/null 2>&1 &`,
path.Join(util.GetProgramPath(), burnCpuBin), numCPU)
ctx := context.Background()
response := exec.NewLocalChannel().Run(ctx, "nohup", args)
if !response.Success {
Expand All @@ -62,7 +70,6 @@ func startBurnCpu() {
if pids == nil || len(pids) == 0 {
printErrAndExit(fmt.Sprintf("%s pid not found", burnCpuBin))
}
printOutputAndExit(strings.Join(pids, " "))
}

// stopBurnCpu
Expand Down
52 changes: 43 additions & 9 deletions exec/os/cpu.go
@@ -1,10 +1,16 @@
package os

import (
"context"
"fmt"
"github.com/chaosblade-io/chaosblade/exec"
"github.com/chaosblade-io/chaosblade/transport"
. "github.com/chaosblade-io/chaosblade/util"
"log"
. "os/exec"
"path"
"context"
"runtime"
"strconv"
)

type CpuCommandModelSpec struct {
Expand Down Expand Up @@ -33,7 +39,18 @@ func (*CpuCommandModelSpec) Actions() []exec.ExpActionCommandSpec {
}

func (cms *CpuCommandModelSpec) Flags() []exec.ExpFlagSpec {
return []exec.ExpFlagSpec{}
return []exec.ExpFlagSpec{
&exec.ExpFlag{
Name: "timeout",
Desc: "execute timeout",
Required: false,
},
&exec.ExpFlag{
Name: "numcpu",
Desc: "number of cpus",
Required: false,
},
}
}

func (*CpuCommandModelSpec) PreExecutor() exec.PreExecutor {
Expand Down Expand Up @@ -67,13 +84,11 @@ func (*fullLoadActionCommand) LongDesc() string {
}

func (*fullLoadActionCommand) Matchers() []exec.ExpFlagSpec {
return []exec.ExpFlagSpec{
}
return []exec.ExpFlagSpec{}
}

func (*fullLoadActionCommand) Flags() []exec.ExpFlagSpec {
return []exec.ExpFlagSpec{
}
return []exec.ExpFlagSpec{}
}

func (*fullLoadActionCommand) Executor(channel exec.Channel) exec.Executor {
Expand All @@ -95,20 +110,39 @@ func (ce *cpuExecutor) SetChannel(channel exec.Channel) {
}

func (ce *cpuExecutor) Exec(uid string, ctx context.Context, model *exec.ExpModel) *transport.Response {
// set benchmark timeout
if timeout, err := strconv.ParseUint(model.ActionFlags["timeout"], 10, 64); err == nil && timeout > 0 {
script := path.Join(GetProgramPath(), bladeBin)
args := fmt.Sprintf("nohup /bin/sh -c 'sleep %d; %s destroy %s' > /dev/null 2>&1 &",
timeout, script, uid)
cmd := CommandContext(context.TODO(), "/bin/sh", "-c", args)
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}

// number of cpu cores
numcpu, err := strconv.ParseUint(model.ActionFlags["numcpus"], 10, 64)
if err != nil || numcpu <= 0 || int(numcpu) > runtime.NumCPU() {
numcpu = uint64(runtime.NumCPU())
}

if ce.channel == nil {
return transport.ReturnFail(transport.Code[transport.ServerError], "channel is nil")
}
if _, ok := exec.IsDestroy(ctx); ok {
return ce.stop(ctx)
} else {
return ce.start(ctx)
return ce.start(ctx, int(numcpu))
}
}

const burnCpuBin = "chaos_burncpu"
const bladeBin = "blade"

func (ce *cpuExecutor) start(ctx context.Context) *transport.Response {
return ce.channel.Run(ctx, path.Join(ce.channel.GetScriptPath(), burnCpuBin), "--start")
func (ce *cpuExecutor) start(ctx context.Context, numcpu int) *transport.Response {
return ce.channel.Run(ctx, path.Join(ce.channel.GetScriptPath(), burnCpuBin),
fmt.Sprintf("--start --numcpu %d", numcpu))
}

func (ce *cpuExecutor) stop(ctx context.Context) *transport.Response {
Expand Down