Sign inSign up

viktoruj/ping_pong

By viktoruj

Updated 2 months ago

This is a small HTTP server that sends back the incoming request with all the metadata in response

Image
Developer tools
Operating systems
Monitoring & observability
0

10K+

viktoruj/ping_pong repository overview

ping_pong

SRE Learning Platform

This project is a small HTTP server that returns the incoming request with metadata in the response, including the sender's IP address, all request parameters, and headers. The server's configuration can be modified using environment variables. Additionally, the server supports CPU and memory load testing and provides metrics in Prometheus format. The running server's parameters can be modified via API.

The server can be run as a Docker container or Kubernetes resources for x86 or ARM platforms. It can also be run as compiled binary files or compiled from source code.

Table of Contents

Usage Scenarios

run http ping-pong server
kubectl  run test  --image  viktoruj/ping_pong
run http ping-pong server with custom server Name
kubectl  run test  --image  viktoruj/ping_pong  --env SERVER_NAME=pingPongServer
run http ping-pong server with serverName = podName
kubectl  run test  --image  viktoruj/ping_pong --env ENABLE_DEFAULT_HOSTNAME=false
run http ping-pong server with MEMORY_USAGE
kubectl  run test --image  viktoruj/ping_pong   --env MEMORY_USAGE_PROFILE='5=60  20=60   1024=360    ' --env ENABLE_LOAD_MEMORY=true
run http ping-pong server with MEMORY_USAGE and CPU_USAGE and enable cpu and memory loging . It uses CPU_MAXPROC =2 .
kubectl  run test  --image  viktoruj/ping_pong  --env CPU_USAGE_PROFILE='1=400=1=60 1=4=1=60 10=1=4=60'  --env ENABLE_LOAD_CPU=true  --env ENABLE_LOG_LOAD_CPU=true --env CPU_MAXPROC=2 --env MEMORY_USAGE_PROFILE='5=60  20=60   1024=360  ' --env ENABLE_LOAD_MEMORY=true --env ENABLE_LOG_LOAD_MEMORY=true
run http ping-pong server with slow start 10 seconds.
kubectl  run test  --image  viktoruj/ping_pong  --env DELAY_START=10
run http ping-pong server with response delay 3 seconds and 10 workers.
kubectl  run test  --image  viktoruj/ping_pong --env  RESPONSE_DELAY=3000 --env  MAX_RESPONSE_WORKER=10
Configuration
ENV VARAPP VARset via APIDescription
SERVER_NAMEserverNameYesThe name of the server. Default is ping_pong_server.
ENABLE_DEFAULT_HOSTNAMEenableDefaultHostNameYesUse the default hostname (default = "true").
SRV_PORTserverPortNoThe port on which the server is running.
METRIC_PORTmetricPortNoThe port where server metrics are available in Prometheus format.
You can view the current metrics at {server_address}:{METRIC_PORT}/metrics.
ENABLE_OUTPUTenableOutputYesLog to stdout (default = "true").
LOG_PATHlogPathYesWrite logs to file ${LOG_PATH} (default="" without file output).
ENABLE_LOAD_MEMORYenableLoadMemoryYesEnable memory usage (default=false).
MEMORY_USAGE_PROFILEmemoryProfileStrYesAdditional memory usage and time for usage. Example: 5=10 7=60 1024=360.
ENABLE_LOG_LOAD_MEMORYenableLogLoadMemoryYesLogs LOAD_MEMORY function.
ENABLE_LOAD_CPUenableLoadCpuYesEnable CPU usage (default=false).
ENABLE_LOG_LOAD_CPUenableLogLoadCpuYesLogs LOAD_CPU function.
CPU_USAGE_PROFILEcpuProfileStrYesAdditional CPU usage iteraction_milion=wait_msec=gorutins=time_sec. Example: 10=1=1=30 1=400=1=60.
CPU_MAXPROCcpuMaxProcYesGOMAXPROCS (default = 1). How many cores to use.
DELAY_STARTparsedDelayNoDelay start in seconds before start app (default = 0). Useful for testing slow start of app and test startup probes.
RESPONSE_DELAYresponseDelayYesResponse delay in milliseconds.
MAX_RESPONSE_WORKERmaxResponseWorkerYesMaximum number of response workers.
run Docker container
docker run -d -p 8080:8080 viktoruj/ping_pong
tags :

