I've just completed some performance fixes to the AccessLogValve.
My tests show that it's now more than 40% faster.

I found that it was creating far more Date objects than it needed to,
and formatting date strings too often as well.  I've optimized most
of that, which is what accounts for most of the performance gain,
plus I optimized the common log format logging as well (for another
small performance gain).

Here's a diff:

--- AccessLogValve.java    Wed Nov 29 11:31:38 2000
+++ AccessLogValve.java-new    Wed Nov 29 11:31:34 2000
@@ -65,7 +65,9 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
-import java.sql.Timestamp;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@@ -122,6 +124,7 @@
  * can be implemented.
  *
  * @author Craig R. McClanahan
+ * @author Jason Brittain
  * @version $Revision: 1.1 $ $Date: 2000/08/11 17:03:15 $
  */

@@ -151,7 +154,7 @@
     * The as-of date for the currently open log file, or a zero-length
     * string if there is no open log file.
     */
-    private String date = "";
+    private String dateStamp = "";


    /**
@@ -161,6 +164,14 @@


    /**
+     * If the current log pattern is the same as the common access log
+     * format pattern, then we'll set this variable to true and log in
+     * a more optimal and hard-coded way.
+     */
+    private boolean common = false;
+
+
+    /**
     * The descriptive information about this implementation.
     */
    protected static final String info =
@@ -194,6 +205,54 @@


    /**
+     * A date formatter to format a Date into a date in the format
+     * "yyyy-MM-dd".
+     */
+    private SimpleDateFormat dateFormatter = null;
+
+
+    /**
+     * A date formatter to format Dates into a day string in the format
+     * "dd".
+     */
+    private SimpleDateFormat dayFormatter = null;
+
+
+    /**
+     * A date formatter to format a Date into a month string in the format
+     * "MM".
+     */
+    private SimpleDateFormat monthFormatter = null;
+
+
+    /**
+     * A date formatter to format a Date into a year string in the format
+     * "yyyy".
+     */
+    private SimpleDateFormat yearFormatter = null;
+
+
+    /**
+     * A date formatter to format a Date into a time in the format
+     * "kk:mm:ss" (kk is a 24-hour representation of the hour).
+     */
+    private SimpleDateFormat timeFormatter = null;
+
+
+    /**
+     * The time zone relative to GMT.
+     */
+    private String timeZone = null;
+
+
+    /**
+     * The system time when we last updated the Date that this valve
+     * uses for log lines.
+     */
+    private Date currentDate = null;
+
+
+    /**
     * The string manager for this package.
     */
    private StringManager sm =
@@ -218,6 +277,12 @@
    private PrintWriter writer = null;


+    /**
+     * When formatting log lines, we often use strings like this one (" ").
+     */
+    private String space = " ";
+
+
    // ------------------------------------------------------------- 
Properties


@@ -276,6 +341,11 @@
        pattern = Constants.AccessLog.COMMON_PATTERN;
    this.pattern = pattern;

+    if (this.pattern.equals(Constants.AccessLog.COMMON_PATTERN))
+        common = true;
+    else
+        common = false;
+
    }


@@ -342,20 +412,80 @@
    // Pass this request on to the next valve in our pipeline
    invokeNext(request, response);

-    // Generate a message based on the defined pattern
    StringBuffer result = new StringBuffer();
-    boolean replace = false;
-    for (int i = 0; i < pattern.length(); i++) {
-        char ch = pattern.charAt(i);
-        if (replace) {
-        result.append(replace(ch, request, response));
-        replace = false;
-        } else if (ch == '%')
-        replace = true;
+
+    Date date = getDate();
+
+    // Check to see if we should log using the "common" access log pattern
+    if (common) {
+        String value = null;
+
+        ServletRequest req = request.getRequest();
+        HttpServletRequest hreq = null;
+        if (req instanceof HttpServletRequest)
+        hreq = (HttpServletRequest) req;
+
+        result.append(req.getRemoteHost());
+
+        result.append(" - ");
+       
+        if (hreq != null)
+        value = hreq.getRemoteUser();
+        if (value == null)
+        result.append("- ");
        else
-        result.append(ch);
+        {
+        result.append(value);
+        result.append(space);
+        }
+
+        result.append("[");
+        result.append(dayFormatter.format(date));            // Day
+        result.append('/');
+        result.append(lookup(monthFormatter.format(date))); // Month
+        result.append('/');
+        result.append(yearFormatter.format(date));            // Year
+        result.append(':');
+        result.append(timeFormatter.format(date));        // Time
+        result.append(space);
+        result.append(timeZone);                            // Time Zone
+        result.append("] \"");
+
+        result.append(hreq.getMethod());
+        result.append(space);
+        result.append(hreq.getRequestURI());
+        result.append(space);
+        result.append(hreq.getProtocol());
+        result.append("\" ");
+
+        result.append(((HttpResponse) response).getStatus());
+
+        result.append(space);
+
+
+        int length = response.getContentCount();
+        if (length <= 0)
+        value = "-";
+        else
+        value = "" + length;
+        result.append(value);
+    }
+    else {
+        // Generate a message based on the defined pattern
+        boolean replace = false;
+        for (int i = 0; i < pattern.length(); i++) {
+        char ch = pattern.charAt(i);
+        if (replace) {
+            result.append(replace(ch, date, request, response));
+            replace = false;
+        } else if (ch == '%')
+            replace = true;
+        else
+            result.append(ch);
+        }
    }
-    log(result.toString());
+
+    log(result.toString(), date);

    }

