Skip to content

Commit

Permalink
Merge pull request #5 from okankoAMZ/traces-performance-test
Browse files Browse the repository at this point in the history
Traces performance test
  • Loading branch information
okankoAMZ authored Aug 14, 2023
2 parents a847e06 + a9f3137 commit c25c1a2
Show file tree
Hide file tree
Showing 25 changed files with 718 additions and 10 deletions.
11 changes: 6 additions & 5 deletions generator/test_case_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ var testTypeToTestConfig = map[string][]testConfig{
//{testDir: "../../../test/assume_role"},
},
"ec2_performance": {
{testDir: "../../test/performance/emf"},
{testDir: "../../test/performance/logs"},
{testDir: "../../test/performance/system"},
{testDir: "../../test/performance/statsd"},
{testDir: "../../test/performance/collectd"},
// {testDir: "../../test/performance/emf"},
// {testDir: "../../test/performance/logs"},
// {testDir: "../../test/performance/system"},
// {testDir: "../../test/performance/statsd"},
// {testDir: "../../test/performance/collectd"},
{testDir: "../../test/performance/trace/xray"},
},
"ec2_windows_performance": {
{testDir: "../../test/performance/windows/logs"},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/aws/aws-xray-sdk-go v1.8.1
github.com/cenkalti/backoff/v4 v4.2.1
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/mitchellh/mapstructure v1.5.0
github.com/prozz/aws-embedded-metrics-golang v1.2.0
github.com/qri-io/jsonschema v0.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4Zs
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down
23 changes: 23 additions & 0 deletions mock_server/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Header above mocked_servers cert in bundle
CERT_HEADER=mocked_servers

CONFIG_PATH=openssl.conf
CERT_PATH=certificates/ssl/certificate.crt
KEY_PATH=certificates/private.key
BUNDLE_PATH=certificates/ssl/ca-bundle.crt

.PHONY: update-certs
update-certs: gen-cert update-bundle

# Expects mocked_servers cert to be the last cert in the bundle
# Cuts until the first instance of "mocked_servers" in the bundle
# and concatenates it with the current cert
.PHONY: update-bundle
update-bundle:
sed /$(CERT_HEADER)/q $(BUNDLE_PATH) | cat - $(CERT_PATH) > $(BUNDLE_PATH).tmp && \
mv $(BUNDLE_PATH).tmp $(BUNDLE_PATH)

# Generates the annual cert and private key using the config
.PHONY: gen-cert
gen-cert:
openssl req -config $(CONFIG_PATH) -new -x509 -nodes -days 365 -out $(CERT_PATH) -keyout $(KEY_PATH)
8 changes: 8 additions & 0 deletions mock_server/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Mocked Servers

Most of the tests use mocked servers as stand-ins for the exporters to send to.
The docker images are built and pushed to ECR as part of `terraform/imagebuild` and then used in the tests as part of `defaults/docker_compose.tpl`.

Most will use the `https` mocked_server, but some test cases like `otlp_grpc_exporter_metric_mock` and `otlp_grpc_exporter_trace_mock` will need to use the `grpc_metrics` and `grpc_trace` mocked_servers.

The servers themselves are very basic. They listen for a message and tend to set a flag to success upon receiving it. Typically, they are set up with two servers: one to mock the backend and one to report on the status of that mock (if endpoint is called, how many times, etc.). There's a built-in 15ms of latency between each message that's received. The status reporter will always be on port 8080 and support the `:8080/` (returns "healthcheck") and the `:8080/check-data` (returns "success" if mock has received messages) endpoints.
52 changes: 52 additions & 0 deletions mock_server/certificates/private.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCUVAB1VBcztRsu
D0Kr+OVwEiN5HfinLUwh+SqX1fjPuSgU5ISfzYAy4aQ20H3rpee7y0DjNyDBKnTH
yxHcN/RNoRsM0D96QuRYx9+P5wCUUNchK7Pku/lFsJiO0TtuCxA5JqrlQRx7WHnu
eCiC+vneaL/Nj65EhPC/jiMuMh/VTRGnXa/Cf9XWU8AjYGuuLy6n8ari9s1BbNRM
FxWDJHWrrfCEiYf0bJX1bykABMaW1zE3Ql1ophxdL2qy4cXtEQwG3XswwV7f0tUh
unvpTymH69p0Cicz137vxnt03okwob1hz2DbMOX9YHC2tyNCuLBC05VSlaBuvQeN
sh2YPXq1LdSUF5S3XZa+JotolXcCTQ5oxvrqqjQATPHGQrnkn/tfzMs1S5crvzie
17BBGGAGbMOE6Guj+MuPxzrvvx6ZtXL8jFWlgQTLcLTf+haY7mIez+1VXHjPYEtt
wEweMK+z+qVX2YgEBl4mC5+hfvww57HPx7+6Tf3gqcRRKjeUDGN4XhnbskXyCIsj
6LJCLTAaVULXgcSZc1E65EAGgWxeYTpl8SM7/QAq6jC+FcWTIIcIkg4sPILOlFyj
ZhOuIg+tPhIusUQFEtiHEtddYorJPG0bZnTmeZfjva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C/7VM3
D5ECggEAZGHdPoHrZux7FP7aeCV3RkFyYySg3WWwpnch16RX821iIk0oZlHMXD9l
BmalEvo2Xia8cdTFjVi+FmagktmwGlPLsTXHIFEU+kHV8QG7t6iOB++gbN3z2qo3
SQ8f90gBOUl0J30eObVaE1tpOO1flMk/VibQ9VWrpNX2v6/khbSUrXI5gVjYvMa+
YmGOcZmo8+f9Q409DQ+LTaHH91lYt2sp1Rjk6J7fADVKT5RXWxZKW+HZ1p7Omyz6
AVyUAyScT9mjLz7faAGEA+e9ThrIfd/N0ohAhKMGnvPMpYvw6c8NP8AGFvFAt2kt
0rI6iVINGaTTRWLvOZOnOrZ6RFXOHg==
-----END PRIVATE KEY-----
36 changes: 36 additions & 0 deletions mock_server/certificates/ssl/ca-bundle.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-----BEGIN CERTIFICATE-----
MIIGOTCCBCGgAwIBAgIUcz7fCLPSodq6qjL3Czp30dMvib4wDQYJKoZIhvcNAQEL
BQAwgaAxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApXYXNoaW5ndG9uMRAwDgYDVQQH
DAdTZWF0dGxlMRgwFgYDVQQKDA9FeGFtcGxlIENvbXBhbnkxFjAUBgNVBAsMDUlU
IERlcGFydG1lbnQxIjAgBgkqhkiG9w0BCQEWE2V4YW1wbGVAZXhhbXBsZS5jb20x
FDASBgNVBAMMC2V4YW1wbGUuY29tMB4XDTIzMDgwOTE5MjUzNloXDTI0MDgwODE5
MjUzNlowgaAxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApXYXNoaW5ndG9uMRAwDgYD
VQQHDAdTZWF0dGxlMRgwFgYDVQQKDA9FeGFtcGxlIENvbXBhbnkxFjAUBgNVBAsM
DUlUIERlcGFydG1lbnQxIjAgBgkqhkiG9w0BCQEWE2V4YW1wbGVAZXhhbXBsZS5j
b20xFDASBgNVBAMMC2V4YW1wbGUuY29tMIICIjANBgkqhkiG9w0BAQEFAAOCAg8A
MIICCgKCAg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tlNDBg8gcpscnLq7ksQ4phZTNBPOE6pBzXEd9kS5ReCdkqqkafM2kqmJeB5nQ/Y
zyc8szw4+sJKf7Fszq6xW9AkzKZRhaNumWXTeTMALoq5/WfA4J4jcQ0BmVLPWA01
zYCdlNb0HHDKq/1ejWpfb3w/TWncdntqYndYRUR2Mr/1HNPSAiP6vaqFH6e/fzbE
G+b0+A8S5VEFzARO4RWwiXAxjhyWfPgC+TYPar5pmD7XGL6J1XepAvZGUsW0mqZD
9MdV62syt1kX2Ogok6Wz4dCkvzzy9N+oY+LvNhQfSr6ahDVZJ3up+oqzjhJr+Xf+
MSF6scI421mTP7mf3MEQDwISoTAtFOHvuHEHZnJws7pJfmPiiG0WdxclJ25RFya7
s+nwKPP+rz7AUrqkxc16nZlJLlfNqWRoFsrkNbg5AaLMAKzsdrWRBPkXig1siseD
teHsab59Of+lkNRo2g==
-----END CERTIFICATE-----
36 changes: 36 additions & 0 deletions mock_server/certificates/ssl/certificate.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-----BEGIN CERTIFICATE-----
MIIGOTCCBCGgAwIBAgIUcz7fCLPSodq6qjL3Czp30dMvib4wDQYJKoZIhvcNAQEL
BQAwgaAxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApXYXNoaW5ndG9uMRAwDgYDVQQH
DAdTZWF0dGxlMRgwFgYDVQQKDA9FeGFtcGxlIENvbXBhbnkxFjAUBgNVBAsMDUlU
IERlcGFydG1lbnQxIjAgBgkqhkiG9w0BCQEWE2V4YW1wbGVAZXhhbXBsZS5jb20x
FDASBgNVBAMMC2V4YW1wbGUuY29tMB4XDTIzMDgwOTE5MjUzNloXDTI0MDgwODE5
MjUzNlowgaAxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApXYXNoaW5ndG9uMRAwDgYD
VQQHDAdTZWF0dGxlMRgwFgYDVQQKDA9FeGFtcGxlIENvbXBhbnkxFjAUBgNVBAsM
DUlUIERlcGFydG1lbnQxIjAgBgkqhkiG9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z3ZoqM9prUHWXMZthG0CNLeMXkjs6OL3dw8twfT
LgGk+VfVam21emdI/NAdjwf3Bp3ANg1pQJSvxcq0HkFPvGe6BPLrlgrmqNfNdNli
BtlNDBg8gcpscnLq7ksQ4phZTNBPOE6pBzXEd9kS5ReCdkqqkafM2kqmJeB5nQ/Y
zyc8szw4+sJKf7Fszq6xW9AkzKZRhaNumWXTeTMALoq5/WfA4J4jcQ0BmVLPWA01
zYCdlNb0HHDKq/1ejWpfb3w/TWncdntqYndYRUR2Mr/1HNPSAiP6vaqFH6e/fzbE
G+b0+A8S5VEFzARO4RWwiXAxjhyWfPgC+TYPar5pmD7XGL6J1XepAvZGUsW0mqZD
9MdV62syt1kX2Ogok6Wz4dCkvzzy9N+oY+LvNhQfSr6ahDVZJ3up+oqzjhJr+Xf+
MSF6scI421mTP7mf3MEQDwISoTAtFOHvuHEHZnJws7pJfmPiiG0WdxclJ25RFya7
s+nwKPP+rz7AUrqkxc16nZlJLlfNqWRoFsrkNbg5AaLMAKzsdrWRBPkXig1siseD
teHsab59Of+lkNRo2g==
-----END CERTIFICATE-----
134 changes: 134 additions & 0 deletions mock_server/http_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2021 Amazon.com, Inc. or its affiliates
//
// 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 mockserver

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"sync/atomic"
"time"

"github.com/gorilla/mux"
)

