forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityGroupQueueTest.java
More file actions
219 lines (187 loc) · 7.26 KB
/
SecurityGroupQueueTest.java
File metadata and controls
219 lines (187 loc) · 7.26 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network.security;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.cloud.utils.Profiler;
import junit.framework.TestCase;
public class SecurityGroupQueueTest extends TestCase {
public final static SecurityGroupWorkQueue queue = new LocalSecurityGroupWorkQueue();
public static class Producer implements Runnable {
int _maxVmId = 0;
int _newWorkQueued=0;
Set<Long> vmIds = new HashSet<Long>();
public Producer(int maxVmId) {
this._maxVmId = maxVmId;
for (long i=1; i <= _maxVmId; i++) {
vmIds.add(i);
}
}
public void run() {
_newWorkQueued = queue.submitWorkForVms(vmIds);
}
public int getNewWork() {
return _newWorkQueued;
}
public int getTotalWork() {
return _maxVmId;
}
}
public static class Consumer implements Runnable {
private int _numJobsToDequeue = 0;
private int _numJobsDequeued = 0;
public Consumer(int numJobsToDequeu) {
this._numJobsToDequeue = numJobsToDequeu;
}
public void run() {
List<SecurityGroupWork> result = new ArrayList<SecurityGroupWork>();
try {
result = queue.getWork(_numJobsToDequeue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this._numJobsDequeued = result.size();
}
int getNumJobsToDequeue() {
return _numJobsToDequeue;
}
int getNumJobsDequeued() {
return _numJobsDequeued;
}
}
public void testNumJobsEqToNumVms1() {
queue.clear();
final int numProducers = 50;
Thread [] pThreads = new Thread[numProducers];
Producer [] producers = new Producer[numProducers];
int numProduced = 0;
for (int i=0; i < numProducers; i++) {
producers[i] = new Producer(i+1);
pThreads[i] = new Thread(producers[i]);
numProduced += i+1;
pThreads[i].start();
}
for (int i=0; i < numProducers ; i++) {
try {
pThreads[i].join();
} catch (InterruptedException ie){
ie.printStackTrace();
}
}
System.out.println("Num Vms= " + numProducers + " Queue size = " + queue.size());
assertEquals(numProducers, queue.size());
}
protected void testNumJobsEqToNumVms2(int numProducers, int maxVmId) {
queue.clear();
Thread [] pThreads = new Thread[numProducers];
Producer [] producers = new Producer[numProducers];
int numProduced = 0;
Profiler p = new Profiler();
p.start();
for (int i=0; i < numProducers; i++) {
producers[i] = new Producer(maxVmId);
pThreads[i] = new Thread(producers[i]);
numProduced += i+1;
pThreads[i].start();
}
for (int i=0; i < numProducers ; i++) {
try {
pThreads[i].join();
} catch (InterruptedException ie){
ie.printStackTrace();
}
}
p.stop();
System.out.println("Num Vms= " + maxVmId + " Queue size = " + queue.size() + " time=" + p.getDuration() + " ms");
assertEquals(maxVmId, queue.size());
}
public void testNumJobsEqToNumVms3() {
testNumJobsEqToNumVms2(50,20000);
testNumJobsEqToNumVms2(400,5000);
testNumJobsEqToNumVms2(1,1);
testNumJobsEqToNumVms2(1,1000000);
testNumJobsEqToNumVms2(1000,1);
}
protected void _testDequeueOneJob(final int numConsumers, final int numProducers, final int maxVmId) {
queue.clear();
Thread [] pThreads = new Thread[numProducers];
Thread [] cThreads = new Thread[numConsumers];
Consumer [] consumers = new Consumer[numConsumers];
Producer [] producers = new Producer[numProducers];
int numProduced = 0;
for (int i=0; i < numConsumers; i++) {
consumers[i] = new Consumer(1);
cThreads[i] = new Thread(consumers[i]);
cThreads[i].start();
}
for (int i=0; i < numProducers; i++) {
producers[i] = new Producer(maxVmId);
pThreads[i] = new Thread(producers[i]);
numProduced += maxVmId;
pThreads[i].start();
}
for (int i=0; i < numConsumers ; i++) {
try {
cThreads[i].join();
} catch (InterruptedException ie){
ie.printStackTrace();
}
}
for (int i=0; i < numProducers ; i++) {
try {
pThreads[i].join();
} catch (InterruptedException ie){
ie.printStackTrace();
}
}
int totalDequeued = 0;
for (int i=0; i < numConsumers; i++) {
//System.out.println("Consumer " + i + " ask to dequeue " + consumers[i].getNumJobsToDequeue() + ", dequeued " + consumers[i].getNumJobsDequeued());
totalDequeued += consumers[i].getNumJobsDequeued();
}
int totalQueued = 0;
for (int i=0; i < numProducers; i++) {
//System.out.println("Producer " + i + " ask to queue " + producers[i].getTotalWork() + ", queued " + producers[i].getNewWork());
totalQueued += producers[i].getNewWork();
}
System.out.println("Total jobs dequeued = " + totalDequeued + ", num queued=" + totalQueued + " queue current size=" + queue.size());
assertEquals(totalDequeued, numConsumers);
assertEquals(totalQueued - totalDequeued, queue.size());
}
public void testDequeueOneJobAgain() {
_testDequeueOneJob(10,10,1000);
int queueSize = queue.size();
Thread cThread = new Thread(new Consumer(1));
cThread.start();
try {
cThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertEquals(queue.size(), queueSize-1);
}
public void testDequeueOneJob() {
_testDequeueOneJob(10,10,1000);
_testDequeueOneJob(1,10,1000);
_testDequeueOneJob(10,1,1000);
_testDequeueOneJob(10,1,10);
}
}