feat: added loadtest service

This commit is contained in:
ITQ
2025-07-25 00:59:54 +03:00
parent dd0568bf91
commit 3804495ce5
14 changed files with 34943 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
BACKEND_ADDRESS=http://backend:8080
+38
View File
@@ -0,0 +1,38 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Code coverage profiles and other test artifacts
*.out
coverage.*
*.coverprofile
profile.cov
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
go.work.sum
# env file
.env
# Editor/IDE
# .idea/
# .vscode/
# App binary
main
# Gitignore
.gitignore
+4
View File
@@ -0,0 +1,4 @@
# Change all vars before going to production and remove all comments (!)
# Below all environment variables and default values
BACKEND_ADDRESS=http://localhost:8080
+35
View File
@@ -0,0 +1,35 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Code coverage profiles and other test artifacts
*.out
coverage.*
*.coverprofile
profile.cov
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
go.work.sum
# env file
.env
# Editor/IDE
# .idea/
# .vscode/
# App binary
main
+28
View File
@@ -0,0 +1,28 @@
ARG GOARCH=amd64
# Stage 1: Build go binary
FROM docker.io/golang:1.24-alpine AS build
WORKDIR /build
COPY . .
RUN go mod download
ARG GOARCH
RUN CGO_ENABLED=0 GOARCH=$GOARCH go build -o loadtest
# Stage 2: Run go binary
FROM docker.io/alpine:3.22
WORKDIR /app
COPY --from=build /build/loadtest .
COPY . .
EXPOSE 5001
CMD [ "./loadtest", "--port", "5001" ]
+68
View File
@@ -0,0 +1,68 @@
# AdNova Loadtest
## Prerequisites
Ensure you have the following installed on your system:
- [Go](https://go.dev/) (1.24 recommended)
- [Docker](https://www.docker.com/) (for containerized setup, latest version recommended)
## Basic setup
### Installation
#### Clone the project
#### Go to the project directory
```bash
cd AdNova/services/loadtest
```
#### Customize environment
```bash
cp .env.template .env
```
And setup env vars according to your needs.
#### Install dependencies
```bash
go mod download
```
#### Running
```bash
go run main.go
```
## Containerized setup
### Clone the project
### Go to the project directory
```bash
cd AdNova/services/loadtest
```
### Build docker image
```bash
docker build -t adnova-loadtest .
```
### Customize environment
Customize environment with `docker run` command, for all environment vars and default values see [.env.template](./.env.template).
### Run docker image
```bash
docker run -p 5001:5001 --name adnova-loadtest adnova-loadtest
```
Loadtest will be available on [127.0.0.1:5001](http://127.0.0.1:5001).
+8
View File
@@ -0,0 +1,8 @@
module loadtest
go 1.24.5
require (
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.3
)
+4
View File
@@ -0,0 +1,4 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+788
View File
@@ -0,0 +1,788 @@
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"log"
"math/rand"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
// --- Mock Data Structures ---
type Client struct {
ClientID string `json:"client_id"`
Login string `json:"login"`
Age int `json:"age"`
Location string `json:"location"`
Gender string `json:"gender"`
}
type Advertiser struct {
AdvertiserID string `json:"advertiser_id"`
Name string `json:"name"`
}
type MLScore struct {
AdvertiserId string `json:"advertiser_id"`
ClientID string `json:"client_id"`
Score int64 `json:"score"`
}
type Campaign struct {
Targeting struct {
Gender string `json:"gender,omitempty"`
AgeFrom int `json:"age_from,omitempty"`
AgeTo int `json:"age_to,omitempty"`
Location string `json:"location,omitempty"`
} `json:"targeting"`
AdTitle string `json:"ad_title"`
AdText string `json:"ad_text"`
ImpressionsLimit int `json:"impressions_limit"`
ClicksLimit int `json:"clicks_limit"`
CostPerImpression float64 `json:"cost_per_impression"`
CostPerClick float64 `json:"cost_per_click"`
StartDate int64 `json:"start_date"`
EndDate int64 `json:"end_date"`
}
type CampaignFileEntry struct {
AdvertiserID string `json:"advertiser_id"`
CampaignData Campaign `json:"campaign_data"`
}
// --- Mock data filenames ---
const clientsMockDataFile = "./mocks/bulk_clients.json"
const advertisersMockDataFile = "./mocks/bulk_advertisers.json"
const campaignsMockDataFile = "./mocks/campaigns.json"
const mlscoresMockDataFile = "./mocks/ml_scores.json"
// --- In-Memory Data Store ---
var (
loadedClientIDs []string
dataMutex = &sync.RWMutex{}
backendAddress string
)
// --- Mock Data Loading and Posting Logic ---
func loadInitialMockIDs() {
dataMutex.Lock()
defer dataMutex.Unlock()
loadedClientIDs = nil
log.Println("Loading client IDs from local files...")
readFile := func(filename string) ([]byte, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading %s: %w", filename, err)
}
return content, nil
}
clientsContent, err := readFile(clientsMockDataFile)
if err != nil {
log.Printf("Warning: Could not read %s for initial ID load: %v", clientsMockDataFile, err)
} else {
var tempClients []Client
if err := json.Unmarshal(clientsContent, &tempClients); err != nil {
log.Printf("Warning: Error unmarshaling %s for initial ID load: %v", clientsMockDataFile, err)
} else {
for _, c := range tempClients {
loadedClientIDs = append(loadedClientIDs, c.ClientID)
}
log.Printf("Loaded %d client IDs for stress testing.", len(loadedClientIDs))
}
}
}
func loadAndPostMocks() error {
log.Println("Attempting to load mock data from files and post to external backend:", backendAddress)
readFile := func(filename string) ([]byte, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading %s: %w", filename, err)
}
return content, nil
}
postJSON := func(url string, data interface{}) error {
jsonData, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("failed to marshal JSON for %s: %w", url, err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create request for %s: %w", url, err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: 60 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to POST to %s: %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("POST to %s failed with status %d: %s", url, resp.StatusCode, string(body))
}
return nil
}
// 1. Load bulk_clients.json
clientsContent, err := readFile(clientsMockDataFile)
if err != nil {
return err
}
var tempClients []Client
if err := json.Unmarshal(clientsContent, &tempClients); err != nil {
return fmt.Errorf("error unmarshaling %s: %w", clientsMockDataFile, err)
}
if err := postJSON(fmt.Sprintf("%s/clients/bulk", backendAddress), tempClients); err != nil {
return fmt.Errorf("error posting bulk clients: %w", err)
}
log.Printf("Successfully posted %d clients to %s/clients/bulk", len(tempClients), backendAddress)
// 2. Load bulk_advertisers.json
advertisersContent, err := readFile(advertisersMockDataFile)
if err != nil {
return err
}
var tempAdvertisers []Advertiser
if err := json.Unmarshal(advertisersContent, &tempAdvertisers); err != nil {
return fmt.Errorf("error unmarshaling %s: %w", advertisersMockDataFile, err)
}
if err := postJSON(fmt.Sprintf("%s/advertisers/bulk", backendAddress), tempAdvertisers); err != nil {
return fmt.Errorf("error posting bulk advertisers: %w", err)
}
log.Printf("Successfully posted %d advertisers to %s/advertisers/bulk", len(tempAdvertisers), backendAddress)
// 3. Load campaigns.json
campaignsContent, err := readFile(campaignsMockDataFile)
if err != nil {
return err
}
var campaignDataList []CampaignFileEntry
if err := json.Unmarshal(campaignsContent, &campaignDataList); err != nil {
return fmt.Errorf("error unmarshaling %s: %w", campaignsMockDataFile, err)
}
for i, entry := range campaignDataList {
if err := postJSON(fmt.Sprintf("%s/advertisers/%s/campaigns", backendAddress, entry.AdvertiserID), entry.CampaignData); err != nil {
log.Printf("Warning: Failed to post campaign for advertiser %s (entry %d): %v", entry.AdvertiserID, i, err)
}
}
log.Printf("Attempted to post campaigns for %d advertisers from %s", len(campaignDataList), campaignsMockDataFile)
// 4. Load ml_scores.json
mlScoresContent, err := readFile(mlscoresMockDataFile)
if err != nil {
return err
}
var tempMLScores []MLScore
if err := json.Unmarshal(mlScoresContent, &tempMLScores); err != nil {
return fmt.Errorf("error unmarshaling %s: %w", mlscoresMockDataFile, err)
}
for i, score := range tempMLScores {
if err := postJSON(fmt.Sprintf("%s/ml-scores", backendAddress), score); err != nil {
log.Printf("Warning: Failed to post ML score %d (client %s): %v", i, score.ClientID, err)
}
}
log.Printf("Attempted to post %d ML scores one by one to %s/ml-scores", len(tempMLScores), backendAddress)
log.Println("Mock data loading and posting process complete.")
return nil
}
// --- WebSocket Hub ---
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}
type Hub struct {
clients map[*websocket.Conn]bool
broadcast chan []byte
register chan *websocket.Conn
unregister chan *websocket.Conn
mutex sync.Mutex
}
func newHub() *Hub {
return &Hub{
broadcast: make(chan []byte),
register: make(chan *websocket.Conn),
unregister: make(chan *websocket.Conn),
clients: make(map[*websocket.Conn]bool),
}
}
func (h *Hub) run() {
for {
select {
case client := <-h.register:
h.mutex.Lock()
h.clients[client] = true
h.mutex.Unlock()
case client := <-h.unregister:
h.mutex.Lock()
if _, ok := h.clients[client]; ok {
delete(h.clients, client)
client.Close()
}
h.mutex.Unlock()
case message := <-h.broadcast:
h.mutex.Lock()
for client := range h.clients {
err := client.WriteMessage(websocket.TextMessage, message)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(h.clients, client)
}
}
h.mutex.Unlock()
}
}
}
var hub = newHub()
// --- Load Generator ---
type TestConfig struct {
BackendAddress string `json:"backendAddress"`
MaxRPS int `json:"maxRps"`
LoadProfile string `json:"loadProfile"`
FromRPS int `json:"fromRPS"`
ToRPS int `json:"toRPS"`
StepRPS int `json:"stepRps"`
StepDuration int `json:"stepDuration"`
OnceCount int `json:"onceCount"`
}
type RequestResult struct {
StatusCode int
Latency time.Duration
Error bool
}
type TestStats struct {
RPS int `json:"rps"`
Latency float64 `json:"latency"`
ErrorRate float64 `json:"errorRate"`
TotalReqs int64 `json:"totalReqs"`
TotalErrors int64 `json:"totalErrors"`
IsRunning bool `json:"isRunning"`
}
type TestManager struct {
config TestConfig
isRunning bool
cancelFunc context.CancelFunc
mutex sync.Mutex
httpClient *http.Client
}
var testManager = TestManager{
httpClient: &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 1000,
MaxIdleConnsPerHost: 1000,
IdleConnTimeout: 90 * time.Second,
},
},
}
func (tm *TestManager) startTest(config TestConfig) {
tm.mutex.Lock()
if tm.isRunning {
tm.mutex.Unlock()
log.Println("Test already running.")
return
}
tm.config = config
tm.isRunning = true
ctx, cancel := context.WithCancel(context.Background())
tm.cancelFunc = cancel
tm.mutex.Unlock()
go tm.runLoadGenerator(ctx)
}
func (tm *TestManager) stopTest() {
tm.mutex.Lock()
if tm.isRunning && tm.cancelFunc != nil {
tm.cancelFunc()
tm.isRunning = false
}
tm.mutex.Unlock()
}
func (tm *TestManager) runLoadGenerator(ctx context.Context) {
log.Printf("Starting test with config: %+v\n", tm.config)
results := make(chan RequestResult, 10000)
var wg sync.WaitGroup
var totalReqs, totalErrors int64
go func() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
var reqsInSecond int
var totalLatency time.Duration
var errorsInSecond int
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
avgLatency := 0.0
if reqsInSecond > 0 {
avgLatency = float64(totalLatency.Milliseconds()) / float64(reqsInSecond)
}
errorRate := 0.0
if reqsInSecond > 0 {
errorRate = float64(errorsInSecond) / float64(reqsInSecond) * 100
}
stats := TestStats{
RPS: reqsInSecond,
Latency: avgLatency,
ErrorRate: errorRate,
TotalReqs: totalReqs,
TotalErrors: totalErrors,
IsRunning: true,
}
jsonStats, _ := json.Marshal(stats)
hub.broadcast <- jsonStats
reqsInSecond = 0
totalLatency = 0
errorsInSecond = 0
case res, ok := <-results:
if !ok {
return
}
totalReqs++
reqsInSecond++
totalLatency += res.Latency
if res.Error {
totalErrors++
errorsInSecond++
}
}
}
}()
numWorkers := 1000
if tm.config.MaxRPS > 0 && tm.config.MaxRPS < numWorkers {
numWorkers = tm.config.MaxRPS
}
jobs := make(chan string, 10000)
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case url, ok := <-jobs:
if !ok {
return
}
start := time.Now()
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
resp, err := tm.httpClient.Do(req)
latency := time.Since(start)
if err != nil {
results <- RequestResult{StatusCode: 0, Latency: latency, Error: true}
continue
}
results <- RequestResult{StatusCode: resp.StatusCode, Latency: latency, Error: resp.StatusCode >= 500}
resp.Body.Close()
}
}
}()
}
go func() {
defer close(jobs)
dataMutex.RLock()
if len(loadedClientIDs) == 0 {
log.Println("No client IDs loaded for stress test.")
dataMutex.RUnlock()
return
}
clientIDsForTest := make([]string, len(loadedClientIDs))
copy(clientIDsForTest, loadedClientIDs)
dataMutex.RUnlock()
log.Printf("Stress testing with %d client IDs.", len(clientIDsForTest))
switch tm.config.LoadProfile {
case "const":
ticker := time.NewTicker(time.Second / time.Duration(tm.config.MaxRPS))
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
clientID := clientIDsForTest[rand.Intn(len(clientIDsForTest))]
url := fmt.Sprintf("%s/ads?client_id=%s", tm.config.BackendAddress, clientID)
jobs <- url
}
}
case "line":
duration := 10 * time.Second
startTime := time.Now()
for {
select {
case <-ctx.Done():
return
default:
elapsed := time.Since(startTime)
if elapsed >= duration {
elapsed = duration
}
progress := float64(elapsed) / float64(duration)
currentRPS := float64(tm.config.FromRPS) + (float64(tm.config.ToRPS-tm.config.FromRPS) * progress)
if currentRPS <= 0 {
currentRPS = 1
}
sleepDuration := time.Second / time.Duration(currentRPS)
time.Sleep(sleepDuration)
clientID := clientIDsForTest[rand.Intn(len(clientIDsForTest))]
url := fmt.Sprintf("%s/ads?client_id=%s", tm.config.BackendAddress, clientID)
jobs <- url
if elapsed >= duration {
constSleepDuration := time.Second / time.Duration(tm.config.ToRPS)
for {
select {
case <-ctx.Done():
return
default:
time.Sleep(constSleepDuration)
clientID := clientIDsForTest[rand.Intn(len(clientIDsForTest))]
url := fmt.Sprintf("%s/ads?client_id=%s", tm.config.BackendAddress, clientID)
jobs <- url
}
}
}
}
}
case "step":
currentRPS := tm.config.FromRPS
for currentRPS <= tm.config.ToRPS {
log.Printf("Step load: %d RPS for %d seconds", currentRPS, tm.config.StepDuration)
stepEndTime := time.After(time.Duration(tm.config.StepDuration) * time.Second)
sleepDuration := time.Second / time.Duration(currentRPS)
stepLoop:
for {
select {
case <-ctx.Done():
return
case <-stepEndTime:
break stepLoop
default:
time.Sleep(sleepDuration)
clientID := clientIDsForTest[rand.Intn(len(clientIDsForTest))]
url := fmt.Sprintf("%s/ads?client_id=%s", tm.config.BackendAddress, clientID)
jobs <- url
}
}
currentRPS += tm.config.StepRPS
if tm.config.StepRPS == 0 && currentRPS != tm.config.ToRPS {
break
}
}
case "once":
for i := 0; i < tm.config.OnceCount; i++ {
select {
case <-ctx.Done():
return
default:
clientID := clientIDsForTest[rand.Intn(len(clientIDsForTest))]
url := fmt.Sprintf("%s/ads?client_id=%s", tm.config.BackendAddress, clientID)
jobs <- url
}
}
case "unlimited":
for {
select {
case <-ctx.Done():
return
default:
clientID := clientIDsForTest[rand.Intn(len(clientIDsForTest))]
url := fmt.Sprintf("%s/ads?client_id=%s", tm.config.BackendAddress, clientID)
jobs <- url
}
}
}
}()
wg.Wait()
close(results)
time.Sleep(1 * time.Second)
tm.mutex.Lock()
tm.isRunning = false
tm.mutex.Unlock()
finalStats := TestStats{IsRunning: false, TotalReqs: totalReqs, TotalErrors: totalErrors}
jsonStats, _ := json.Marshal(finalStats)
hub.broadcast <- jsonStats
log.Println("Test finished.")
}
// --- HTTP Handlers ---
func serveWs(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
hub.register <- conn
go func() {
defer func() {
hub.unregister <- conn
conn.Close()
}()
for {
if _, _, err := conn.NextReader(); err != nil {
break
}
}
}()
}
func handleLoadMocks(w http.ResponseWriter, r *http.Request) {
if err := loadAndPostMocks(); err != nil {
http.Error(w, fmt.Sprintf("Failed to load mocks: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"status": "Mocks loaded successfully"})
}
func checkEndpoint(url string) bool {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("Error creating request for %s: %v", url, err)
return false
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("Error making request to %s: %v", url, err)
return false
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Check for %s failed with status: %d", url, resp.StatusCode)
return false
}
if strings.Contains(url, "/campaigns") {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading response body from %s: %v", url, err)
return false
}
var campaignsData []Campaign
if err := json.Unmarshal(bodyBytes, &campaignsData); err != nil {
log.Printf("Error unmarshaling campaign data from %s: %v", url, err)
return false
}
return len(campaignsData) > 0
}
return true
}
func handleCheckMocks(w http.ResponseWriter, r *http.Request) {
dataMutex.RLock()
defer dataMutex.RUnlock()
status := make(map[string]bool)
var tempClients []Client
clientsContent, err := os.ReadFile(clientsMockDataFile)
if err != nil {
log.Printf("Warning: Could not read %s for checks: %v", clientsMockDataFile, err)
} else {
json.Unmarshal(clientsContent, &tempClients)
}
var tempAdvertisers []Advertiser
advertisersContent, err := os.ReadFile(advertisersMockDataFile)
if err != nil {
log.Printf("Warning: Could not read %s for checks: %v", advertisersMockDataFile, err)
} else {
json.Unmarshal(advertisersContent, &tempAdvertisers)
}
clientsLoaded := false
if len(tempClients) >= 3 {
firstClient := tempClients[0].ClientID
medianClient := tempClients[len(tempClients)/2].ClientID
lastClient := tempClients[len(tempClients)-1].ClientID
clientsLoaded = checkEndpoint(fmt.Sprintf("%s/clients/%s", backendAddress, firstClient)) &&
checkEndpoint(fmt.Sprintf("%s/clients/%s", backendAddress, medianClient)) &&
checkEndpoint(fmt.Sprintf("%s/clients/%s", backendAddress, lastClient))
} else if len(tempClients) > 0 {
clientsLoaded = checkEndpoint(fmt.Sprintf("%s/clients/%s", backendAddress, tempClients[0].ClientID))
}
status["clients"] = clientsLoaded
advertisersLoaded := false
if len(tempAdvertisers) >= 3 {
firstAdvertiser := tempAdvertisers[0].AdvertiserID
medianAdvertiser := tempAdvertisers[len(tempAdvertisers)/2].AdvertiserID
lastAdvertiser := tempAdvertisers[len(tempAdvertisers)-1].AdvertiserID
advertisersLoaded = checkEndpoint(fmt.Sprintf("%s/advertisers/%s", backendAddress, firstAdvertiser)) &&
checkEndpoint(fmt.Sprintf("%s/advertisers/%s", backendAddress, medianAdvertiser)) &&
checkEndpoint(fmt.Sprintf("%s/advertisers/%s", backendAddress, lastAdvertiser))
} else if len(tempAdvertisers) > 0 {
advertisersLoaded = checkEndpoint(fmt.Sprintf("%s/advertisers/%s", backendAddress, tempAdvertisers[0].AdvertiserID))
}
status["advertisers"] = advertisersLoaded
campaignsLoaded := false
if len(tempAdvertisers) >= 3 {
firstAdvertiser := tempAdvertisers[0].AdvertiserID
medianAdvertiser := tempAdvertisers[len(tempAdvertisers)/2].AdvertiserID
lastAdvertiser := tempAdvertisers[len(tempAdvertisers)-1].AdvertiserID
campaignsLoaded = checkEndpoint(fmt.Sprintf("%s/advertisers/%s/campaigns", backendAddress, firstAdvertiser)) &&
checkEndpoint(fmt.Sprintf("%s/advertisers/%s/campaigns", backendAddress, medianAdvertiser)) &&
checkEndpoint(fmt.Sprintf("%s/advertisers/%s/campaigns", backendAddress, lastAdvertiser))
} else if len(tempAdvertisers) > 0 {
campaignsLoaded = checkEndpoint(fmt.Sprintf("%s/advertisers/%s/campaigns", backendAddress, tempAdvertisers[0].AdvertiserID))
}
status["campaigns"] = campaignsLoaded
_, err = os.ReadFile(mlscoresMockDataFile)
status["ml_scores"] = err == nil
if !status["ml_scores"] {
log.Printf("Warning: ML Scores file not found or readable, assuming not loaded for check.")
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(status)
}
func handleStartTest(w http.ResponseWriter, r *http.Request) {
var config TestConfig
if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
config.BackendAddress = backendAddress
go testManager.startTest(config)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"status": "Test started"})
}
func handleStopTest(w http.ResponseWriter, r *http.Request) {
testManager.stopTest()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"status": "Test stopped"})
}
func main() {
rand.Seed(time.Now().UnixNano())
port := flag.String("port", "5002", "Port to run the server on")
flag.Parse()
backendAddress = os.Getenv("BACKEND_ADDRESS")
if backendAddress == "" {
log.Println("BACKEND_ADDRESS environment variable not set. Defaulting to http://localhost:8080")
backendAddress = "http://localhost:8080"
}
loadInitialMockIDs()
go hub.run()
r := mux.NewRouter()
r.HandleFunc("/", serveUI)
r.HandleFunc("/ws", serveWs)
r.HandleFunc("/api/load-mocks", handleLoadMocks).Methods("POST")
r.HandleFunc("/api/check-mocks", handleCheckMocks).Methods("GET")
r.HandleFunc("/api/start-test", handleStartTest).Methods("POST")
r.HandleFunc("/api/stop-test", handleStopTest).Methods("POST")
addr := ":" + *port
log.Printf("Server starting on port %s. Open http://localhost%s\n", *port, addr)
if err := http.ListenAndServe(addr, r); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// --- Embedded HTML UI ---
func serveUI(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
t, err := template.ParseFiles("static/index.html")
if err != nil {
http.Error(w, "Could not load UI template", http.StatusInternalServerError)
return
}
err = t.Execute(w, nil)
if err != nil {
http.Error(w, "Failed to render UI", http.StatusInternalServerError)
}
}
@@ -0,0 +1,122 @@
[
{
"advertiser_id": "addf3407-9265-44b8-8e7e-6b015f63c47d",
"name": "Smart Inc"
},
{
"advertiser_id": "4d830921-f650-4f2f-869b-db48208dc952",
"name": "Advanced Technologies"
},
{
"advertiser_id": "752ddcd3-e944-4eaf-b1de-5e49977354a7",
"name": "Global Group"
},
{
"advertiser_id": "e04d35cd-e24b-4bfd-b5fa-6ac1c12ae8a0",
"name": "Global Media"
},
{
"advertiser_id": "8b7f547f-bab5-448d-9997-61cbbd6e3f1c",
"name": "Modern Systems"
},
{
"advertiser_id": "57dd000f-32fe-4a49-8dca-bfdc84fec7dd",
"name": "Advanced Technologies"
},
{
"advertiser_id": "22d28b06-32c5-434e-b05e-0654fa1cac09",
"name": "Global Corp"
},
{
"advertiser_id": "7ba54148-4885-48da-97ba-6ecf7b3a300b",
"name": "Tech Solutions"
},
{
"advertiser_id": "00ac5657-1a1a-48f1-815a-2ffdb72b82b8",
"name": "Elite Group"
},
{
"advertiser_id": "b9d5a8ce-ae23-4a93-946e-4bd85bddef5c",
"name": "Modern Marketing"
},
{
"advertiser_id": "3696b94d-6591-4e15-86f3-8c46ee535e27",
"name": "Advanced Systems"
},
{
"advertiser_id": "20024aab-2476-4d35-b96f-f47589f05474",
"name": "Premier Technologies"
},
{
"advertiser_id": "a68ef7d6-c50f-40d1-b70f-a32d7cdcd036",
"name": "Elite Partners"
},
{
"advertiser_id": "960e2896-3f34-43a3-8aa1-d984bb92b96e",
"name": "Future Inc"
},
{
"advertiser_id": "80a7f6a9-5939-4d74-9a7e-e455b9fa0fdd",
"name": "Modern Marketing"
},
{
"advertiser_id": "6bb59511-360d-49dc-9626-be2f6b9b3a0a",
"name": "Future Solutions"
},
{
"advertiser_id": "d7a27dc9-7d8d-4f25-bea4-2528f88238d1",
"name": "Advanced Solutions"
},
{
"advertiser_id": "e6cde283-ca4b-456c-b1fb-c2967918b5ef",
"name": "Digital Solutions"
},
{
"advertiser_id": "2a51ba38-bed3-43af-bbb4-99093b72e0d1",
"name": "Tech Group"
},
{
"advertiser_id": "95a9a488-8fee-4aa6-9c21-458f8f5523ca",
"name": "Elite Systems"
},
{
"advertiser_id": "2fb992b8-4441-47f5-9176-1da68526eafb",
"name": "Tech Inc"
},
{
"advertiser_id": "f0d160bd-18b4-4c88-917c-06b699abeeec",
"name": "Global Inc"
},
{
"advertiser_id": "d8147d8c-2bcd-4ac8-bab5-82e420509a7f",
"name": "Smart Inc"
},
{
"advertiser_id": "94209720-2c10-4a55-bc37-c28838d08012",
"name": "Advanced Corp"
},
{
"advertiser_id": "33fae4ce-d252-4b34-af2b-c11e770f65b5",
"name": "Smart Advertising"
},
{
"advertiser_id": "2c0261ba-bd3c-4b19-a53c-44efa10819e5",
"name": "Digital Marketing"
},
{
"advertiser_id": "0892fbb7-fbc6-45e9-b52f-494258a9c8f8",
"name": "Smart Group"
},
{
"advertiser_id": "055af498-567a-4a4e-b25f-60a57b9b591c",
"name": "Advanced Systems"
},
{
"advertiser_id": "0dfd7246-4cfa-4721-899b-e6dd9c483626",
"name": "Premier Advertising"
},
{
"advertiser_id": "32d75bc6-0779-4081-98b3-58273497c561",
"name": "Modern Partners"
}
]
File diff suppressed because it is too large Load Diff
+572
View File
@@ -0,0 +1,572 @@
[
{
"advertiser_id": "addf3407-9265-44b8-8e7e-6b015f63c47d",
"campaign_data": {
"impressions_limit": 8,
"clicks_limit": 8,
"cost_per_impression": 1.5,
"cost_per_click": 2.5,
"ad_title": "Amazing Offer",
"ad_text": "Get great savings now!",
"start_date": 0,
"end_date": 15,
"targeting": {
"gender": "MALE",
"age_from": 25,
"age_to": 40,
"location": "Chicago"
}
}
},
{
"advertiser_id": "4d830921-f650-4f2f-869b-db48208dc952",
"campaign_data": {
"impressions_limit": 10,
"clicks_limit": 10,
"cost_per_impression": 1.2,
"cost_per_click": 2.8,
"ad_title": "Incredible Deal",
"ad_text": "Discover amazing quality!",
"start_date": 0,
"end_date": 20,
"targeting": {
"gender": "FEMALE",
"age_from": 30,
"age_to": 50,
"location": "New York"
}
}
},
{
"advertiser_id": "752ddcd3-e944-4eaf-b1de-5e49977354a7",
"campaign_data": {
"impressions_limit": 6,
"clicks_limit": 6,
"cost_per_impression": 1.8,
"cost_per_click": 2.2,
"ad_title": "Best Service",
"ad_text": "Buy unique advantages today!",
"start_date": 0,
"end_date": 22,
"targeting": {
"gender": "ALL",
"age_from": 18,
"age_to": 60,
"location": "Los Angeles"
}
}
},
{
"advertiser_id": "e04d35cd-e24b-4bfd-b5fa-6ac1c12ae8a0",
"campaign_data": {
"impressions_limit": 7,
"clicks_limit": 7,
"cost_per_impression": 1.1,
"cost_per_click": 2.9,
"ad_title": "Exclusive Opportunity",
"ad_text": "Try our new product now!",
"start_date": 0,
"end_date": 13,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "8b7f547f-bab5-448d-9997-61cbbd6e3f1c",
"campaign_data": {
"impressions_limit": 9,
"clicks_limit": 9,
"cost_per_impression": 1.6,
"cost_per_click": 2.4,
"ad_title": "Special Features",
"ad_text": "Experience our service today!",
"start_date": 0,
"end_date": 11,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "57dd000f-32fe-4a49-8dca-bfdc84fec7dd",
"campaign_data": {
"impressions_limit": 5,
"clicks_limit": 5,
"cost_per_impression": 1.3,
"cost_per_click": 2.1,
"ad_title": "Limited Time Offer",
"ad_text": "Get it before it's gone!",
"start_date": 0,
"end_date": 12,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "22d28b06-32c5-434e-b05e-0654fa1cac09",
"campaign_data": {
"impressions_limit": 6,
"clicks_limit": 6,
"cost_per_impression": 1.7,
"cost_per_click": 2.6,
"ad_title": "New Arrival",
"ad_text": "Check out our latest product!",
"start_date": 0,
"end_date": 14,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "7ba54148-4885-48da-97ba-6ecf7b3a300b",
"campaign_data": {
"impressions_limit": 8,
"clicks_limit": 8,
"cost_per_impression": 1.9,
"cost_per_click": 2.3,
"ad_title": "Discover More",
"ad_text": "Find out what we offer!",
"start_date": 0,
"end_date": 16,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "00ac5657-1a1a-48f1-815a-2ffdb72b82b8",
"campaign_data": {
"impressions_limit": 7,
"clicks_limit": 7,
"cost_per_impression": 1.4,
"cost_per_click": 2.8,
"ad_title": "Best Quality",
"ad_text": "Experience the best quality!",
"start_date": 0,
"end_date": 18,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "b9d5a8ce-ae23-4a93-946e-4bd85bddef5c",
"campaign_data": {
"impressions_limit": 9,
"clicks_limit": 9,
"cost_per_impression": 1.6,
"cost_per_click": 2.2,
"ad_title": "Limited Edition",
"ad_text": "Get it while it lasts!",
"start_date": 0,
"end_date": 15,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "3696b94d-6591-4e15-86f3-8c46ee535e27",
"campaign_data": {
"impressions_limit": 10,
"clicks_limit": 10,
"cost_per_impression": 1.2,
"cost_per_click": 2.9,
"ad_title": "Exclusive Access",
"ad_text": "Join us for exclusive access!",
"start_date": 0,
"end_date": 13,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "20024aab-2476-4d35-b96f-f47589f05474",
"campaign_data": {
"impressions_limit": 6,
"clicks_limit": 6,
"cost_per_impression": 1.8,
"cost_per_click": 2.5,
"ad_title": "Best Experience",
"ad_text": "Experience the best with us!",
"start_date": 0,
"end_date": 14,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "a68ef7d6-c50f-40d1-b70f-a32d7cdcd036",
"campaign_data": {
"impressions_limit": 5,
"clicks_limit": 5,
"cost_per_impression": 1.5,
"cost_per_click": 2.7,
"ad_title": "Incredible Quality",
"ad_text": "Get the quality you deserve!",
"start_date": 0,
"end_date": 12,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "960e2896-3f34-43a3-8aa1-d984bb92b96e",
"campaign_data": {
"impressions_limit": 7,
"clicks_limit": 7,
"cost_per_impression": 1.3,
"cost_per_click": 2.4,
"ad_title": "Best Value",
"ad_text": "Get the best value for your money!",
"start_date": 0,
"end_date": 11,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "80a7f6a9-5939-4d74-9a7e-e455b9fa0fdd",
"campaign_data": {
"impressions_limit": 6,
"clicks_limit": 6,
"cost_per_impression": 1.9,
"cost_per_click": 2.6,
"ad_title": "Exclusive Offer",
"ad_text": "Don't miss out on this offer!",
"start_date": 0,
"end_date": 15,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "6bb59511-360d-49dc-9626-be2f6b9b3a0a",
"campaign_data": {
"impressions_limit": 8,
"clicks_limit": 8,
"cost_per_impression": 1.7,
"cost_per_click": 2.3,
"ad_title": "Limited Time Deal",
"ad_text": "Act fast to get this deal!",
"start_date": 0,
"end_date": 13,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "d7a27dc9-7d8d-4f25-bea4-2528f88238d1",
"campaign_data": {
"impressions_limit": 9,
"clicks_limit": 9,
"cost_per_impression": 1.2,
"cost_per_click": 2.9,
"ad_title": "Discover Our Services",
"ad_text": "Find out what we can do for you!",
"start_date": 0,
"end_date": 14,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "e6cde283-ca4b-456c-b1fb-c2967918b5ef",
"campaign_data": {
"impressions_limit": 10,
"clicks_limit": 10,
"cost_per_impression": 1.5,
"cost_per_click": 2.8,
"ad_title": "Get Started Today",
"ad_text": "Join us and start saving!",
"start_date": 0,
"end_date": 12,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "2a51ba38-bed3-43af-bbb4-99093b72e0d1",
"campaign_data": {
"impressions_limit": 6,
"clicks_limit": 6,
"cost_per_impression": 1.8,
"cost_per_click": 2.5,
"ad_title": "Best Choice",
"ad_text": "Choose the best for your needs!",
"start_date": 0,
"end_date": 11,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "95a9a488-8fee-4aa6-9c21-458f8f5523ca",
"campaign_data": {
"impressions_limit": 7,
"clicks_limit": 7,
"cost_per_impression": 1.3,
"cost_per_click": 2.2,
"ad_title": "Exclusive Access",
"ad_text": "Get exclusive access to our services!",
"start_date": 0,
"end_date": 13,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "2fb992b8-4441-47f5-9176-1da68526eafb",
"campaign_data": {
"impressions_limit": 8,
"clicks_limit": 8,
"cost_per_impression": 1.9,
"cost_per_click": 2.6,
"ad_title": "Best Quality",
"ad_text": "Experience the best quality!",
"start_date": 0,
"end_date": 15,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "f0d160bd-18b4-4c88-917c-06b699abeeec",
"campaign_data": {
"impressions_limit": 9,
"clicks_limit": 9,
"cost_per_impression": 1.4,
"cost_per_click": 2.3,
"ad_title": "Incredible Quality",
"ad_text": "Get the quality you deserve!",
"start_date": 0,
"end_date": 12,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "d8147d8c-2bcd-4ac8-bab5-82e420509a7f",
"campaign_data": {
"impressions_limit": 10,
"clicks_limit": 10,
"cost_per_impression": 1.6,
"cost_per_click": 2.8,
"ad_title": "Discover Our Services",
"ad_text": "Find out what we can do for you!",
"start_date": 0,
"end_date": 14,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "94209720-2c10-4a55-bc37-c28838d08012",
"campaign_data": {
"impressions_limit": 6,
"clicks_limit": 6,
"cost_per_impression": 1.2,
"cost_per_click": 2.9,
"ad_title": "Best Value",
"ad_text": "Get the best value for your money!",
"start_date": 0,
"end_date": 11,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "33fae4ce-d252-4b34-af2b-c11e770f65b5",
"campaign_data": {
"impressions_limit": 7,
"clicks_limit": 7,
"cost_per_impression": 1.3,
"cost_per_click": 2.4,
"ad_title": "Exclusive Offer",
"ad_text": "Don't miss out on this offer!",
"start_date": 0,
"end_date": 12,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "2c0261ba-bd3c-4b19-a53c-44efa10819e5",
"campaign_data": {
"impressions_limit": 8,
"clicks_limit": 8,
"cost_per_impression": 1.9,
"cost_per_click": 2.6,
"ad_title": "Best Choice",
"ad_text": "Choose the best for your needs!",
"start_date": 0,
"end_date": 13,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "0892fbb7-fbc6-45e9-b52f-494258a9c8f8",
"campaign_data": {
"impressions_limit": 9,
"clicks_limit": 9,
"cost_per_impression": 1.5,
"cost_per_click": 2.3,
"ad_title": "Incredible Quality",
"ad_text": "Get the quality you deserve!",
"start_date": 0,
"end_date": 14,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "055af498-567a-4a4e-b25f-60a57b9b591c",
"campaign_data": {
"impressions_limit": 10,
"clicks_limit": 10,
"cost_per_impression": 1.6,
"cost_per_click": 2.8,
"ad_title": "Get Started Today",
"ad_text": "Join us and start saving!",
"start_date": 0,
"end_date": 12,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "0dfd7246-4cfa-4721-899b-e6dd9c483626",
"campaign_data": {
"impressions_limit": 6,
"clicks_limit": 6,
"cost_per_impression": 1.2,
"cost_per_click": 2.9,
"ad_title": "Best Experience",
"ad_text": "Experience the best with us!",
"start_date": 0,
"end_date": 11,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
},
{
"advertiser_id": "32d75bc6-0779-4081-98b3-58273497c561",
"campaign_data": {
"impressions_limit": 7,
"clicks_limit": 7,
"cost_per_impression": 1.3,
"cost_per_click": 2.2,
"ad_title": "Discover Our Services",
"ad_text": "Find out what we can do for you!",
"start_date": 0,
"end_date": 13,
"targeting": {
"gender": null,
"age_from": null,
"age_to": null,
"location": null
}
}
}
]
File diff suppressed because it is too large Load Diff
+458
View File
@@ -0,0 +1,458 @@
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AdNova Loadtest</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: 'Inter', sans-serif;
}
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
.chart-container {
position: relative;
height: 250px;
width: 100%;
}
.status-dot {
height: 10px;
width: 10px;
border-radius: 50%;
display: inline-block;
}
.tooltip {
visibility: hidden;
}
.has-tooltip:hover .tooltip {
visibility: visible;
}
</style>
</head>
<body class="bg-slate-900 text-gray-300">
<div class="container mx-auto p-4 md:p-8">
<header class="mb-8">
<h1 class="text-4xl font-bold text-white tracking-tight">AdNova Loadtest</h1>
</header>
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
<!-- Left Column: Controls -->
<div class="lg:col-span-1 flex flex-col gap-6">
<!-- Mock Data Management -->
<div class="bg-slate-800 rounded-lg p-6 shadow-lg">
<h2 class="text-xl font-semibold text-white mb-4">1. Data Setup</h2>
<button id="loadMocksBtn"
class="w-full bg-indigo-600 hover:bg-indigo-500 text-white font-bold py-2 px-4 rounded-lg transition-colors duration-300 flex items-center justify-center">
Load Mock Data
</button>
<div id="mockStatus" class="mt-4 space-y-2 text-sm">
<div class="flex items-center justify-between"><span class="text-slate-300">Clients:</span><span
id="clientsStatus" class="font-mono">Checking...</span></div>
<div class="flex items-center justify-between"><span
class="text-slate-300">Advertisers:</span><span id="advertisersStatus"
class="font-mono">Checking...</span></div>
<div class="flex items-center justify-between"><span
class="text-slate-300">Campaigns:</span><span id="campaignsStatus"
class="font-mono">Checking...</span></div>
<div class="flex items-center justify-between"><span class="text-slate-300">ML
Scores:</span><span id="mlScoresStatus" class="font-mono">Checking...</span></div>
</div>
</div>
<!-- Test Configuration -->
<div id="config-panel" class="bg-slate-800 rounded-lg p-6 shadow-lg">
<h2 class="text-xl font-semibold text-white mb-4">2. Test Configuration</h2>
<div class="space-y-4">
<div>
<label for="loadProfile" class="block text-sm font-medium text-slate-300">Load
Profile</label>
<select id="loadProfile"
class="mt-1 block w-full bg-slate-700 border border-slate-600 rounded-md shadow-sm py-2 px-3 text-white focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
<option value="const">Constant</option>
<option value="line">Line</option>
<option value="step">Step</option>
<option value="once">Once</option>
<option value="unlimited">Unlimited</option>
</select>
</div>
<!-- Profile Specific Options -->
<div id="const-options">
<label for="maxRps" class="block text-sm font-medium text-slate-300">Requests Per Second
(RPS)</label>
<input type="number" id="maxRps" value="100"
class="mt-1 block w-full bg-slate-700 border border-slate-600 rounded-md shadow-sm py-2 px-3 text-white focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
</div>
<div id="line-options" class="hidden space-y-2">
<input type="number" id="fromRps" placeholder="From RPS (e.g., 10)"
class="block w-full bg-slate-700 border border-slate-600 rounded-md py-2 px-3 text-white">
<input type="number" id="toRps" placeholder="To RPS (e.g., 100)"
class="block w-full bg-slate-700 border border-slate-600 rounded-md py-2 px-3 text-white">
</div>
<div id="step-options" class="hidden space-y-2">
<input type="number" id="stepFromRps" placeholder="From RPS"
class="block w-full bg-slate-700 border border-slate-600 rounded-md py-2 px-3 text-white">
<input type="number" id="stepToRps" placeholder="To RPS"
class="block w-full bg-slate-700 border border-slate-600 rounded-md py-2 px-3 text-white">
<input type="number" id="stepRps" placeholder="Step Size (RPS)"
class="block w-full bg-slate-700 border border-slate-600 rounded-md py-2 px-3 text-white">
<input type="number" id="stepDuration" placeholder="Step Duration (sec)"
class="block w-full bg-slate-700 border border-slate-600 rounded-md py-2 px-3 text-white">
</div>
<div id="once-options" class="hidden">
<input type="number" id="onceCount" placeholder="Number of Requests"
class="block w-full bg-slate-700 border border-slate-600 rounded-md py-2 px-3 text-white">
</div>
<div id="unlimited-options"
class="hidden p-2 bg-yellow-900/50 rounded-md text-yellow-300 text-xs">
Warning: This profile sends requests as fast as possible and may exhaust system resources.
</div>
</div>
</div>
<!-- Start/Stop Controls -->
<div class="bg-slate-800 rounded-lg p-6 shadow-lg">
<h2 class="text-xl font-semibold text-white mb-4">3. Execute Test</h2>
<button id="startStopBtn"
class="w-full bg-green-600 hover:bg-green-500 text-white font-bold py-3 px-4 rounded-lg transition-colors duration-300 text-lg flex items-center justify-center">
Start Test
</button>
</div>
</div>
<!-- Right Column: Stats & Charts -->
<div class="lg:col-span-2 flex flex-col gap-6">
<!-- Summary Stats -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="bg-slate-800 p-4 rounded-lg shadow-lg text-center">
<h3 class="text-sm font-medium text-slate-400">RPS</h3>
<p id="rpsStat" class="text-3xl font-semibold text-white">0</p>
</div>
<div class="bg-slate-800 p-4 rounded-lg shadow-lg text-center">
<h3 class="text-sm font-medium text-slate-400">Avg Latency (ms)</h3>
<p id="latencyStat" class="text-3xl font-semibold text-white">0.00</p>
</div>
<div class="bg-slate-800 p-4 rounded-lg shadow-lg text-center">
<h3 class="text-sm font-medium text-slate-400">Error Rate</h3>
<p id="errorRateStat" class="text-3xl font-semibold text-white">0.00%</p>
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div class="bg-slate-800 p-4 rounded-lg shadow-lg text-center">
<h3 class="text-sm font-medium text-slate-400">Total Requests</h3>
<p id="totalReqsStat" class="text-2xl font-semibold text-white">0</p>
</div>
<div class="bg-slate-800 p-4 rounded-lg shadow-lg text-center">
<h3 class="text-sm font-medium text-slate-400">Total Errors</h3>
<p id="totalErrorsStat" class="text-2xl font-semibold text-white">0</p>
</div>
</div>
<!-- Charts -->
<div class="bg-slate-800 p-4 rounded-lg shadow-lg">
<h3 class="font-semibold text-white mb-2">Requests Per Second (RPS)</h3>
<div class="chart-container"><canvas id="rpsChart"></canvas></div>
</div>
<div class="bg-slate-800 p-4 rounded-lg shadow-lg">
<h3 class="font-semibold text-white mb-2">Average Latency (ms)</h3>
<div class="chart-container"><canvas id="latencyChart"></canvas></div>
</div>
<div class="bg-slate-800 p-4 rounded-lg shadow-lg">
<h3 class="font-semibold text-white mb-2">Error Rate (%)</h3>
<div class="chart-container"><canvas id="errorRateChart"></canvas></div>
</div>
</div>
</div>
</div>
<script>
const loadProfileSelect = document.getElementById( 'loadProfile' )
const startStopBtn = document.getElementById( 'startStopBtn' )
const loadMocksBtn = document.getElementById( 'loadMocksBtn' )
const configPanel = document.getElementById( 'config-panel' )
const rpsStat = document.getElementById( 'rpsStat' )
const latencyStat = document.getElementById( 'latencyStat' )
const errorRateStat = document.getElementById( 'errorRateStat' )
const totalReqsStat = document.getElementById( 'totalReqsStat' )
const totalErrorsStat = document.getElementById( 'totalErrorsStat' )
let ws
let rpsChart, latencyChart, errorRateChart
let isRunning = false
const MAX_DATA_POINTS = 60
function createChart ( ctx, label, color )
{
return new Chart( ctx, {
type: 'line',
data: {
labels: [],
datasets: [ {
label: label,
data: [],
borderColor: color,
backgroundColor: color + '33',
borderWidth: 2,
pointRadius: 0,
tension: 0.4,
fill: true,
} ]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
ticks: { color: '#94a3b8' },
grid: { color: '#334155' }
},
y: {
beginAtZero: true,
ticks: { color: '#94a3b8' },
grid: { color: '#334155' }
}
},
plugins: {
legend: { display: false }
}
}
} )
}
function updateChart ( chart, label, newData )
{
chart.data.labels.push( label )
chart.data.datasets[ 0 ].data.push( newData )
if ( chart.data.labels.length > MAX_DATA_POINTS )
{
chart.data.labels.shift()
chart.data.datasets[ 0 ].data.shift()
}
chart.update( 'none' )
}
function resetCharts ()
{
const charts = [ rpsChart, latencyChart, errorRateChart ]
charts.forEach( chart =>
{
if ( chart )
{
chart.data.labels = []
chart.data.datasets[ 0 ].data = []
chart.update()
}
} )
rpsStat.textContent = '0'
latencyStat.textContent = '0.00'
errorRateStat.textContent = '0.00%'
totalReqsStat.textContent = '0'
totalErrorsStat.textContent = '0'
}
function setupWebSocket ()
{
const wsUrl = 'ws://' + window.location.host + '/ws'
ws = new WebSocket( wsUrl )
ws.onopen = () => console.log( 'WebSocket connected' )
ws.onclose = () => console.log( 'WebSocket disconnected' )
ws.onerror = ( error ) => console.error( 'WebSocket error:', error )
ws.onmessage = ( event ) =>
{
const data = JSON.parse( event.data )
if ( typeof data.isRunning !== 'undefined' )
{
if ( data.isRunning )
{
isRunning = true
startStopBtn.textContent = 'Stop Test'
startStopBtn.classList.remove( 'bg-green-600', 'hover:bg-green-500' )
startStopBtn.classList.add( 'bg-red-600', 'hover:bg-red-500' )
configPanel.querySelectorAll( 'input, select' ).forEach( el => el.disabled = true )
}
}
if ( data.isRunning )
{
const now = new Date().toLocaleTimeString()
rpsStat.textContent = data.rps
latencyStat.textContent = data.latency.toFixed( 2 )
errorRateStat.textContent = data.errorRate.toFixed( 2 ) + '%'
totalReqsStat.textContent = data.totalReqs
totalErrorsStat.textContent = data.totalErrors
updateChart( rpsChart, now, data.rps )
updateChart( latencyChart, now, data.latency )
updateChart( errorRateChart, now, data.errorRate )
} else
{
totalReqsStat.textContent = data.totalReqs
totalErrorsStat.textContent = data.totalErrors
}
}
}
function setRunningState ()
{
isRunning = true
startStopBtn.textContent = 'Stop Test'
startStopBtn.classList.remove( 'bg-green-600', 'hover:bg-green-500' )
startStopBtn.classList.add( 'bg-red-600', 'hover:bg-red-500' )
configPanel.querySelectorAll( 'input, select' ).forEach( el => el.disabled = true )
resetCharts()
}
function setStoppedState ()
{
isRunning = false
startStopBtn.textContent = 'Start Test'
startStopBtn.classList.remove( 'bg-red-600', 'hover:bg-red-500' )
startStopBtn.classList.add( 'bg-green-600', 'hover:bg-green-500' )
configPanel.querySelectorAll( 'input, select' ).forEach( el => el.disabled = false )
}
startStopBtn.addEventListener( 'click', () =>
{
if ( isRunning )
{
fetch( '/api/stop-test', { method: 'POST' } )
.then( () => setStoppedState() )
} else
{
const config = {
loadProfile: loadProfileSelect.value,
maxRps: parseInt( document.getElementById( 'maxRps' ).value ) || 100,
fromRps: parseInt( document.getElementById( 'fromRps' ).value ) || 0,
toRps: parseInt( document.getElementById( 'toRps' ).value ) || 0,
stepFromRps: parseInt( document.getElementById( 'stepFromRps' ).value ) || 0,
stepToRps: parseInt( document.getElementById( 'stepToRps' ).value ) || 0,
stepRps: parseInt( document.getElementById( 'stepRps' ).value ) || 0,
stepDuration: parseInt( document.getElementById( 'stepDuration' ).value ) || 0,
onceCount: parseInt( document.getElementById( 'onceCount' ).value ) || 0,
}
if ( config.loadProfile === 'step' )
{
config.fromRps = config.stepFromRps
config.toRps = config.stepToRps
}
fetch( '/api/start-test', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify( config )
} ).then( res =>
{
if ( res.ok ) setRunningState()
else alert( "Failed to start test. Check console for details." )
} ).catch( err =>
{
console.error( "Error starting test:", err )
alert( "Failed to start test. Network error or server unreachable." )
} )
}
} )
function updateMockStatus ( status )
{
const statuses = {
clients: document.getElementById( 'clientsStatus' ),
advertisers: document.getElementById( 'advertisersStatus' ),
campaigns: document.getElementById( 'campaignsStatus' ),
ml_scores: document.getElementById( 'mlScoresStatus' ),
}
for ( const key in status )
{
const el = statuses[ key ]
if ( el )
{
if ( status[ key ] )
{
el.innerHTML = '<span class="status-dot bg-green-500 mr-2"></span>Loaded'
el.classList.remove( 'text-red-400' )
el.classList.add( 'text-green-400' )
} else
{
el.innerHTML = '<span class="status-dot bg-red-500 mr-2"></span>Not Loaded'
el.classList.remove( 'text-green-400' )
el.classList.add( 'text-red-400' )
}
}
}
}
function checkMocks ()
{
fetch( '/api/check-mocks' )
.then( res => res.json() )
.then( data => updateMockStatus( data ) )
.catch( err => console.error( "Failed to check mocks:", err ) )
}
loadMocksBtn.addEventListener( 'click', () =>
{
loadMocksBtn.textContent = 'Loading...'
loadMocksBtn.disabled = true
fetch( '/api/load-mocks', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify( {} )
} )
.then( res =>
{
if ( !res.ok ) alert( 'Failed to load mocks. Check console for details.' )
return res.json()
} )
.then( () =>
{
checkMocks()
} )
.finally( () =>
{
loadMocksBtn.textContent = 'Load Mock Data'
loadMocksBtn.disabled = false
} )
} )
loadProfileSelect.addEventListener( 'change', ( e ) =>
{
document.getElementById( 'const-options' ).classList.add( 'hidden' )
document.getElementById( 'line-options' ).classList.add( 'hidden' )
document.getElementById( 'step-options' ).classList.add( 'hidden' )
document.getElementById( 'once-options' ).classList.add( 'hidden' )
document.getElementById( 'unlimited-options' ).classList.add( 'hidden' )
document.getElementById( e.target.value + '-options' ).classList.remove( 'hidden' )
} )
window.onload = () =>
{
rpsChart = createChart( document.getElementById( 'rpsChart' ).getContext( '2d' ), 'RPS', '#6366f1' )
latencyChart = createChart( document.getElementById( 'latencyChart' ).getContext( '2d' ), 'Latency', '#34d399' )
errorRateChart = createChart( document.getElementById( 'errorRateChart' ).getContext( '2d' ), 'Error Rate', '#f87171' )
setupWebSocket()
checkMocks()
loadProfileSelect.dispatchEvent( new Event( 'change' ) )
};
</script>
</body>
</html>