const (
HealthCheckMessage = "healthcheck"
SuccessMessage = "success"
CertFilePath = "./certificates/ssl/certificate.crt"
KeyFilePath = "./certificates/private.key"
DaemonPort = ":1053"
)

type transactionStore struct {
transactions uint32
startTime time.Time
}

type TransactionPayload struct {
TransactionsPerMinute float64 `json:"tpm"`
}

func healthCheck(w http.ResponseWriter, _ *http.Request) {
if _, err := io.WriteString(w, HealthCheckMessage); err != nil {
log.Printf("Unable to write response: %v", err)
}
}

func (ts *transactionStore) checkData(w http.ResponseWriter, _ *http.Request) {
var message string
var t =atomic.LoadUint32(&ts.transactions)
if t > 0 {
message = SuccessMessage
}
fmt.Printf("\033[31m Time: %d | checkData msg: %s | %d\033[0m \n", time.Now().Unix(), message,t)
if _, err := io.WriteString(w, message); err != nil {
io.WriteString(w, err.Error())
log.Printf("Unable to write response: %v", err)
}
}

func (ts *transactionStore) dataReceived(w http.ResponseWriter, _ *http.Request) {
atomic.AddUint32(&ts.transactions, 1)

// Built-in latency
fmt.Printf("\033[31m Time: %d | data Received \033[0m \n", time.Now().Unix())
time.Sleep(15 * time.Millisecond)
w.WriteHeader(http.StatusOK)
}

