KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > email > test > EmailPerformanceTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.email.test;
19
20
21 import java.util.Properties JavaDoc;
22
23 import javax.mail.Message JavaDoc;
24 import javax.mail.Session JavaDoc;
25 import javax.mail.Transport JavaDoc;
26 import javax.mail.internet.InternetAddress JavaDoc;
27 import javax.mail.internet.MimeMessage JavaDoc;
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 /**
42  * <p>Test harness for performance testing of Email Service.
43  * Performance testing is done with two scenarios, one with hold
44  * connection property as false and other with this property as true. Then the
45  * time is compared with the time measured by sending mails by directly using
46  * JavaMail. </p>
47  * </p>
48  *
49  * <p>Copyright 2002 Sapient</p>
50  * @stereotype test harness
51  * @since carbon 1.0
52  * @author $Author: dvoet $ $Date: 2003/05/05 21:21:29 $
53  * @version $Revision: 1.9 $
54  *
55  */

56 public class EmailPerformanceTest extends EmailServiceTest {
57
58     /**
59      * Provides a handle to Apache-commons logger
60      */

61     private Log log = LogFactory.getLog(this.getClass());
62
63     // No of emails to be send.
64
public static final int ITERATIONS = 20;
65
66     public EmailPerformanceTest(String JavaDoc name) {
67         super(name);
68     }
69
70     /**
71      * <p>Sends mails with hold connection property as false. </p>
72      */

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         // Now get the time taken to send the mails without using our component.
94
long timeWithJavaMail = testSendingMailsUsingJavaMail(
95             "/email/test/testEmail");
96
97         // Now log the difference.
98
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     /**
108      * <p>Sends mails with hold connection property as true. </p>
109      */

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         // Now get the time taken to send the mails without using our component.
131
long timeWithJavaMail = testSendingMailsUsingJavaMail(
132             "/email/test/testHoldConnectionTrue");
133
134         // Now log the difference.
135
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     /**
145      * <p>This test sends the mail without using our component. It is
146      * used to compare the time differences. </p>
147      *
148      * @return long The average time taken to send each email.
149      */

150     public long testSendingMailsUsingJavaMail(String JavaDoc 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 JavaDoc props = new Properties JavaDoc();
167         props.put("mail.transport.protocol", "smtp");
168         props.put("mail.host", config.getSmtpHost());
169
170         try {
171             Session JavaDoc session = Session.getInstance(props, null);
172
173             Transport JavaDoc transport = session.getTransport();
174
175             for(int i = 0; i < EmailPerformanceTest.ITERATIONS; i++) {
176
177                 MimeMessage JavaDoc message = new MimeMessage JavaDoc(session);
178
179                 message.addRecipient(Message.RecipientType.TO,
180                     new InternetAddress JavaDoc(EmailServiceTest.TO_EMAIL,
181                     EmailServiceTest.TO_PERSONAL));
182
183                 message.setFrom(new InternetAddress JavaDoc(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                     // Double check the connection.
192
if(!transport.isConnected()) {
193                         transport.connect();
194                     }
195
196                     // Send the message.
197
transport.sendMessage(message,
198                         message.getAllRecipients());
199                 }
200                 else {
201                      // Open connection, send and close.
202
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 JavaDoc e) {
219             fail("Failed to send mails using standalone application. ");
220         }
221
222         return averageTime;
223     }
224
225
226     /**
227      * Adds all tests into the master suite.
228      */

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