Skip to content

Commit

Permalink
Merge branch 'xray-performance-test' into traces-performance-test
Browse files Browse the repository at this point in the history
  • Loading branch information
okankoAMZ committed Aug 14, 2023
2 parents c61d69a + a847e06 commit a9f3137
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 126 deletions.
50 changes: 24 additions & 26 deletions mock_server/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import (
"encoding/json"
"io"
"net/http"
"sync"
"testing"
"time"

"github.com/aws/amazon-cloudwatch-agent-test/test/xray"
"github.com/aws/amazon-cloudwatch-agent-test/util/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
//https://github.com/aws-observability/aws-otel-test-framework/blob/terraform/mocked_servers/https/main.go

// https://github.com/aws-observability/aws-otel-test-framework/blob/terraform/mocked_servers/https/main.go
const (
APP_SERVER_ADDR = "http://127.0.0.1"
APP_SERVER_PORT = ":" + "8080"
Expand All @@ -23,22 +21,22 @@ const (
DATA_SERVER_PORT = ":" + "443"
DATA_SERVER = DATA_SERVER_ADDR + DATA_SERVER_PORT

SEND_TEST_DATA_COUNT time.Duration= 10 * time.Second
TPM_CHECK_INTERVAL time.Duration = 10 * time.Second

SEND_TEST_DATA_COUNT time.Duration = 10 * time.Second
TPM_CHECK_INTERVAL time.Duration = 10 * time.Second
)
func ResponseToString(res * http.Response) (string,error){

func ResponseToString(res *http.Response) (string, error) {
responseText, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(responseText), nil
}
func HttpsGetRequest(url string) (string, error){
func HttpsGetRequest(url string) (string, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}

client := &http.Client{Transport: tr}

res, err := client.Get(url)
Expand Down Expand Up @@ -82,20 +80,21 @@ func HttpServerCheckTPM(t *testing.T) {
json.Unmarshal([]byte(resString), &httpData)
tpm, ok := httpData["tpm"]
require.True(t, ok, "tpm json is broken")
assert.Truef(t,tpm.(float64) > 1, "tpm is less than 1 %f", tpm)
assert.Truef(t, tpm.(float64) > 1, "tpm is less than 1 %f", tpm)
}
// func HttpServerSendData(t *testing.T,) {
// var wg sync.WaitGroup
// wg.Add(2)
// go func(){
// defer wg.Done()
// for i := 1; i < 5; i++ {
// HttpServerCheckTPM(t)
// time.Sleep(TPM_CHECK_INTERVAL)
// }
// }()
// wg.Wait()
// }

// func HttpServerSendData(t *testing.T,) {
// var wg sync.WaitGroup
// wg.Add(2)
// go func(){
// defer wg.Done()
// for i := 1; i < 5; i++ {
// HttpServerCheckTPM(t)
// time.Sleep(TPM_CHECK_INTERVAL)
// }
// }()
// wg.Wait()
// }
func TestMockServer(t *testing.T) {
serverControlChan := startHttpServer()
time.Sleep(3 * time.Second)
Expand All @@ -107,7 +106,6 @@ func TestMockServer(t *testing.T) {

}


func TestStartMockServer(t * testing.T){
func TestStartMockServer(t *testing.T) {
startHttpServer()
}
44 changes: 27 additions & 17 deletions test/ca_bundle/ca_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ const (
commonConfigTOML = "/common-config.toml"
targetString = "x509: certificate signed by unknown authority"

// Let the agent run for 30 seconds. This will give agent enough time to call server
agentRuntime = 30 * time.Second
localstackS3Key = "integration-test/ls_tmp/%s"
keyDelimiter = "/"
localstackConfigPath = "../../localstack/ls_tmp/"
originalPem = "original.pem"
combinePem = "combine.pem"
snakeOilPem = "snakeoil.pem"
tmpDirectory = "/tmp/"
runEMF = "sudo bash resources/emf.sh"
logfile = "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
)

type input struct {
findTarget bool
dataInput string
findTarget bool
commonConfigInput string
agentConfigInput string
testType string
}

func init() {
Expand All @@ -58,28 +60,36 @@ func TestBundle(t *testing.T) {

parameters := []input{
//Use the system pem ca bundle + local stack pem file ssl should connect thus target string not found
{dataInput: "resources/integration/ssl/with/combine/bundle", findTarget: false},
{commonConfigInput: "resources/with/combine/", agentConfigInput: "resources/https/", findTarget: false, testType: "metric"},
{commonConfigInput: "resources/with/combine/", agentConfigInput: "resources/https/", findTarget: false, testType: "emf"},
//Do not look for ca bundle with http connection should connect thus target string not found
{dataInput: "resources/integration/ssl/without/bundle/http", findTarget: false},
{commonConfigInput: "resources/without/", agentConfigInput: "resources/http/", findTarget: false, testType: "metric"},
{commonConfigInput: "resources/without/", agentConfigInput: "resources/http/", findTarget: false, testType: "emf"},
//Use the system pem ca bundle ssl should not connect thus target string found
{dataInput: "resources/integration/ssl/with/original/bundle", findTarget: true},
{commonConfigInput: "resources/with/original/", agentConfigInput: "resources/https/", findTarget: true, testType: "metric"},
{commonConfigInput: "resources/with/original/", agentConfigInput: "resources/https/", findTarget: true, testType: "emf"},
//Do not look for ca bundle should not connect thus target string found
{dataInput: "resources/integration/ssl/without/bundle", findTarget: true},
{commonConfigInput: "resources/without/", agentConfigInput: "resources/https/", findTarget: true, testType: "metric"},
{commonConfigInput: "resources/without/", agentConfigInput: "resources/https/", findTarget: true, testType: "emf"},
}

for _, parameter := range parameters {
//before test run
log.Printf("resource file location %s find target %t", parameter.dataInput, parameter.findTarget)
t.Run(fmt.Sprintf("resource file location %s find target %t", parameter.dataInput, parameter.findTarget), func(t *testing.T) {
common.ReplaceLocalStackHostName(parameter.dataInput + configJSON)
t.Logf("config file after localstack host replace %s", string(readFile(parameter.dataInput+configJSON)))
common.CopyFile(parameter.dataInput+configJSON, configOutputPath)
common.CopyFile(parameter.dataInput+commonConfigTOML, commonConfigOutputPath)
configFile := parameter.agentConfigInput + parameter.testType + configJSON
commonConfigFile := parameter.commonConfigInput + commonConfigTOML
log.Printf("common config file location %s agent config file %s find target %t", commonConfigFile, configFile, parameter.findTarget)
t.Run(fmt.Sprintf("common config file location %s agent config file %s find target %t", commonConfigFile, configFile, parameter.findTarget), func(t *testing.T) {
common.RecreateAgentLogfile(logfile)
common.ReplaceLocalStackHostName(configFile)
t.Logf("config file after localstack host replace %s", string(readFile(configFile)))
common.CopyFile(configFile, configOutputPath)
common.CopyFile(commonConfigFile, commonConfigOutputPath)
common.StartAgent(configOutputPath, true, false)
time.Sleep(agentRuntime)
log.Printf("Agent has been running for : %s", agentRuntime.String())
// this command will take 5 seconds time 12 = 1 minute
common.RunCommand(runEMF)
log.Printf("Agent has been running for : %s", time.Minute)
common.StopAgent()
output := common.ReadAgentOutput(agentRuntime)
output := common.ReadAgentLogfile(logfile)
containsTarget := outputLogContainsTarget(output)
if (parameter.findTarget && !containsTarget) || (!parameter.findTarget && containsTarget) {
t.Errorf("Find target is %t contains target is %t", parameter.findTarget, containsTarget)
Expand Down
6 changes: 6 additions & 0 deletions test/ca_bundle/resources/emf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
for times in {1..6}
do
sleep 5
CURRENT_TIME=$(date +%s%N | cut -b1-13)
echo '{"_aws":{"Timestamp":'"${CURRENT_TIME}"',"LogGroupName":"MetricValueBenchmarkTest","CloudWatchMetrics":[{"Namespace":"MetricValueBenchmarkTest","Dimensions":[["Type","InstanceId"]],"Metrics":[{"Name":"EMFCounter","Unit":"Count"}]}]},"Type":"Counter","EMFCounter":5}' \ > /dev/udp/0.0.0.0/25888
done
13 changes: 13 additions & 0 deletions test/ca_bundle/resources/http/emf/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"agent": {
"run_as_user": "root",
"debug": true
},
"logs": {
"endpoint_override": "http://localhost.localstack.cloud:4566",
"metrics_collected": {
"emf": { }
},
"force_flush_interval": 5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"agent": {
"metrics_collection_interval": 10,
"run_as_user": "root",
"debug": true,
"logfile": ""
"debug": true
},
"metrics": {
"endpoint_override": "http://localhost.localstack.cloud:4566",
Expand Down
13 changes: 13 additions & 0 deletions test/ca_bundle/resources/https/emf/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"agent": {
"run_as_user": "root",
"debug": true
},
"logs": {
"endpoint_override": "https://localhost.localstack.cloud:4566",
"metrics_collected": {
"emf": { }
},
"force_flush_interval": 5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"agent": {
"metrics_collection_interval": 10,
"run_as_user": "root",
"debug": true,
"logfile": ""
"debug": true
},
"metrics": {
"endpoint_override": "https://localhost.localstack.cloud:4566",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

17 changes: 0 additions & 17 deletions test/xray/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package xray
import (
"context"
"errors"
"fmt"
"log"
"os"
"path"
"path/filepath"
"time"

"github.com/aws/amazon-cloudwatch-agent-test/util/common"
"github.com/aws/aws-xray-sdk-go/strategy/sampling"
"github.com/aws/aws-xray-sdk-go/xray"
Expand Down Expand Up @@ -40,21 +37,7 @@ func (g *XrayTracesGenerator) StartSendingTraces(ctx context.Context) error {
func (g *XrayTracesGenerator) StopSendingTraces() {
close(g.Done)
}
func listDir() {
err := filepath.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fmt.Println(path, info.Size())
return nil
})
if err != nil {
log.Println(err)
}
}
func NewLoadGenerator(cfg *common.TraceGeneratorConfig) *XrayTracesGenerator {
listDir()
s, err := sampling.NewLocalizedStrategyFromFilePath(
path.Join("resources", "sampling-rule.json"))
if err != nil {
Expand Down
16 changes: 11 additions & 5 deletions util/common/agent_util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/aws/amazon-cloudwatch-agent-test/environment"
)
Expand Down Expand Up @@ -166,16 +166,22 @@ func StopAgent() {
log.Printf("Agent is stopped")
}

func ReadAgentOutput(d time.Duration) string {
func ReadAgentLogfile(logfile string) string {
out, err := os.ReadFile(logfile)
if err != nil {
log.Fatal(fmt.Sprint(err) + string(out))
}
return string(out)
}

func RecreateAgentLogfile(logfile string) {
out, err := exec.Command("bash", "-c",
fmt.Sprintf("sudo journalctl -u amazon-cloudwatch-agent.service --since \"%s ago\" --no-pager -q", d.String())).
fmt.Sprintf("sudo rm %s", logfile)).
Output()

if err != nil {
log.Fatal(fmt.Sprint(err) + string(out))
}

return string(out)
}

func RunShellScript(path string, args ...string) (string, error) {
Expand Down
2 changes: 2 additions & 0 deletions util/common/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const (
AGENT_SHUTDOWN_DELAY = 20 * time.Second // this const is the delay between stopping trace generation and stopping agent
)

type TraceTestConfig struct {
Generator TraceGeneratorInterface
Name string
Expand Down Expand Up @@ -49,6 +50,7 @@ type TraceGeneratorInterface interface {
GetAgentRuntime() time.Duration
GetName() string
}

func TraceTest(t *testing.T, traceTest TraceTestConfig) error {
t.Helper()
startTime := time.Now()
Expand Down

0 comments on commit a9f3137

Please sign in to comment.