@@ -373,7 +503,7 @@
    writer.flush();
    writer.close();
    writer = null;
-    date = "";
+    dateStamp = "";

    }

@@ -383,26 +513,32 @@
     * has changed since the previous log call.
     *
     * @param message Message to be logged
+     * @param date the current Date object (so this method doesn't need to
+     *        create a new one)
     */
-    public void log(String message) {
+    public void log(String message, Date date) {

-    // Check for a change of date
-    Timestamp ts = new Timestamp(System.currentTimeMillis());
-    String tsString = ts.toString().substring(0, 19);
-    String tsDate = tsString.substring(0, 10);
-
-    // If the date has changed, switch log files
-    if (!date.equals(tsDate)) {
-        synchronized (this) {
-        close();
-        date = tsDate;
-        open();
+    // Only do a logfile switch check once a second, max.
+    if ((System.currentTimeMillis() - currentDate.getTime()) > 1000) {
+        // We need a new currentDate
+        currentDate = new Date();
+
+        // Check for a change of date
+        String tsDate = dateFormatter.format(currentDate);
+
+        // If the date has changed, switch log files
+        if (!dateStamp.equals(tsDate)) {
+        synchronized (this) {
+            close();
+            dateStamp = tsDate;
+            open();
+        }
        }
    }

    // Log this message
    if (writer != null) {
-        writer.println(message);
+          writer.println(message);
    }

    }
@@ -428,7 +564,7 @@


    /**
-     * Open the new log file for the date specified by <code>date</code>.
+     * Open the new log file for the date specified by 
<code>dateStamp</code>.
     */
    private void open() {

@@ -442,7 +578,7 @@
    // Open the current log file
    try {
        String pathname = dir.getAbsolutePath() + File.separator +
-        prefix + date + suffix;
+        prefix + dateStamp + suffix;
        writer = new PrintWriter(new FileWriter(pathname, true), true);
    } catch (IOException e) {
        writer = null;
@@ -455,10 +591,13 @@
     * Return the replacement text for the specified pattern character.
     *
     * @param pattern Pattern character identifying the desired text
+     * @param date the current Date so that this method doesn't need to
+     *        create one
     * @param request Request being processed
     * @param response Response being processed
     */
-    private String replace(char pattern, Request request, Response 
response) {
+    private String replace(char pattern, Date date, Request request,
+               Response response) {

    String value = null;

@@ -506,8 +645,8 @@
        value = "";
    } else if (pattern == 'r') {
        if (hreq != null)
-        value = hreq.getMethod() + " " + hreq.getRequestURI() + " " +
-            hreq.getProtocol();
+        value = hreq.getMethod() + space + hreq.getRequestURI() +
+            space + hreq.getProtocol();
        else
        value = "- - " + req.getProtocol();
    } else if (pattern == 's') {
@@ -516,18 +655,16 @@
        else
        value = "-";
    } else if (pattern == 't') {
-        String timestamp =
-        new Timestamp(System.currentTimeMillis()).toString();
        StringBuffer temp = new StringBuffer("[");
-        temp.append(timestamp.substring(8, 10));        // Day
+        temp.append(dayFormatter.format(date));        // Day
        temp.append('/');
-        temp.append(lookup(timestamp.substring(5, 7)));    // Month
+        temp.append(lookup(monthFormatter.format(date)));    // Month
        temp.append('/');
-        temp.append(timestamp.substring(0, 4));        // Year
+        temp.append(yearFormatter.format(date));        // Year
        temp.append(':');
-        temp.append(timestamp.substring(11, 19));        // Time
+        temp.append(timeFormatter.format(date));        // Time
        temp.append(' ');
-        temp.append("-0800");                // FIXME!!!
+        temp.append(timeZone);                // Time Zone
        temp.append(']');
        value = temp.toString();
    } else if (pattern == 'u') {
@@ -554,6 +691,25 @@
    }


+    /**
+     * This method returns a Date object that is accurate to within one
+     * second.  If a thread calls this method to get a Date and it's been
+     * less than 1 second since a new Date was created, this method
+     * simply gives out the same Date again so that the system doesn't
+     * spend time creating Date objects unnecessarily.
+     */
+    private Date getDate() {
+
+    // Only create a new Date once per second, max.
+    if ((System.currentTimeMillis() - currentDate.getTime()) > 1000) {
+        currentDate = new Date();
+    }
+
+    return currentDate;
+
+    }
+
+
    // ------------------------------------------------------ Lifecycle 
Methods


@@ -600,6 +756,23 @@
    lifecycle.fireLifecycleEvent(START_EVENT, null);
    started = true;

+    // Initialize the timeZone, Date formatters, and currentDate
+    TimeZone tz = TimeZone.getDefault();
+    timeZone = "" + (tz.getRawOffset() / (60 * 60 * 10));
+    if (timeZone.length() < 5)
+        timeZone = timeZone.substring(0, 1) + "0" +
+        timeZone.substring(1, timeZone.length());
+    dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
+    dateFormatter.setTimeZone(tz);
+    dayFormatter = new SimpleDateFormat("dd");
+    dayFormatter.setTimeZone(tz);
+    monthFormatter = new SimpleDateFormat("MM");
+    monthFormatter.setTimeZone(tz);
+    yearFormatter = new SimpleDateFormat("yyyy");
+    yearFormatter.setTimeZone(tz);
+    timeFormatter = new SimpleDateFormat("kk:mm:ss");
+    timeFormatter.setTimeZone(tz);
+    currentDate = new Date();
    }


Here's output from my new Valve tester (which I'll send another
message about after this one) benchmarking this new version of
AccessLogValve:

[jason@netninja src]$ java -Dcatalina.home=`pwd` -cp 
/home/jason/catalina/build/tomcat-4.0/server/catalina.jar:/home/jason/catalina/bin/servlet.jar
 org.apache.catalina.connector.http.AccessLogValveTester
Creating new instance of org.apache.catalina.valves.AccessLogValve
AccessLogValveTester: Sending access log to: /home/jason/catalina/src/logs/
AccessLogValveTester: Running benchmark on AccessLogValve..
AccessLogValveTester: Ran 100000 requests through the valve.
AccessLogValveTester: Elapsed time (ms): 13317
AccessLogValveTester: Average time (ms): 0
AccessLogValveTester: DONE running benchmark on AccessLogValve!

Here's the same output for the original AccessLogValve, before my
optimizations:

[jason@netninja src]$ java -Dcatalina.home=`pwd` -cp 
/home/jason/catalina/build/tomcat-4.0/server/catalina.jar:/home/jason/catalina/bin/servlet.jar
 org.apache.catalina.connector.http.AccessLogValveTester
Creating new instance of org.apache.catalina.valves.AccessLogValve
AccessLogValveTester: Sending access log to: /home/jason/catalina/src/logs/
AccessLogValveTester: Running benchmark on AccessLogValve..
AccessLogValveTester: Ran 100000 requests through the valve.
AccessLogValveTester: Elapsed time (ms): 23903
AccessLogValveTester: Average time (ms): 0
AccessLogValveTester: DONE running benchmark on AccessLogValve!

Yep!  That's a full 10 seconds slower than the new version, making
the new version about 44% faster.

This test is single-threaded, but I think it still shows noticeable
performance improvement.

Something else that I could do with this that I haven't yet is
make the valve able to buffer log file output.  From my tests,
it looks to me like this make the test run about 4 seconds faster,
a maximum of 1.6% gain (not real big, but it's something).

Let me know what you think.

-- 
Jason Brittain
Software Engineer, Olliance Inc.        http://www.Olliance.com
Current Maintainer, Locomotive Project  http://www.Locomotive.org


Reply via email to