|
Load Balancer Simulation
A simulation of a load balancer with dynamic server scaling in C++
|
Main simulation engine that manages web servers and request queue. More...
#include <LoadBalancer.h>
Public Member Functions | |
| LoadBalancer (int initialServers, const LoadBalancerConfig &config) | |
| Construct the load balancer with initial servers and config. | |
| void | run (int totalClockCycles) |
| Run the simulation for the specified number of clock cycles. | |
| void | prefillQueue (int count) |
| Prefill the request queue with random requests. | |
| void | generateRequest () |
| Possibly generate a new request (based on probability). | |
| void | scaleServers () |
| Add or remove servers based on queue size thresholds. | |
| void | assignRequests () |
| Assign queued requests to idle servers. | |
| void | printSummary (int initialServerCount) const |
| Print simulation summary statistics. | |
Private Member Functions | |
| Request | createRandomRequest () |
| Create a random request with random IPs and processing time. | |
| std::string | generateRandomIP () |
| Generate a random IP address string (x.x.x.x format). | |
| void | log (const char *colorCode, const char *msg) const |
| Log a message with optional color code. | |
Private Attributes | |
| LoadBalancerConfig | config_ |
| std::queue< Request > | requestQueue_ |
| std::vector< std::unique_ptr< WebServer > > | servers_ |
| int | currentTime_ |
| int | minQueueThreshold_ |
| int | maxQueueThreshold_ |
| int | lastScaleTime_ |
| int | nextServerId_ |
| std::mt19937 | rng_ |
| std::bernoulli_distribution | requestDist_ |
| std::uniform_int_distribution< int > | timeDist_ |
| std::uniform_int_distribution< int > | octetDist_ |
| std::uniform_int_distribution< int > | jobTypeDist_ |
| int | totalProcessed_ |
| int | totalBlocked_ |
| int | requestsAddedToQueue_ |
| int | serversAdded_ |
| int | serversRemoved_ |
Main simulation engine that manages web servers and request queue.
Handles request generation, assignment to servers, and dynamic scaling based on queue thresholds.
Definition at line 24 of file LoadBalancer.h.
| LoadBalancer::LoadBalancer | ( | int | initialServers, |
| const LoadBalancerConfig & | config ) |
Construct the load balancer with initial servers and config.
| initialServers | Number of web servers to start with |
| config | Configuration parameters for the simulation |
Definition at line 40 of file LoadBalancer.cpp.
| void LoadBalancer::assignRequests | ( | ) |
Assign queued requests to idle servers.
Definition at line 129 of file LoadBalancer.cpp.
Referenced by run().
|
private |
Create a random request with random IPs and processing time.
Definition at line 72 of file LoadBalancer.cpp.
Referenced by generateRequest(), and prefillQueue().
|
private |
Generate a random IP address string (x.x.x.x format).
Definition at line 65 of file LoadBalancer.cpp.
Referenced by createRandomRequest().
| void LoadBalancer::generateRequest | ( | ) |
Possibly generate a new request (based on probability).
Definition at line 93 of file LoadBalancer.cpp.
Referenced by run().
|
private |
Log a message with optional color code.
| colorCode | ANSI color code (or empty) |
| msg | Message to log |
Definition at line 28 of file LoadBalancer.cpp.
Referenced by generateRequest(), prefillQueue(), run(), and scaleServers().
| void LoadBalancer::prefillQueue | ( | int | count | ) |
Prefill the request queue with random requests.
| count | Number of requests to add to the queue |
Definition at line 82 of file LoadBalancer.cpp.
Referenced by main().
| void LoadBalancer::printSummary | ( | int | initialServerCount | ) | const |
Print simulation summary statistics.
| initialServerCount | Starting number of servers for comparison |
Definition at line 214 of file LoadBalancer.cpp.
Referenced by main().
| void LoadBalancer::run | ( | int | totalClockCycles | ) |
Run the simulation for the specified number of clock cycles.
| totalClockCycles | Number of simulation cycles to execute |
Definition at line 172 of file LoadBalancer.cpp.
Referenced by main().
| void LoadBalancer::scaleServers | ( | ) |
Add or remove servers based on queue size thresholds.
Definition at line 139 of file LoadBalancer.cpp.
Referenced by run().
|
private |
Simulation configuration
Definition at line 86 of file LoadBalancer.h.
Referenced by generateRequest(), LoadBalancer(), log(), printSummary(), run(), and scaleServers().
|
private |
Current simulation clock
Definition at line 89 of file LoadBalancer.h.
Referenced by LoadBalancer(), log(), run(), and scaleServers().
|
private |
Distribution for job type
Definition at line 99 of file LoadBalancer.h.
Referenced by createRandomRequest(), and LoadBalancer().
|
private |
Last clock cycle when scaling occurred
Definition at line 92 of file LoadBalancer.h.
Referenced by LoadBalancer(), and scaleServers().
|
private |
Queue size to trigger server addition
Definition at line 91 of file LoadBalancer.h.
Referenced by LoadBalancer(), and scaleServers().
|
private |
Queue size to trigger server removal
Definition at line 90 of file LoadBalancer.h.
Referenced by LoadBalancer(), and scaleServers().
|
private |
ID for next server to be created
Definition at line 93 of file LoadBalancer.h.
Referenced by LoadBalancer(), and scaleServers().
|
private |
Distribution for IP octets
Definition at line 98 of file LoadBalancer.h.
Referenced by generateRandomIP(), and LoadBalancer().
|
private |
Distribution for request generation
Definition at line 96 of file LoadBalancer.h.
Referenced by generateRequest(), and LoadBalancer().
|
private |
Pending requests
Definition at line 87 of file LoadBalancer.h.
Referenced by assignRequests(), generateRequest(), prefillQueue(), printSummary(), and scaleServers().
|
private |
Total new requests added
Definition at line 103 of file LoadBalancer.h.
Referenced by generateRequest(), LoadBalancer(), and printSummary().
|
private |
Random number generator
Definition at line 95 of file LoadBalancer.h.
Referenced by createRandomRequest(), generateRandomIP(), generateRequest(), and LoadBalancer().
|
private |
Web server pool
Definition at line 88 of file LoadBalancer.h.
Referenced by assignRequests(), LoadBalancer(), printSummary(), run(), and scaleServers().
|
private |
Total servers added during simulation
Definition at line 104 of file LoadBalancer.h.
Referenced by LoadBalancer(), printSummary(), run(), and scaleServers().
|
private |
Total servers removed during simulation
Definition at line 105 of file LoadBalancer.h.
Referenced by LoadBalancer(), printSummary(), run(), and scaleServers().
|
private |
Distribution for processing time
Definition at line 97 of file LoadBalancer.h.
Referenced by createRandomRequest(), and LoadBalancer().
|
private |
Total requests blocked (bad octet)
Definition at line 102 of file LoadBalancer.h.
Referenced by generateRequest(), LoadBalancer(), and printSummary().
|
private |
Total requests completed
Definition at line 101 of file LoadBalancer.h.
Referenced by LoadBalancer(), printSummary(), and run().