The images work on x86 and ARM platforms.

binary
run binary on macOS
sudo xattr -d com.apple.quarantine ping-pong-darwin-amd64
sudo chmod +x ping-pong-darwin-amd64
./ping-pong-darwin-amd64 
# or
SERVER_NAME=myCustomServer ./ping-pong-linux-amd64

# check server 
curl 127.0.0.1:8080

run binary on linux
sudo chmod +x ping-pong-linux-amd64
./ping-pong-linux-amd64
# or
SERVER_NAME=myCustomServer ./ping-pong-linux-amd64

# check server 
curl 127.0.0.1:8080

run binary on android
  • use Termux
copy ping-pong-android-arm64  to /data/data/com.termux/files/usr/bin/
chmod +x /data/data/com.termux/files/usr/bin/ping-pong-android-arm64
ping-pong-android-arm64 
Compilation from Source Code
cd docker/ping_pong/app
go mod tidy
go run app.go # or go build app.go

api
  • /ping-pong-api/getVar - get all ping-pong variables
  • /ping-pong-api/setVar - set ping-pong variables
  • /ping-pong-api/osInfo - get os info
  • /ping-pong-api/panic - emulate crash app
  • /ping-pong-api/getMetric - get metric
api examples
get variables
curl {ping-pong ip}:8080/ping-pong-api/getVar  -s | jq 
{
  "cpuMaxProc": 2,
  "cpuProfileStr": "1000=1=1=30",
  "delayStart": 0,
  "enableDefaultHostName": "true",
  "enableLoadCpu": "true",
  "enableLoadMemory": "false",
  "enableLogLoadCpu": "true",
  "enableLogLoadMemory": "false",
  "enableOutput": "true",
  "hostName": "ping_pong_server",
  "logPath": "",
  "memoryProfiles": null,
  "serverName": "ping_pong_server"
}

set variables
curl -X POST "http://{ping-pong ip}:8080/ping-pong-api/setVar" -H "Content-Type: application/json" -d '{"enableLoadCpu":"true", "cpuProfileStr":"1000=1=1=30","cpuMaxProc":2}' -s | jq
{
  "changes": {
    "cpuMaxProc": {
      "new": "2",
      "old": "1"
    },
    "cpuProfileStr": {
      "new": "1000=1=1=30",
      "old": "1000=1=2=30"
    },
    "enableLoadCpu": {
      "new": "true",
      "old": "false"
    }
  },
  "status": "Variables updated. Handlers reloaded."
}

get os info
curl {ping-pong ip}:8080/ping-pong-api/osInfo  -s | jq

{
  "architecture": {
    "architecture": "amd64",
    "goamd64": "v4",
    "goarm": ""
  },
  "cpu": {
    "cores": [
      {
        "core": 1,
        "unit": "percentage",
        "usage": 13.7
      },
      {
        "core": 2,
        "unit": "percentage",
        "usage": 7.6
      },

.............

get metric
curl {ping-pong ip}:8080/ping-pong-api/getMetric  -s | jq

{
  "goroutines": 6,
  "requests_per_minute": 0,
  "requests_per_second": 0,
  "requests_total": 0
}


emulate crash app
curl {ping-pong ip}:8080/ping-pong-api/panic  -s 

# in app log

Panic occurred: Test panic
exit status 1
 

Tag summary

Content type

Image

Digest

sha256:32d367cdf

Size

5.2 MB

Last updated

2 months ago

docker pull viktoruj/ping_pong