Skip to content

Commit

Permalink
add ilogtail file2blackhole benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Assassin718 committed Jul 10, 2024
1 parent 4e25935 commit b17ae40
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 33 deletions.
3 changes: 3 additions & 0 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/alibaba/ilogtail/test/engine/cleanup"
"github.com/alibaba/ilogtail/test/engine/control"
"github.com/alibaba/ilogtail/test/engine/setup"
"github.com/alibaba/ilogtail/test/engine/setup/monitor"
"github.com/alibaba/ilogtail/test/engine/setup/subscriber"
"github.com/alibaba/ilogtail/test/engine/trigger"
"github.com/alibaba/ilogtail/test/engine/verify"
Expand Down Expand Up @@ -87,10 +88,12 @@ func scenarioInitializer(ctx *godog.ScenarioContext) {

// When
ctx.When(`^generate \{(\d+)\} regex logs, with interval \{(\d+)\}ms$`, trigger.RegexSingle)
ctx.When(`^generate \{(\d+)\} logs every \{(\d+)\}ms, total \{(\d+)\}min, to file \{(.*)\}, template`, trigger.GenerateLogToFile)
ctx.When(`^generate \{(\d+)\} http logs, with interval \{(\d+)\}ms, url: \{(.*)\}, method: \{(.*)\}, body:`, trigger.HTTP)
ctx.When(`^add k8s label \{(.*)\}`, control.AddLabel)
ctx.When(`^remove k8s label \{(.*)\}`, control.RemoveLabel)
ctx.When(`^start docker-compose \{(\S+)\}`, setup.StartDockerComposeEnv)
ctx.When(`^start monitor \{(\S+)\}`, monitor.StartMonitor)

// Then
ctx.Then(`^there is \{(\d+)\} logs$`, verify.LogCount)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@input
Feature: performance file to blackhole iLogtail
Performance file to blackhole iLogtail

@e2e-performance @docker-compose
Scenario: PerformanceFileToBlackholeiLogtail
Given {docker-compose} environment
Given subcribe data from {grpc} with config
"""
"""
Given {performance-file-to-blackhole-ilogtail-case} local config as below
"""
enable: true
inputs:
- Type: input_file
FilePaths:
- /home/test-log/json.log
processors:
- Type: processor_parse_json_native
SourceKey: content
- Type: processor_filter_regex_native
FilterKey:
- user-agent
FilterRegex:
- ^no-agent$
flushers:
- Type: flusher_stdout
OnlyStdout: true
Tags: true
"""
Given iLogtail container mount {./a.log} to {/home/test-log/json.log}
When start docker-compose {performance_file_to_blackhole_ilogtail}
When start monitor {e2e-ilogtailC-1}
When generate {50} logs every {1}ms, total {1}min, to file {./a.log}, template
"""
{"url": "POST /PutData?Category=YunOsAccountOpLog HTTP/1.1", "ip": "10.200.98.220", "user-agent": "aliyun-sdk-java", "request": {"status": "200", "latency": "18204"}, "time": "07/Jul/2022:10:30:28"}
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 iLogtail Authors
#
# 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.

version: '3.8'

services:
cadvisor:
image: gcr.io/cadvisor/cadvisor:v0.49.1
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
- /dev/disk/:/dev/disk:ro
ports:
- "8080:8080"
privileged: true
devices:
- /dev/kmsg
restart: unless-stopped
1 change: 1 addition & 0 deletions test/engine/cleanup/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func All() {
_, _ = control.RemoveAllLocalConfig(ctx)
_, _ = AllGeneratedLog(ctx)
_, _ = GoTestCache(ctx)
_, _ = StopMonitor(ctx)
_, _ = DeleteContainers(ctx)
if subscriber.TestSubscriber != nil {
_ = subscriber.TestSubscriber.Stop()
Expand Down
11 changes: 11 additions & 0 deletions test/engine/cleanup/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cleanup

import (
"context"

"github.com/alibaba/ilogtail/test/engine/setup/monitor"
)

func StopMonitor(ctx context.Context) (context.Context, error) {
return monitor.StopMonitor(ctx)
}
132 changes: 132 additions & 0 deletions test/engine/setup/monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package monitor

import (
"context"
"fmt"
"log"
"os"
"sync/atomic"
"time"

"github.com/google/cadvisor/client"
v1 "github.com/google/cadvisor/info/v1"

"github.com/alibaba/ilogtail/test/config"
)

const (
cadvisorURL = "http://localhost:8080/"
interval = 3
)

var stopCh chan bool
var isMonitoring atomic.Bool

func StartMonitor(ctx context.Context, containerName string) (context.Context, error) {
stopCh = make(chan bool)
isMonitoring.Store(true)
// connect to cadvisor
client, err := client.NewClient("http://localhost:8080/")
if err != nil {
return ctx, err
}
// 获取机器信息
_, err = client.MachineInfo()
if err != nil {
// 处理错误
return ctx, err
}
// fmt.Println("Machine Info:", machineInfo)
fmt.Println("Start monitoring container:", containerName)
go monitoring(client, containerName)
return ctx, nil
}

func StopMonitor(ctx context.Context) (context.Context, error) {
if isMonitoring.Load() {
stopCh <- true
}
return ctx, nil
}

func monitoring(client *client.Client, containerName string) {
// create csv file
reportDir := config.CaseHome + "/report/"
if _, err := os.Stat(reportDir); os.IsNotExist(err) {
// 文件夹不存在,创建文件夹
err := os.MkdirAll(reportDir, 0755) // 使用适当的权限
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)
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
header := "Timestamp,CPU Total(%),CPU User(%),CPU System(%),CPU Load,Memory Usage(MB)\n"
_, err = file.WriteString(header)
if err != nil {
fmt.Println("Error writring file header:", err)
return
}
// new ticker
ticker := time.NewTicker(interval * time.Second)
defer ticker.Stop()
// read from cadvisor per interval seconds
request := &v1.ContainerInfoRequest{NumStats: 10}
var lastStat *v1.ContainerStats
for {
select {
case <-stopCh:
return
case <-ticker.C:
// 获取容器信息
containerInfo, err := client.DockerContainer(containerName, request)
if err != nil {
fmt.Println("Error getting container info:", err)
return
}
for _, stat := range containerInfo.Stats {
if lastStat == nil {
lastStat = stat
continue
}
timestamp := stat.Timestamp
if !timestamp.After(lastStat.Timestamp) {
continue
}
// 计算cpu使用率
cpuUsageRateTotal, cpuUsageRateUser, cpuUsageRateSys := calculateCpuUsageRate(lastStat, stat)
// 写入文件
_, err := file.WriteString(fmt.Sprintf("%s,%f,%f,%f,%d,%d\n",
timestamp.Format("2006-01-02 15:04:05"),
cpuUsageRateTotal,
cpuUsageRateUser,
cpuUsageRateSys,
stat.Cpu.LoadAverage,
stat.Memory.Usage/1024/1024,
))
if err != nil {
fmt.Println("Error writing file:", err)
return
}
lastStat = stat
}
}
}
}

func calculateCpuUsageRate(lastStat, stat *v1.ContainerStats) (float64, float64, float64) {
if lastStat == nil {
return 0, 0, 0
}
cpuUsageTotal := stat.Cpu.Usage.Total - lastStat.Cpu.Usage.Total
cpuUsageUser := stat.Cpu.Usage.User - lastStat.Cpu.Usage.User
cpuUsageSys := stat.Cpu.Usage.System - lastStat.Cpu.Usage.System
cpuUsageRateTotal := float64(cpuUsageTotal) / float64(stat.Timestamp.Sub(lastStat.Timestamp).Nanoseconds()) * 100
cpuUsageRateUser := float64(cpuUsageUser) / float64(stat.Timestamp.Sub(lastStat.Timestamp).Nanoseconds()) * 100
cpuUsageRateSys := float64(cpuUsageSys) / float64(stat.Timestamp.Sub(lastStat.Timestamp).Nanoseconds()) * 100
return cpuUsageRateTotal, cpuUsageRateUser, cpuUsageRateSys
}
45 changes: 45 additions & 0 deletions test/engine/trigger/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package trigger

import (
"context"
"fmt"
"os"
"path/filepath"
"time"

"github.com/alibaba/ilogtail/test/config"
)

func GenerateLogToFile(ctx context.Context, cnt, interval, totalTime int, path string, templateStr string) (context.Context, error) {
// 打开或创建文件
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)
if err != nil {
return ctx, err
}
defer file.Close()

// 创建定时器
ticker := time.NewTicker(time.Millisecond * time.Duration(interval))
defer ticker.Stop()

// 总时间控制
timeout := time.After(time.Minute * time.Duration(totalTime))

for {
select {
case <-ctx.Done(): // 上下文取消
return ctx, ctx.Err()
case <-timeout: // 总时间到
return ctx, nil
case <-ticker.C: // 定时器触发
for i := 0; i < cnt; i++ {
if _, err := fmt.Fprintln(file, templateStr); err != nil {
return ctx, err
}
}
}
}
}
29 changes: 15 additions & 14 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ require (
github.com/alibabacloud-go/tea v1.2.1
github.com/avast/retry-go/v4 v4.6.0
github.com/cucumber/godog v0.14.1
github.com/docker/docker v20.10.23+incompatible
github.com/docker/docker v20.10.27+incompatible
github.com/docker/go-connections v0.4.0
github.com/elastic/go-elasticsearch/v8 v8.6.0
github.com/google/cadvisor v0.49.1
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/melbahja/goph v1.4.0
github.com/mitchellh/mapstructure v1.5.0
github.com/testcontainers/testcontainers-go v0.14.0
golang.org/x/crypto v0.10.0
google.golang.org/grpc v1.53.0
golang.org/x/crypto v0.16.0
google.golang.org/grpc v1.58.3
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.23.4
k8s.io/apimachinery v0.23.4
Expand Down Expand Up @@ -53,7 +54,7 @@ require (
github.com/cucumber/gherkin/go/v26 v26.2.0 // indirect
github.com/cucumber/messages/go/v21 v21.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/elastic-transport-go/v8 v8.0.0-20211216131617-bbee439d559c // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand All @@ -79,13 +80,13 @@ require (
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/sys/mount v0.3.3 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/opencontainers/runc v1.1.12 // indirect
github.com/paulmach/orb v0.8.0 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
Expand All @@ -99,21 +100,21 @@ require (
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
Expand Down
Loading

0 comments on commit b17ae40

Please sign in to comment.