-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestLB.cpp
158 lines (140 loc) · 3.1 KB
/
TestLB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Test the various Load Balance
// Yuchuan Wang
// yuchuan.wang@gmail.com
#include "LoadBalanceRoundRobin.h"
#include "LoadBalanceWeightedRoundRobin.h"
#include "LoadBalanceRandom.h"
#include "LoadBalanceWeightedRandom.h"
#include "LoadBalanceConsistentHashing.h"
/*
Server Hit stats with round robin:
192.168.1.14: 200000
192.168.1.13: 200000
192.168.1.12: 200000
192.168.1.11: 200000
192.168.1.10: 200000
*/
void TestRoundRobin()
{
LoadBalanceRoundRobin lb;
lb.AddServer("192.168.1.10");
lb.AddServer("192.168.1.11");
lb.AddServer("192.168.1.12");
lb.AddServer("192.168.1.13");
lb.AddServer("192.168.1.14");
for(int i = 0; i < 1000000; i++)
{
lb.NextRequest();
}
lb.PrintStats();
}
/*
Server Hit stats with weighted round robin:
192.168.1.13: 200000
192.168.1.10: 50000
192.168.1.12: 150000
192.168.1.11: 100000
192.168.1.14: 500000
*/
void TestWeightedRoundRobin()
{
LoadBalanceWeightedRoundRobin lb;
lb.AddServer("192.168.1.10", 1);
lb.AddServer("192.168.1.11", 2);
lb.AddServer("192.168.1.12", 3);
lb.AddServer("192.168.1.13", 4);
lb.AddServer("192.168.1.14", 10);
for(int i = 0; i < 1000000; i++)
{
lb.NextRequest();
}
lb.PrintStats();
}
/*
Server Hit stats with random:
192.168.1.13: 200448
192.168.1.10: 199584
192.168.1.14: 199799
192.168.1.11: 199495
192.168.1.12: 200674
*/
void TestRandom()
{
LoadBalanceRandom lb;
lb.AddServer("192.168.1.10");
lb.AddServer("192.168.1.11");
lb.AddServer("192.168.1.12");
lb.AddServer("192.168.1.13");
lb.AddServer("192.168.1.14");
for(int i = 0; i < 1000000; i++)
{
lb.NextRequest();
}
lb.PrintStats();
}
/*
Server Hit stats with weighted random:
192.168.1.13: 200290
192.168.1.12: 149612
192.168.1.11: 100060
192.168.1.10: 50291
192.168.1.14: 499747
*/
void TestWeightedRandom()
{
LoadBalanceWeightedRandom lb;
lb.AddServer("192.168.1.10", 1);
lb.AddServer("192.168.1.11", 2);
lb.AddServer("192.168.1.12", 3);
lb.AddServer("192.168.1.13", 4);
lb.AddServer("192.168.1.14", 10);
for(int i = 0; i < 1000000; i++)
{
lb.NextRequest();
}
lb.PrintStats();
}
/*
Server Hit stats with Consistent Hashing:
192.168.1.13: 200693
192.168.1.14: 140848
192.168.1.11: 180680
192.168.1.12: 265031
192.168.1.10: 212748
Server Hit stats with Consistent Hashing:
192.168.1.14: 205757
192.168.1.10: 292805
192.168.1.11: 241764
192.168.1.13: 259674
*/
void TestConsistentHashing()
{
LoadBalanceConsistentHashing lb;
lb.AddServer("192.168.1.10");
lb.AddServer("192.168.1.11");
lb.AddServer("192.168.1.12");
lb.AddServer("192.168.1.13");
lb.AddServer("192.168.1.14");
for(int i = 0; i < 1000000; i++)
{
lb.NextRequest();
}
lb.PrintStats();
// Delete one server, and try again
lb.DeleteServer("192.168.1.12");
lb.ResetStats();
for(int i = 0; i < 1000000; i++)
{
lb.NextRequest();
}
lb.PrintStats();
}
int main()
{
TestRoundRobin();
TestWeightedRoundRobin();
TestRandom();
TestWeightedRandom();
TestConsistentHashing();
return 0;
}