Skip to content

Commit

Permalink
fix lint check errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Assassin718 committed Jul 11, 2024
1 parent 88ebf59 commit 762a3f3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
22 changes: 17 additions & 5 deletions test/engine/setup/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"sync/atomic"
"time"

Expand Down Expand Up @@ -51,20 +52,28 @@ func StopMonitor(ctx context.Context) (context.Context, error) {

func monitoring(client *client.Client, containerName string) {
// create csv file
reportDir := config.CaseHome + "/report/"
if _, err := os.Stat(reportDir); os.IsNotExist(err) {
reportDir := filepath.Join(config.CaseHome, "report")
reportDir, err := filepath.Abs(reportDir)
if err != nil {
log.Fatalf("Failed to get absolute path: %s", err)
}
if _, err = os.Stat(reportDir); os.IsNotExist(err) {
// 文件夹不存在,创建文件夹
err := os.MkdirAll(reportDir, 0755) // 使用适当的权限
err := os.MkdirAll(reportDir, 0750) // 使用适当的权限
if err != nil {
log.Fatalf("Failed to create folder: %s", err)
}
}
file, err := os.OpenFile(reportDir+"performance.csv", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
reportDir = filepath.Join(reportDir, "performance.csv")
reportDir, err = filepath.Abs(reportDir)
if err != nil {
log.Fatalf("Failed to get absolute path: %s", err)
}
file, err := os.OpenFile(reportDir+"performance.csv", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
header := "CPU Usage Max(%),CPU Usage Avg(%), Memory Usage Max(MB), Memory Usage Avg(MB)\n"
_, err = file.WriteString(header)
if err != nil {
Expand All @@ -81,6 +90,9 @@ func monitoring(client *client.Client, containerName string) {
select {
case <-stopCh:
fmt.Fprintln(file, monitorStatistic.cpu.maxVal, ",", monitorStatistic.cpu.avgVal, ",", monitorStatistic.mem.maxVal, ",", monitorStatistic.mem.avgVal)
if err = file.Close(); err != nil {
fmt.Println("Error closing file:", err)
}
return
case <-ticker.C:
// 获取容器信息
Expand Down
24 changes: 12 additions & 12 deletions test/engine/setup/monitor/statistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
v1 "github.com/google/cadvisor/info/v1"
)

type Statistic struct {
type Info struct {
maxVal float64
avgVal float64
cnt int64
}

func (s *Statistic) Add(val float64) {
func (s *Info) Add(val float64) {
if s.cnt < 1 {
s.maxVal = val
s.avgVal = val
Expand All @@ -26,32 +26,32 @@ func (s *Statistic) Add(val float64) {
s.cnt++
}

type MonitorStatistic struct {
cpu Statistic
mem Statistic
type Statistic struct {
cpu Info
mem Info
lastStat *v1.ContainerStats
}

func NewMonitorStatistic() *MonitorStatistic {
return &MonitorStatistic{
cpu: Statistic{},
mem: Statistic{},
func NewMonitorStatistic() *Statistic {
return &Statistic{
cpu: Info{},
mem: Info{},
lastStat: nil,
}
}

func (m *MonitorStatistic) UpdateStatistic(stat *v1.ContainerStats) {
func (m *Statistic) UpdateStatistic(stat *v1.ContainerStats) {
if m.lastStat != nil && !stat.Timestamp.After(m.lastStat.Timestamp) {
return
}
cpuUsageRateTotal := calculateCpuUsageRate(m.lastStat, stat)
cpuUsageRateTotal := calculateCPUUsageRate(m.lastStat, stat)
m.cpu.Add(cpuUsageRateTotal)
m.mem.Add(float64(stat.Memory.Usage) / 1024 / 1024)
m.lastStat = stat
fmt.Println("CPU Usage Rate(%):", cpuUsageRateTotal, "CPU Usage Rate Max(%):", m.cpu.maxVal, "CPU Usage Rate Avg(%):", m.cpu.avgVal, "Memory Usage Max(MB):", m.mem.maxVal, "Memory Usage Avg(MB):", m.mem.avgVal)
}

func calculateCpuUsageRate(lastStat, stat *v1.ContainerStats) float64 {
func calculateCPUUsageRate(lastStat, stat *v1.ContainerStats) float64 {
if lastStat == nil {
return 0
}
Expand Down
9 changes: 7 additions & 2 deletions test/engine/trigger/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ func GenerateLogToFile(ctx context.Context, speed, totalTime int, path string, t
if !filepath.IsAbs(path) {
path = filepath.Join(config.CaseHome, path)
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return ctx, err
}
defer file.Close()

// interval = templateLen / speed
interval := time.Microsecond * time.Duration(len(templateStr)/speed)
Expand All @@ -33,8 +32,14 @@ func GenerateLogToFile(ctx context.Context, speed, totalTime int, path string, t
for {
select {
case <-ctx.Done(): // 上下文取消
if err := file.Close(); err != nil {
return ctx, err
}
return ctx, ctx.Err()
case <-timeout: // 总时间到
if err := file.Close(); err != nil {
return ctx, err
}
return ctx, nil
default:
if err := limiter.Wait(ctx); err != nil {
Expand Down

0 comments on commit 762a3f3

Please sign in to comment.