1 package org.apache.archiva.redback.tests;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
21 import junit.framework.TestCase;
22 import org.apache.archiva.redback.rbac.Permission;
23 import org.apache.archiva.redback.rbac.RBACManager;
24 import org.apache.archiva.redback.rbac.RbacManagerException;
25 import org.apache.archiva.redback.rbac.Resource;
26 import org.apache.archiva.redback.rbac.Role;
27 import org.apache.archiva.redback.rbac.UserAssignment;
28 import org.apache.archiva.redback.rbac.Operation;
29 import org.apache.archiva.redback.tests.utils.RBACDefaults;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.springframework.test.context.ContextConfiguration;
33 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35 import java.util.Collections;
37 @RunWith( SpringJUnit4ClassRunner.class )
38 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
39 public class AbstractRbacManagerPerformanceTestCase
42 private RBACManager rbacManager;
44 private RBACDefaults rbacDefaults;
46 public void setRbacManager( RBACManager store )
48 this.rbacManager = store;
49 rbacDefaults = new RBACDefaults( rbacManager );
58 public void tearDown()
64 private Role getDeveloperRole()
65 throws RbacManagerException
67 Role role = rbacManager.createRole( "DEVELOPER" );
68 role.setAssignable( true );
70 Permission perm = rbacManager.createPermission( "EDIT_MY_USER", "EDIT", "User:Self" );
72 role.addPermission( perm );
77 private Role getSuperDeveloperRole()
79 Role role = rbacManager.createRole( "SUPER_DEVELOPER" );
80 role.setAssignable( true );
85 private static final int ITERATIONS = 10000;
87 private static final int ONESECOND = 1000;
89 public void assertPerformance( String msg, long startTime, long endTime, int iterations, double threshold )
91 long elapsed = endTime - startTime;
92 double ratio = (double) elapsed / (double) ONESECOND; // ratio of time to 1 second.
93 double opsPerSecond = (double) iterations / ratio;
95 System.out.println( "Performance " + msg + ": " + opsPerSecond + " operations per second. (effective)" );
97 if ( opsPerSecond < threshold )
101 StringBuilder stats = new StringBuilder();
103 stats.append( "Stats on " ).append( msg );
104 stats.append( "\nStart Time (ms): " ).append( Long.toString( startTime ) );
105 stats.append( "\nEnd Time (ms) : " ).append( Long.toString( endTime ) );
106 stats.append( "\nElapsed (ms) : " ).append( Long.toString( elapsed ) );
107 stats.append( "\nRatio : " ).append( Double.toString( ratio ) );
108 stats.append( "\nOps per second : " ).append( Double.toString( opsPerSecond ) );
110 System.out.println( stats.toString() );
112 fail( "Performance Error: " + msg + " expecting greater than [" + threshold + "], actual [" + opsPerSecond
118 public void testPerformanceResource()
119 throws RbacManagerException
121 assertNotNull( rbacManager );
122 rbacManager.eraseDatabase();
124 Resource resource = rbacManager.createResource( "foo" );
125 Resource resource2 = rbacManager.createResource( "bar" );
127 assertNotNull( resource );
129 Resource added = rbacManager.saveResource( resource );
130 assertNotNull( added );
131 Resource added2 = rbacManager.saveResource( resource2 );
132 assertNotNull( added2 );
134 assertEquals( 2, rbacManager.getAllResources().size() );
136 String resFooId = resource.getIdentifier();
137 String resBarId = resource2.getIdentifier();
138 long startTime = System.currentTimeMillis();
140 for ( int i = 0; i <= ITERATIONS; i++ )
142 Resource resFoo = rbacManager.getResource( resFooId );
143 Resource resBar = rbacManager.getResource( resBarId );
145 assertNotNull( resFoo );
146 assertNotNull( resBar );
148 assertEquals( "foo", resFoo.getIdentifier() );
149 assertEquals( "bar", resBar.getIdentifier() );
152 long endTime = System.currentTimeMillis();
154 assertPerformance( "Resource", startTime, endTime, ITERATIONS, 500.0 );
158 public void testPerformanceUserAssignment()
159 throws RbacManagerException
161 RBACManager manager = rbacManager;
163 rbacManager.eraseDatabase();
165 Role devRole = getDeveloperRole();
166 Role devPlusRole = getSuperDeveloperRole();
167 devPlusRole.setChildRoleNames( Collections.singletonList( devRole.getName() ) );
168 devRole = manager.saveRole( devRole );
169 devPlusRole = manager.saveRole( devPlusRole );
171 // Setup User / Assignment with 1 role.
172 String username = "bob";
173 UserAssignment assignment = manager.createUserAssignment( username );
174 assignment.addRoleName( devRole );
175 assignment = manager.saveUserAssignment( assignment );
177 assertEquals( 1, manager.getAllUserAssignments().size() );
178 assertEquals( "should be only one role assigned", 1, manager.getAssignedRoles( assignment.getPrincipal() )
180 assertEquals( "should be one role left to assign", 1, manager.getUnassignedRoles( assignment.getPrincipal() )
182 assertEquals( 2, manager.getAllRoles().size() );
184 // assign the same role again to the same user
185 assignment.addRoleName( devRole.getName() );
186 manager.saveUserAssignment( assignment );
188 // we certainly shouldn't have 2 roles here now
189 assertEquals( 1, assignment.getRoleNames().size() );
191 String bobId = assignment.getPrincipal();
195 devPlusRole.setChildRoleNames( Collections.singletonList( devRole.getName() ) );
196 devRole = manager.saveRole( devRole );
197 manager.saveRole( devPlusRole );
199 assignment = manager.createUserAssignment( username );
200 assignment.addRoleName( devRole );
201 assignment = manager.saveUserAssignment( assignment );
203 assertEquals( 2, manager.getAllUserAssignments().size() );
204 assertEquals( "should be only one role assigned", 1, manager.getAssignedRoles( assignment.getPrincipal() )
206 assertEquals( "should be one role left to assign", 1, manager.getUnassignedRoles( assignment.getPrincipal() )
208 assertEquals( 2, manager.getAllRoles().size() );
210 // assign the same role again to the same user
211 assignment.addRoleName( devRole.getName() );
212 manager.saveUserAssignment( assignment );
214 // we certainly shouldn't have 2 roles here now
215 assertEquals( 1, assignment.getRoleNames().size() );
217 String janetId = assignment.getPrincipal();
219 long startTime = System.currentTimeMillis();
221 for ( int i = 0; i <= ITERATIONS; i++ )
223 UserAssignment uaBob = rbacManager.getUserAssignment( bobId );
224 UserAssignment uaJanet = rbacManager.getUserAssignment( janetId );
226 assertNotNull( uaBob );
227 assertNotNull( uaJanet );
229 assertEquals( "bob", uaBob.getPrincipal() );
230 assertEquals( "janet", uaJanet.getPrincipal() );
233 long endTime = System.currentTimeMillis();
234 assertPerformance( "UserAssignment", startTime, endTime, ITERATIONS, 350.0 );
238 public void testPerformanceRoles()
239 throws RbacManagerException
241 rbacDefaults.createDefaults();
243 String roleIdSysAdmin = "System Administrator";
244 String roleIdUserAdmin = "User Administrator";
246 long startTime = System.currentTimeMillis();
248 for ( int i = 0; i <= ITERATIONS; i++ )
250 Role roleSysAdmin = rbacManager.getRole( roleIdSysAdmin );
251 Role roleUserAdmin = rbacManager.getRole( roleIdUserAdmin );
253 assertNotNull( roleSysAdmin );
254 assertNotNull( roleUserAdmin );
256 assertEquals( roleIdSysAdmin, roleSysAdmin.getName() );
257 assertEquals( roleIdUserAdmin, roleUserAdmin.getName() );
260 long endTime = System.currentTimeMillis();
262 assertPerformance( "Roles", startTime, endTime, ITERATIONS, 130 );
266 public void testPerformancePermissions()
267 throws RbacManagerException
269 rbacDefaults.createDefaults();
271 String permIdRunIndexer = "Run Indexer";
272 String permIdAddRepo = "Add Repository";
274 long startTime = System.currentTimeMillis();
276 for ( int i = 0; i <= ITERATIONS; i++ )
278 Permission permRunIndex = rbacManager.getPermission( permIdRunIndexer );
279 Permission permAddRepo = rbacManager.getPermission( permIdAddRepo );
281 assertNotNull( permRunIndex );
282 assertNotNull( permAddRepo );
284 assertEquals( permIdRunIndexer, permRunIndex.getName() );
285 assertEquals( permIdAddRepo, permAddRepo.getName() );
288 long endTime = System.currentTimeMillis();
290 assertPerformance( "Permissions", startTime, endTime, ITERATIONS, 350 );
294 public void testPerformanceOperations()
295 throws RbacManagerException
297 rbacDefaults.createDefaults();
299 String operIdEditRepo = "edit-repository";
300 String operIdDelRepo = "delete-repository";
302 long startTime = System.currentTimeMillis();
304 for ( int i = 0; i <= ITERATIONS; i++ )
306 Operation operEditRepo = rbacManager.getOperation( operIdEditRepo );
307 Operation operDelRepo = rbacManager.getOperation( operIdDelRepo );
309 assertNotNull( operEditRepo );
310 assertNotNull( operDelRepo );
312 assertEquals( operIdEditRepo, operEditRepo.getName() );
313 assertEquals( operIdDelRepo, operDelRepo.getName() );
316 long endTime = System.currentTimeMillis();
318 assertPerformance( "Operations", startTime, endTime, ITERATIONS, 500 );