Web Project Add Interface response duration

Source: Internet
Author: User
Tags aop

Background: A few days ago the company launched a project (mobile phone H5 project) said that the user and the boss feel that the corresponding speed is relatively slow, and then down to find the reason, so you need to count the server-side interface call length.
Scenario:
1.logback+code
The specific steps are to add a point in time before and after each method call, and then log the results.
code called within the interface:

@Override public
    User getuserinfo (int userId) {
        Long correspondingstarttime = System.currenttimemillis ();
        Logger.debug ("Debug_getuserinfo userid\t" + userId);
        try {
            User user = Userinfoservice.getuserinfo (userId);
            if (user==null| | userid<=0) {
                logger.debug ("GetUserInfo userid==null" +userid);
            } else{
                logger.debug ("GetUserInfo userid\t" + userId + ", Response:" + user.tostring ());
            }
            Long correspondingendtime = System.currenttimemillis ();
            Correspondingtime.correspondingtime ("Userserviceimpl", "GetUserInfo", Correspondingstarttime, Correspondingendtime);
            return user;
        } catch (RuntimeException e) {
            logger.error ("GetUserInfo  userid:" + userid, e);
            Encapsulates an object.
            User user = new user ();
            Return User.seterror (Getbaseresponse (e));
        }
    }

Corresponding time-length statistics classes:

public class Correspondingtime {
    private static final Logger Logger = Loggerfactory.getlogger ("Correspondingtimelog ");
    public static void Correspondingtime (String clazzname,string methodname,long Correspondingstarttime,long Correspondingendtime) {
        logger.info (clazzname+ ";") +methodname+ ";" + (Correspondingendtime-correspondingstarttime));
    }
}

Logback Adding log files:

  <appender name= "Correspondingtime" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
        <file A. /user-log/server-correspondingtime.log</file>
        <encoder>
            <pattern>%m%n</pattern >
        </encoder>
        <rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
            <filenamepattern>, .... /user-log/server-correspondingtime.log.%d{yyyy-mm-dd}</filenamepattern>
        </rollingPolicy>
        <filter class= "Ch.qos.logback.classic.filter.ThresholdFilter" >
            <level>info</level>
        </filter>
    </appender>/
     * log file name */
    <logger name= "Correspondingtimelog" additivity= " False ">
        <appender-ref ref=" Correspondingtime "/>
    </logger>

2.Spring AOP
Since spring AOP can output logs within a method, it is certainly possible to count the length of the docking port response within the method. Find the following information:

@Aspect @Component public class Timestataspect {private static final Logger LOG = Loggerfactory.getlogger (timestatasp
    Ect.class);
    @Around ("Execution (* com.baidu.soa.crm.user.dao.impl.*daoimpl.* (..))")
        Public Object printtime (Proceedingjoinpoint pjp) throws Throwable {long L1 = System.currenttimemillis ();
        Object obj = Pjp.proceed ();
        String param = "(";
        object[] params = Pjp.getargs ();
            if (params! = null) {Boolean first = true; for (Object o:params) {String p = (o = = null?)
                "Null": O.tostring ());
                    if (first) {param = param + p;
                First = false;
            } else param = param + ":" + P;
        }} param = param + ")"; Log.debug ("Class name:" + pjp.gettarget (). GetClass (). GetName () + "| Method Name:" + pjp.getsignature (). GetName () + "| parameter:" + param + "| execution
    The time is: "+ (System.currenttimemillis ()-L1));    return obj; }
}

Summary:
1, better think, but also easy to achieve but a bit of Earth turtle. If you need to count the interface too much, then the amount of change is very large, not recommended.
2, call Spring AOP own properties, relatively simple, can achieve the statistics on the interface, like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.