Skip to content

nivebb8/NativeTestApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

.NET Core Stress Test Application with Native C Library

A comprehensive .NET Core 8.0 application designed to perform intensive CPU and memory operations using actual native C code via P/Invoke. The application alternates between CPU-intensive and memory-intensive operations every 5 minutes, with each operation running for 4 minutes.

πŸ—οΈ Architecture

This application now includes a real C library that is compiled and called from .NET via P/Invoke, providing true native code integration.

Features

  • CPU-Intensive Operations: Multi-threaded mathematical computations with native C acceleration
  • Memory-Intensive Operations: Both managed (.NET) and unmanaged (C) memory operations
  • Native C Library: Actual C code compiled to shared library (.dll/.so) with P/Invoke integration
  • Background Native Worker: Separate C thread running continuously with system calls
  • Kubernetes Ready: Multi-stage Docker build that compiles C code in container
  • Health Monitoring: Monitoring of both .NET services and native C components
  • Cross-Platform: Works on both Windows and Linux containers

Architecture

StressTestApp/
β”œβ”€β”€ Services/
β”‚   β”œβ”€β”€ CpuIntensiveService.cs          # CPU-intensive operations
β”‚   β”œβ”€β”€ MemoryIntensiveService.cs       # Memory-intensive operations
β”‚   └── StressTestSchedulerService.cs   # Main scheduler service
β”œβ”€β”€ HealthChecks/
β”‚   └── StressTestHealthCheck.cs        # Health monitoring
└── Program.cs                          # Application entry point

NativeComponent/
β”œβ”€β”€ NativeWorker.cs                     # Native worker thread with unsafe operations
└── NativeMathOperations.cs             # Native mathematical operations

Prerequisites

  • .NET 8.0 SDK
  • Docker
  • Kubernetes cluster (local or cloud-based)
  • kubectl configured to access your cluster

Building the Application

Local Development

# Restore dependencies
dotnet restore

# Build the solution
dotnet build

# Run locally
dotnet run --project StressTestApp

Docker Build

# Build Docker image
docker build -t stress-test-app:latest .

# Run in Docker (for testing)
docker run --rm -it stress-test-app:latest

Deployment to Kubernetes

Using PowerShell (Windows)

# Build and deploy
.\deploy.ps1 -Action build-deploy

# Just build
.\deploy.ps1 -Action build

# Just deploy
.\deploy.ps1 -Action deploy

# Clean up
.\deploy.ps1 -Action clean

Using Bash (Linux/macOS)

# Make script executable
chmod +x deploy.sh

# Build and deploy
./deploy.sh build-deploy

# Other options: build, deploy, clean

Manual Deployment

# Apply ConfigMap
kubectl apply -f k8s/configmap.yaml

# Apply Deployment and Service
kubectl apply -f k8s/deployment.yaml

# Check status
kubectl get pods -l app=stress-test-app
kubectl logs -l app=stress-test-app -f

Configuration

Application Settings (appsettings.json)

{
  "StressTestApp": {
    "OperationDurationMinutes": 4,
    "CycleIntervalMinutes": 5,
    "EnableDetailedLogging": false
  }
}

Kubernetes Resource Limits

The application is configured with the following default limits:

  • CPU: 500m request, 2000m limit (up to 2 cores)
  • Memory: 512Mi request, 4Gi limit
  • Auto-scaling: 1-3 replicas based on CPU/Memory usage

Operation Cycle

  1. Startup: Application starts and initializes native worker threads
  2. CPU Cycle (4 minutes):
    • Multi-threaded CPU-intensive operations across all available cores
    • Prime number calculations
    • Matrix multiplications
    • Hash computations
    • Parallel native math operations
  3. Wait Period (1 minute): Brief pause between cycles
  4. Memory Cycle (4 minutes):
    • Large memory allocations (1MB-50MB arrays)
    • String array manipulations
    • Complex object creation and management
    • Memory scanning operations
  5. Repeat: Cycle continues until application is stopped

Monitoring and Health Checks

Health Endpoints

The application includes comprehensive health checks that monitor:

  • Service states (CPU/Memory services)
  • Native worker thread status
  • Memory usage metrics
  • System resource information

Logging

Structured logging with different levels:

  • Information: Cycle start/end, major operations
  • Debug: Detailed operation progress (development mode)
  • Warning: Resource constraints, operation cancellations
  • Error: Exceptions and failures

Kubernetes Monitoring

# Check pod status
kubectl get pods -l app=stress-test-app

# Monitor logs
kubectl logs -l app=stress-test-app -f

# Check resource usage
kubectl top pods -l app=stress-test-app

# Check HPA status
kubectl get hpa stress-test-app-hpa

Customization

Adjusting Operation Duration

Modify the StressTestSchedulerService constructor or configuration:

private readonly TimeSpan _operationDuration = TimeSpan.FromMinutes(4);
private readonly TimeSpan _cycleInterval = TimeSpan.FromMinutes(5);

Resource Limits

Update the Kubernetes deployment YAML:

resources:
  requests:
    cpu: "500m"
    memory: "512Mi"
  limits:
    cpu: "2000m"
    memory: "4Gi"

Troubleshooting

Common Issues

  1. Out of Memory Errors

    • Increase memory limits in Kubernetes deployment
    • Adjust allocation sizes in MemoryIntensiveService
  2. CPU Throttling

    • Increase CPU limits in deployment
    • Reduce the number of concurrent operations
  3. Pod Crashes

    • Check resource limits
    • Review logs for specific error messages
    • Verify node resources are sufficient

Useful Commands

# Describe pod for detailed information
kubectl describe pod -l app=stress-test-app

# Check events
kubectl get events --sort-by=.metadata.creationTimestamp

# Port forward for local testing
kubectl port-forward deployment/stress-test-app 8080:8080

# Scale manually
kubectl scale deployment stress-test-app --replicas=2

Security Considerations

  • Application runs as non-root user (UID 1000)
  • Read-only root filesystem disabled due to native operations
  • Network policies can be applied for network isolation
  • Resource quotas prevent resource exhaustion

Contributing

This application is designed as a stress testing tool for Kubernetes environments. Modifications can be made to:

  • Adjust stress testing patterns
  • Add new types of intensive operations
  • Enhance monitoring and metrics
  • Improve resource management

License

This project is provided as-is for testing and educational purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published