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.
This application now includes a real C library that is compiled and called from .NET via P/Invoke, providing true native code integration.
- 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
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
- .NET 8.0 SDK
- Docker
- Kubernetes cluster (local or cloud-based)
- kubectl configured to access your cluster
# Restore dependencies
dotnet restore
# Build the solution
dotnet build
# Run locally
dotnet run --project StressTestApp# Build Docker image
docker build -t stress-test-app:latest .
# Run in Docker (for testing)
docker run --rm -it stress-test-app:latest# 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# Make script executable
chmod +x deploy.sh
# Build and deploy
./deploy.sh build-deploy
# Other options: build, deploy, clean# 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{
"StressTestApp": {
"OperationDurationMinutes": 4,
"CycleIntervalMinutes": 5,
"EnableDetailedLogging": false
}
}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
- Startup: Application starts and initializes native worker threads
- CPU Cycle (4 minutes):
- Multi-threaded CPU-intensive operations across all available cores
- Prime number calculations
- Matrix multiplications
- Hash computations
- Parallel native math operations
- Wait Period (1 minute): Brief pause between cycles
- Memory Cycle (4 minutes):
- Large memory allocations (1MB-50MB arrays)
- String array manipulations
- Complex object creation and management
- Memory scanning operations
- Repeat: Cycle continues until application is stopped
The application includes comprehensive health checks that monitor:
- Service states (CPU/Memory services)
- Native worker thread status
- Memory usage metrics
- System resource information
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
# 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-hpaModify the StressTestSchedulerService constructor or configuration:
private readonly TimeSpan _operationDuration = TimeSpan.FromMinutes(4);
private readonly TimeSpan _cycleInterval = TimeSpan.FromMinutes(5);Update the Kubernetes deployment YAML:
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "2000m"
memory: "4Gi"-
Out of Memory Errors
- Increase memory limits in Kubernetes deployment
- Adjust allocation sizes in
MemoryIntensiveService
-
CPU Throttling
- Increase CPU limits in deployment
- Reduce the number of concurrent operations
-
Pod Crashes
- Check resource limits
- Review logs for specific error messages
- Verify node resources are sufficient
# 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- 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
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
This project is provided as-is for testing and educational purposes.