-
Notifications
You must be signed in to change notification settings - Fork 188
/
MidScaleRepoTest.java
246 lines (224 loc) · 10.9 KB
/
MidScaleRepoTest.java
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (C) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.repo.sql;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.*;
import org.javasimon.Split;
import org.javasimon.Stopwatch;
import org.jetbrains.annotations.NotNull;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.MiscSchemaUtil;
import com.evolveum.midpoint.tools.testng.PerformanceTestClassMixin;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
/**
* The test is not part of automatically run tests (it is not mentioned in suite XMLs).
* This tests creates data in the repository and then tries various queries.
* Data doesn't need to be business-realistic, full object representation can be dummy.
*/
@ContextConfiguration(locations = { "../../../../../ctx-test.xml" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class MidScaleRepoTest extends BaseSQLRepoTest
implements PerformanceTestClassMixin {
public static final int RESOURCE_COUNT = 10;
public static final int BASE_USER_COUNT = 1000;
public static final int MORE_USER_COUNT = 2000;
public static final int PEAK_USER_COUNT = 1000;
public static final int FIND_COUNT = 1000;
private static final Random RND = new Random();
// maps name -> oid
private final Map<String, String> resources = new LinkedHashMap<>();
private final Map<String, String> users = new LinkedHashMap<>();
private final List<String> memInfo = new ArrayList<>();
@BeforeMethod
public void reportBeforeTest() {
memInfo.add(String.format("%-40.40s before: %,15d",
contextName(), Runtime.getRuntime().totalMemory()));
queryListener.clear();
}
@AfterMethod
public void reportAfterTest() {
memInfo.add(String.format("%-40.40s after: %,15d",
contextName(), Runtime.getRuntime().totalMemory()));
}
@Test
public void test010InitResources() throws ObjectAlreadyExistsException, SchemaException {
OperationResult operationResult = createOperationResult();
Stopwatch stopwatch = stopwatch("resource.add", "Repository addObject(resource)");
for (int resourceIndex = 1; resourceIndex <= RESOURCE_COUNT; resourceIndex++) {
ResourceType resourceType = new ResourceType(prismContext);
String name = String.format("resource-%03d", resourceIndex);
resourceType.setName(PolyStringType.fromOrig(name));
if (resourceIndex == RESOURCE_COUNT) {
queryListener.start();
}
try (Split ignored = stopwatch.start()) {
repositoryService.addObject(resourceType.asPrismObject(), null, operationResult);
}
resources.put(name, resourceType.getOid());
}
queryListener.dumpAndStop();
}
@Test
public void test020AddBaseUsers() throws ObjectAlreadyExistsException, SchemaException {
OperationResult operationResult = createOperationResult();
Stopwatch stopwatch = stopwatch("user.add", "Repository addObject(user) - 1st batch");
for (int userIndex = 1; userIndex <= BASE_USER_COUNT; userIndex++) {
UserType userType = new UserType(prismContext);
String name = String.format("user-%07d", userIndex);
userType.setName(PolyStringType.fromOrig(name));
if (userIndex == BASE_USER_COUNT) {
queryListener.start();
}
try (Split ignored = stopwatch.start()) {
repositoryService.addObject(userType.asPrismObject(), null, operationResult);
}
users.put(name, userType.getOid());
}
queryListener.dumpAndStop();
}
@Test
public void test030AddBaseShadows() throws ObjectAlreadyExistsException, SchemaException {
OperationResult operationResult = createOperationResult();
Stopwatch stopwatch = stopwatch("shadow.add", "Repository addObject(shadow) - 1st batch");
for (int userIndex = 1; userIndex <= BASE_USER_COUNT; userIndex++) {
for (Map.Entry<String, String> resourceEntry : resources.entrySet()) {
String name = String.format("shadow-%07d-at-%s", userIndex, resourceEntry.getKey());
ShadowType shadowType = createShadow(name, resourceEntry.getValue());
// for the last user, but only once for a single resource
if (userIndex == BASE_USER_COUNT && queryListener.hasNoEntries()) {
queryListener.start();
}
try (Split ignored = stopwatch.start()) {
repositoryService.addObject(shadowType.asPrismObject(), null, operationResult);
}
if (queryListener.isStarted()) {
// stop does not clear entries, so it will not be started again
queryListener.stop();
}
}
}
queryListener.dumpAndStop();
}
@NotNull
private ShadowType createShadow(String shadowName, String resourceOid) {
ShadowType shadowType = new ShadowType(prismContext);
shadowType.setName(PolyStringType.fromOrig(shadowName));
shadowType.setResourceRef(MiscSchemaUtil.createObjectReference(
resourceOid, ResourceType.COMPLEX_TYPE));
return shadowType;
}
@Test
public void test110GetUser() throws SchemaException, ObjectNotFoundException {
OperationResult operationResult = createOperationResult();
Stopwatch stopwatch = stopwatch("user.get1", "Repository getObject() -> user, 1st test");
for (int i = 1; i <= FIND_COUNT; i++) {
String randomName = String.format("user-%07d", RND.nextInt(BASE_USER_COUNT) + 1);
if (i == FIND_COUNT) {
queryListener.start();
}
try (Split ignored = stopwatch.start()) {
assertThat(repositoryService.getObject(
UserType.class, users.get(randomName), null, operationResult))
.isNotNull();
}
}
queryListener.dumpAndStop();
}
@Test
public void test210AddMoreUsers() throws ObjectAlreadyExistsException, SchemaException {
OperationResult operationResult = createOperationResult();
Stopwatch stopwatch = stopwatch("user.addMore", "Repository addObject(user) - 2nd batch");
for (int userIndex = 1; userIndex <= MORE_USER_COUNT; userIndex++) {
UserType userType = new UserType(prismContext);
String name = String.format("user-more-%07d", userIndex);
userType.setName(PolyStringType.fromOrig(name));
if (userIndex == MORE_USER_COUNT) {
queryListener.start();
}
try (Split ignored = stopwatch.start()) {
repositoryService.addObject(userType.asPrismObject(), null, operationResult);
}
users.put(name, userType.getOid());
}
queryListener.dumpAndStop();
}
@Test
public void test230AddMoreShadows() throws ObjectAlreadyExistsException, SchemaException {
OperationResult operationResult = createOperationResult();
Stopwatch stopwatch = stopwatch("shadow.addMore", "Repository addObject(shadow) - 2nd batch");
for (int userIndex = 1; userIndex <= MORE_USER_COUNT; userIndex++) {
for (Map.Entry<String, String> resourceEntry : resources.entrySet()) {
String name = String.format("shadow-more-%07d-at-%s", userIndex, resourceEntry.getKey());
ShadowType shadowType = createShadow(name, resourceEntry.getValue());
// for the last user, but only once for a single resource
if (userIndex == MORE_USER_COUNT && queryListener.hasNoEntries()) {
queryListener.start();
}
try (Split ignored = stopwatch.start()) {
repositoryService.addObject(shadowType.asPrismObject(), null, operationResult);
}
if (queryListener.isStarted()) {
queryListener.stop();
}
}
}
queryListener.dumpAndStop();
}
@Test
public void test610PeakMoreUsers() throws ObjectAlreadyExistsException, SchemaException {
OperationResult operationResult = createOperationResult();
Stopwatch stopwatch = stopwatch("user.addPeak", "Repository addObject(user) - 3rd batch");
for (int userIndex = 1; userIndex <= PEAK_USER_COUNT; userIndex++) {
UserType userType = new UserType(prismContext);
String name = String.format("user-peak-%07d", userIndex);
userType.setName(PolyStringType.fromOrig(name));
if (userIndex == PEAK_USER_COUNT) {
queryListener.start();
}
try (Split ignored = stopwatch.start()) {
repositoryService.addObject(userType.asPrismObject(), null, operationResult);
}
users.put(name, userType.getOid());
}
queryListener.dumpAndStop();
}
@Test
public void test710GetUserMore() throws SchemaException, ObjectNotFoundException {
OperationResult operationResult = createOperationResult();
Stopwatch stopwatch = stopwatch("user.get2", "Repository getObject() -> user, 2nd test");
for (int i = 1; i <= FIND_COUNT; i++) {
String randomName = String.format("user-more-%07d", RND.nextInt(MORE_USER_COUNT) + 1);
if (i == FIND_COUNT) {
queryListener.start();
}
try (Split ignored = stopwatch.start()) {
assertThat(repositoryService.getObject(
UserType.class, users.get(randomName), null, operationResult))
.isNotNull();
}
}
queryListener.dumpAndStop();
}
@Test
public void test900PrintObjects() {
System.out.println("resources = " + resources.size());
System.out.println("users = " + users.size());
// WIP: memInfo is not serious yet
memInfo.forEach(System.out::println);
}
}