Web Project Add Interface response length

Source: Internet
Author: User
Tags aop

Background: A few days ago the company online a project (mobile phone H5 project) said that users and bosses feel the corresponding speed is relatively slow, and then down to find out why, so you need to count server-side interface call length.
Scenario:
1.logback+code
The specific step is to add a point in time before and after each method's call, and then log the results to the log.
code invoked within an 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 the object.
            User user = new user ();
            Return User.seterror (Getbaseresponse (e));
        }
    

Corresponding time-length statistics class:

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 Add log file:

  <appender name= "Correspondingtime" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
        <file ";" /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
Now that spring AOP can output logs within a method, it is certainly possible to implement the statistics on the length of the docking port response within the method. Find information as follows:

@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 A = true; for (Object o:params) {String p = (o = = null?)
                "Null": O.tostring ());
                    if (a) {param = param + p;
                i = false;
            else param = param + ":" + P;
        } param = param + ")"; Log.debug ("Class name is:" + pjp.gettarget (). GetClass (). GetName () + "| Method Name:" + pjp.getsignature (). GetName () + "| parameter is:" + param + "|
    The time is: "+ (System.currenttimemillis ()-L1));    return obj; }
}

Summary:
1, relatively good to think, but also easy to achieve but a little turtle. If you need to count too many interfaces, then the amount of change is very large, not recommended.
2, invoke Spring AOP itself properties, relatively simple, can achieve the statistics of the multiple interfaces, praise.

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.