用spring StopWatch计时

很多框架提供计时类 spring也不例外

记录下使用StopWatch的类

package cn.ceopen.zweb.common;

import java.io.IOException;
import java.text.NumberFormat;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restlet.data.Request;
import org.springframework.util.StopWatch;
import org.springframework.util.StopWatch.TaskInfo;

/**
 * 把方法执行时间写日志文件
 * 
 * 用法:
 *  MethodTimeLogger first = new MethodTimeLogger("任务名称");  
        first.start("A");  
        Thread.sleep(200);  
        first.stop();  
        first.start("B");  
        Thread.sleep(200);  
        first.stop();  
        first.start("C");  
        Thread.sleep(120);  
        first.stop();  
        first.writeLog();  
 *
 * @author 徐良永
 * @created 2014年1月13日 上午10:52:25
 */
public class StopWatchLogger {
	private static final Log log = LogFactory.getLog(StopWatchLogger.class);
	
	private StopWatch clock;
	
	/**
	 * 任务名称
	 */
	private String taskName;
	

	/**
	 * 创建一个即时任务
	 */
	public StopWatchLogger(String taskName) {
		if(StringUtils.isBlank(taskName)){
			throw new NullPointerException();
		}
		this.taskName = taskName;
		clock = new StopWatch();
	}
	
	/**
	 * 开始计时
	 */
	public void start(Request request){
		StringBuilder sb = new StringBuilder(); 
		//输出请求数据
		sb.append(request.getResourceRef().getTargetRef().toString());
		sb.append("   " + request.getMethod().getName());
		sb.append("   ");
		
		if(request.getEntity() != null && !request.getEntity().isTransient()){ //isTransient 表示可重复读取  
			try {
				sb.append(request.getEntity().getText());
			} catch (IOException e) {
				log.error(e.getMessage(), e);
			}
		}
		
		clock.start(sb.toString());
	}
	
	/**
	 * 开始一个子任务计时
	 * @param taskName: 任务名
	 */
	public void start(String subTaskName){
		clock.start(subTaskName);
	}
	
	/**
	 * 停止计时
	 */
	public void stop(){
		clock.stop();
	}
	
	/**
	 * 输出执行时间
	 */
	public void writeLog(){
		NumberFormat nf = NumberFormat.getNumberInstance();  
        nf.setMinimumIntegerDigits(5);  
        nf.setGroupingUsed(false); 
        
        log.info(">>>>  " + nf.format(clock.getTotalTimeMillis()) + "  " + taskName);
        
		for (TaskInfo task : clock.getTaskInfo()) {  
			StringBuilder sb = new StringBuilder(); 
            sb.append(nf.format(task.getTimeMillis())).append("  ");  
            sb.append(task.getTaskName());  
            log.info(sb);
        }  
	}

	
	public static void main(String[] args) throws InterruptedException{  
		StopWatchLogger first = new StopWatchLogger("test");  
        first.start("A");  
        Thread.sleep(200);  
        first.stop();  
        first.start("B");  
        Thread.sleep(200);  
        first.stop();  
        first.start("C");  
        Thread.sleep(120);  
        first.stop();  
       first.writeLog();  
          
    }  
	
}

你可能感兴趣的:(spring)