// Retrieve number of transactions per minute
func (ts *transactionStore) tpm(w http.ResponseWriter, _ *http.Request) {
// Calculate duration in minutes
duration := time.Now().Sub(ts.startTime)
transactions := float64(atomic.LoadUint32(&ts.transactions))
tpm := transactions / duration.Minutes()

w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(TransactionPayload{tpm}); err != nil {
io.WriteString(w, err.Error())
log.Printf("Unable to write response: %v", err)
}
}

// Starts an HTTPS server that receives requests for the data handler service at the sample server port
// Starts an HTTP server that receives request from validator only to verify the data ingestion
func startHttpServer() chan interface{} {
serverControlChan := make(chan interface{})
log.Println("\033[31m Starting Server \033[0m")
store := transactionStore{startTime: time.Now()}
dataApp := mux.NewRouter()
daemonServer := &http.Server{Addr: ":443", Handler: dataApp}
verifyApp := http.NewServeMux()
appServer := &http.Server{Addr: ":8080", Handler: verifyApp}
go func(ts *transactionStore) {
defer close(serverControlChan)
dataApp.HandleFunc("/", healthCheck)
dataApp.PathPrefix("/put-data").HandlerFunc(ts.dataReceived)
dataApp.HandleFunc("/trace/v1", ts.dataReceived)
dataApp.HandleFunc("/metric/v1", ts.dataReceived)
if err := daemonServer.ListenAndServeTLS(CertFilePath, KeyFilePath ); err != nil {
log.Fatalf("HTTPS server error: %v", err)
err = daemonServer.Shutdown(context.TODO())
log.Fatalf("Shutdown server error: %v", err)
}
}(&store)

go func(ts *transactionStore) {
defer close(serverControlChan)
verifyApp.HandleFunc("/", healthCheck)
verifyApp.HandleFunc("/check-data", ts.checkData)
verifyApp.HandleFunc("/tpm", ts.tpm)
if err := appServer.ListenAndServe(); err != nil {
log.Fatalf("Verification server error: %v", err)
err := appServer.Shutdown(context.TODO())
log.Fatalf("Shuwdown server error: %v", err)
}
}(&store)
go func() {
for {
select {
case <-serverControlChan:
log.Println("\033[32m Stopping Server \033[0m")

}
}
}()

return serverControlChan
}
Loading

0 comments on commit c25c1a2

Please sign in to comment.