1 17 18 package org.sape.carbon.services.email.test; 19 20 21 import java.util.Properties ; 22 23 import javax.mail.Message ; 24 import javax.mail.Session ; 25 import javax.mail.Transport ; 26 import javax.mail.internet.InternetAddress ; 27 import javax.mail.internet.MimeMessage ; 28 29 import org.sape.carbon.core.component.Lookup; 30 import org.sape.carbon.core.config.Config; 31 import org.sape.carbon.services.email.MailConfiguration; 32 import org.sape.carbon.services.email.MailService; 33 import org.sape.carbon.services.email.util.MailContentTypeEnum; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 56 public class EmailPerformanceTest extends EmailServiceTest { 57 58 61 private Log log = LogFactory.getLog(this.getClass()); 62 63 public static final int ITERATIONS = 20; 65 66 public EmailPerformanceTest(String name) { 67 super(name); 68 } 69 70 73 public void testPerformanceWithHoldConnectionFalse() { 74 75 if (log.isInfoEnabled()) { 76 log.info("Starting sending " + EmailPerformanceTest.ITERATIONS 77 + " mails " + "with hold connection as true."); 78 } 79 80 MailService mail = (MailService) Lookup.getInstance(). 81 fetchComponent("/email/test/testEmail"); 82 83 long fullTime = testSendingMailsToSingleRecipient( 84 MailContentTypeEnum.PLAIN_TEXT, EmailServiceTest.MESSAGE_CONTENTS, 85 mail, EmailPerformanceTest.ITERATIONS); 86 87 if (log.isInfoEnabled()) { 88 log.info( 89 "Time to send a single mail with hold conncection false = " 90 + fullTime); 91 } 92 93 long timeWithJavaMail = testSendingMailsUsingJavaMail( 95 "/email/test/testEmail"); 96 97 if (log.isInfoEnabled()) { 99 log.info("Ratio of time to send a email using our component " + 100 "hold connection = false) TO the time to send a email by " + 101 "directly using Java Mail(hold connection = false) is " + 102 (float)(fullTime/timeWithJavaMail)); 103 } 104 } 105 106 107 110 public void testPerformanceWithHoldConnectionTrue() { 111 112 if (log.isInfoEnabled()) { 113 log.info("Starting sending " + EmailPerformanceTest.ITERATIONS 114 + " mails " + "with hold connection as true."); 115 } 116 117 MailService mail = (MailService) Lookup.getInstance(). 118 fetchComponent("/email/test/testHoldConnectionTrue"); 119 120 long fullTime = testSendingMailsToSingleRecipient( 121 MailContentTypeEnum.PLAIN_TEXT, EmailServiceTest.MESSAGE_CONTENTS, 122 mail, EmailPerformanceTest.ITERATIONS); 123 124 if (log.isInfoEnabled()) { 125 log.info( 126 "Time to send a single mail with hold conncection true = " 127 + fullTime); 128 } 129 130 long timeWithJavaMail = testSendingMailsUsingJavaMail( 132 "/email/test/testHoldConnectionTrue"); 133 134 if (log.isInfoEnabled()) { 136 log.info("Ratio of time to send a email using our component" + 137 "(hold connection = true) TO the time to send a email by " + 138 "directly using Java Mail(hold connection = true) is " + 139 (float)(fullTime/timeWithJavaMail)); 140 } 141 } 142 143 144 150 public long testSendingMailsUsingJavaMail(String configFile) { 151 152 if (log.isInfoEnabled()) { 153 log.info("Starting sending " + EmailPerformanceTest.ITERATIONS 154 + " mails " + "by directly calling methods on JavaMail."); 155 } 156 157 long startTime, endTime, averageTime = 0; 158 159 MailConfiguration config = (MailConfiguration) Config.getInstance(). 160 fetchConfiguration(configFile); 161 162 startTime = System.currentTimeMillis(); 163 164 boolean holdConnection = config.getHoldConnection(); 165 166 Properties props = new Properties (); 167 props.put("mail.transport.protocol", "smtp"); 168 props.put("mail.host", config.getSmtpHost()); 169 170 try { 171 Session session = Session.getInstance(props, null); 172 173 Transport transport = session.getTransport(); 174 175 for(int i = 0; i < EmailPerformanceTest.ITERATIONS; i++) { 176 177 MimeMessage message = new MimeMessage (session); 178 179 message.addRecipient(Message.RecipientType.TO, 180 new InternetAddress (EmailServiceTest.TO_EMAIL, 181 EmailServiceTest.TO_PERSONAL)); 182 183 message.setFrom(new InternetAddress (EmailServiceTest.FROM_EMAIL, 184 EmailServiceTest.FROM_PERSONAL)); 185 186 message.setSubject(EmailServiceTest.SUBJECT); 187 188 message.setText("Simple email from Standalone program"); 189 190 if(holdConnection) { 191 if(!transport.isConnected()) { 193 transport.connect(); 194 } 195 196 transport.sendMessage(message, 198 message.getAllRecipients()); 199 } 200 else { 201 transport.connect(); 203 transport.sendMessage(message, 204 message.getAllRecipients()); 205 transport.close(); 206 } 207 } 208 209 endTime = System.currentTimeMillis(); 210 211 averageTime = (endTime - startTime)/EmailPerformanceTest.ITERATIONS; 212 213 if (log.isInfoEnabled()) { 214 log.info("Time to send single mail by directly using JavaMail " 215 + averageTime); 216 } 217 218 } catch(Exception e) { 219 fail("Failed to send mails using standalone application. "); 220 } 221 222 return averageTime; 223 } 224 225 226 229 public static Test suite() { 230 TestSuite masterSuite = new TestSuite(); 231 masterSuite.addTest(new EmailPerformanceTest( 232 "testPerformanceWithHoldConnectionFalse")); 233 masterSuite.addTest(new EmailPerformanceTest( 234 "testPerformanceWithHoldConnectionTrue")); 235 return masterSuite; 236 } 237 } 238 | Popular Tags |