MimeMessage.writeTo() takes a very long time for a modified message.
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gary_Taylor
Posted On:   Wednesday, October 9, 2002 04:02 AM

I have encountered a performance problem with the writeTo() method. Calling this method on a large message that has been modified takes a very long time, compared to an unmodified message. Trying to write the message out over SAMBA does exacerbate the problem in terms of magnifying the delay taken by the writeTo() method, but a significant delay is also visible when writing to a native Windows disk. The following code, which demonstrates the problem, requires a MIME message which has a multipart/mixed at the top, with the first subpart as a text/plain, and the second subpart as a binary attachment (eg. a standard "Here's an attachment" email.) The larger the binary attachment, the better it demonstrates the    More>>


I have encountered a performance problem with the writeTo() method. Calling this method on a large message that has been modified takes a very long time, compared to an unmodified message. Trying to write the message out over SAMBA does exacerbate the problem in terms of magnifying the delay taken by the writeTo() method, but a significant delay is also visible when writing to a native Windows disk.



The following code, which demonstrates the problem, requires a MIME message which has a multipart/mixed at the top, with the first subpart as a text/plain, and the second subpart as a binary attachment (eg. a standard "Here's an attachment" email.) The larger the binary attachment, the better it demonstrates the delay (I've been using approx 700Kb.)



If you run it (after making sure the filepaths are correct) as is, then it just reads the MIME file, builds a MimeMessage object, copies it, and writes it out again. It also displays how many milliseconds the writeTo() took.



Now edit the code and uncomment the marked block, so now the code reads the MIME file, builds a MimeMessage, copies it, changes the first subpart, then writes it out again. This time the time taken to perform the writeTo() is quite a bit longer even though the size of the message has not changed significantly.



In my tests on P500 running Windows 2000, the first test takes an average of 30ms for a 760Kb MIME file, and the second test takes an average of 7000ms !



Even if further encoding/decoding is going on under the covers because the message has been modified, it still appears to take a lot longer than expected - even the code which builds the MimeMessage object from the original source MIME file on disk doesn't take that long to parse it.



I'd really like to know why writeTo() takes so long in the second case. I can't see anything obvious that I've done wrong in the code or that I'm missing ... but I probably have :-(



The code sample is shown below:


			
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
import java.io.*;

public class mimetst3
{
public static void main(String[] args)
{
try
{
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
session.setDebugOut(null);

// Load a mime message from disk
MimeMessage m = new MimeMessage(session, new FileInputStream("c:\me\BIGMIME"));

// Create Copy
MimeMessage m2 = new MimeMessage(m);

// Uncomment block below to show delay
/*
// 1. Change text/plain part --------------
MimeMultipart mp = (MimeMultipart)m2.getContent();
BodyPart bp = mp.getBodyPart(0);
String foobar = "[FOO-BAR]";
DataHandler dh = new DataHandler(foobar, "text/plain");
bp.setDataHandler(dh);
m2.setContent(mp);
m2.saveChanges();
// ----------------------------------------
*/

long t0 = System.currentTimeMillis();
m2.writeTo(new FileOutputStream("c:\me\BIGMIME-OUT"));
long t1 = System.currentTimeMillis();

long runTime = t1 - t0;
System.out.println("Runtime: " + runTime + " ms");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
   <<Less
About | Sitemap | Contact