1 13 14 package se.anatom.ejbca.ca.sign; 15 16 import java.rmi.RemoteException ; 17 import java.security.KeyPair ; 18 import java.security.KeyPairGenerator ; 19 import java.security.cert.X509Certificate ; 20 import java.security.interfaces.RSAPrivateKey ; 21 22 import javax.ejb.DuplicateKeyException ; 23 import javax.naming.Context ; 24 import javax.naming.NamingException ; 25 26 import junit.framework.TestCase; 27 28 import org.apache.log4j.Logger; 29 import org.ejbca.core.ejb.ca.sign.ISignSessionHome; 30 import org.ejbca.core.ejb.ca.sign.ISignSessionRemote; 31 import org.ejbca.core.ejb.ra.IUserAdminSessionHome; 32 import org.ejbca.core.ejb.ra.IUserAdminSessionRemote; 33 import org.ejbca.core.model.SecConst; 34 import org.ejbca.core.model.log.Admin; 35 import org.ejbca.core.model.ra.UserDataConstants; 36 import org.ejbca.util.CertTools; 37 38 39 46 public class TestSignLotsOfCerts extends TestCase { 47 private static Logger log = Logger.getLogger(TestSignLotsOfCerts.class); 48 public static Context ctx; 49 private static IUserAdminSessionRemote usersession; 50 public static KeyPair keys; 51 private static int caid = 0; 52 public Admin admin; 53 54 59 public TestSignLotsOfCerts(String name) { 60 super(name); 61 } 62 63 protected void setUp() throws Exception { 64 log.debug(">setUp()"); 65 66 CertTools.installBCProvider(); 68 69 admin = new Admin(Admin.TYPE_BATCHCOMMANDLINE_USER); 70 71 caid = -1688117755; 73 ctx = getInitialContext(); 74 Object obj = ctx.lookup("UserAdminSession"); 75 IUserAdminSessionHome userhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, IUserAdminSessionHome.class); 76 usersession = userhome.create(); 77 78 keys = genKeys(); 79 80 log.debug("<setUp()"); 81 } 82 83 protected void tearDown() throws Exception { 84 } 85 86 private Context getInitialContext() throws NamingException { 87 log.debug(">getInitialContext"); 88 Context ctx = new javax.naming.InitialContext (); 89 log.debug("<getInitialContext"); 90 return ctx; 91 } 92 93 100 private static KeyPair genKeys() throws Exception { 101 KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA", "BC"); 102 keygen.initialize(1024); 103 log.debug("Generating keys, please wait..."); 104 KeyPair rsaKeys = keygen.generateKeyPair(); 105 log.debug("Generated " + rsaKeys.getPrivate().getAlgorithm() + " keys with length" + 106 ((RSAPrivateKey ) rsaKeys.getPrivate()).getModulus().bitLength()); 107 108 return rsaKeys; 109 } 111 private void newUser(String post) throws Exception { 112 boolean userExists = false; 114 try { 115 usersession.addUser(admin,"performancefoo"+post,"foo123","C=SE,O=AnaTom,OU=Performance Test,CN=performancefoo",null,"performancefoo@foo.se",false,SecConst.EMPTY_ENDENTITYPROFILE,SecConst.CERTPROFILE_FIXED_ENDUSER,SecConst.USER_ENDUSER,SecConst.TOKEN_SOFT_PEM,0,caid); 116 log.debug("created user: performancefoo"+post+", foo123, C=SE, O=AnaTom, OU=Performance Test,CN=performancefoo"); 117 } catch (RemoteException re) { 118 if (re.detail instanceof DuplicateKeyException ) { 119 userExists = true; 120 } 121 } catch (DuplicateKeyException dke) { 122 userExists = true; 123 } 124 if (userExists) { 125 log.info("User performancefoo already exists, resetting status."); 126 usersession.setUserStatus(admin,"performancefoo"+post,UserDataConstants.STATUS_NEW); 127 log.debug("Reset status to NEW"); 128 } 129 130 } 131 136 public void test01CreateNewUser() throws Exception { 137 log.debug(">test01CreateNewUser()"); 138 newUser("no1"); 139 newUser("no2"); 140 newUser("no3"); 141 newUser("no4"); 142 newUser("no5"); 143 newUser("no6"); 144 newUser("no7"); 145 newUser("no8"); 146 newUser("no9"); 147 newUser("no10"); 148 log.debug("<test01CreateNewUser()"); 149 } 150 151 156 public void test03SignLotsOfCerts() throws Exception { 157 log.debug(">test03SignLotsOfCerts()"); 158 159 long before = System.currentTimeMillis(); 160 Thread no1 = new Thread (new SignTester(),"no1"); 161 Thread no2 = new Thread (new SignTester(),"no2"); 162 Thread no3 = new Thread (new SignTester(),"no3"); 163 Thread no4 = new Thread (new SignTester(),"no4"); 164 Thread no5 = new Thread (new SignTester(),"no5"); 165 Thread no6 = new Thread (new SignTester(),"no6"); 166 Thread no7 = new Thread (new SignTester(),"no7"); 167 Thread no8 = new Thread (new SignTester(),"no8"); 168 Thread no9 = new Thread (new SignTester(),"no9"); 169 Thread no10 = new Thread (new SignTester(),"no10"); 170 no1.start(); 171 System.out.println("Started no1"); 172 no2.start(); 173 System.out.println("Started no2"); 174 no3.start(); 175 System.out.println("Started no3"); 176 no4.start(); 177 System.out.println("Started no4"); 178 no5.start(); 179 System.out.println("Started no5"); 180 no6.start(); 181 System.out.println("Started no6"); 182 no7.start(); 183 System.out.println("Started no7"); 184 no8.start(); 185 System.out.println("Started no8"); 186 no9.start(); 187 System.out.println("Started no9"); 188 no10.start(); 189 System.out.println("Started no10"); 190 no1.join(); 191 no2.join(); 192 no3.join(); 193 no4.join(); 194 no5.join(); 195 no6.join(); 196 no7.join(); 197 no8.join(); 198 no9.join(); 199 no10.join(); 200 long after = System.currentTimeMillis(); 201 long diff = after - before; 202 System.out.println("All threads finished. Total time: "+diff); 203 log.debug("<test03SignLotsOfCerts()"); 207 } 208 209 private class SignTester implements Runnable { 210 public void run() { 211 try { 212 Object obj = ctx.lookup("RSASignSession"); 213 ISignSessionHome rsahome = (ISignSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, ISignSessionHome.class); 214 ISignSessionRemote rsaremote = rsahome.create(); 215 String user = "performancefoo"+Thread.currentThread().getName(); 216 long before = System.currentTimeMillis(); 217 for (int i = 0; i<1000;i++) { 218 X509Certificate cert = (X509Certificate ) rsaremote.createCertificate(admin, user, "foo123", keys.getPublic()); 220 assertNotNull("Misslyckades skapa cert", cert); 221 if ((i % 100) == 0) { 222 long mellantid = System.currentTimeMillis() - before; 223 System.out.println(Thread.currentThread().getName()+" har skapat "+i+", tid="+mellantid); 224 225 } 226 } 227 long after = System.currentTimeMillis(); 228 long diff = after - before; 229 System.out.println("Tidsåtgång ("+Thread.currentThread().getName()+"): "+diff); 230 } catch (Exception e) { 231 e.printStackTrace(); 233 } 234 } 235 } 236 } 237 | Popular Tags |