Authored by 周奇琪

add timer util to log,and add a unit test example

@@ -12,7 +12,8 @@ let API = require('../../library/api'); @@ -12,7 +12,8 @@ let API = require('../../library/api');
12 // Your controller here 12 // Your controller here
13 router.get('/',function(req,res){ 13 router.get('/',function(req,res){
14 let api = new API(); 14 let api = new API();
15 - api.get('/v2/book/1220562',{}).then(function(body){ 15 + let name = '/productColor/queryProductColors';
  16 + api.get(name,{}).then(function(body){
16 res.send(body); 17 res.send(body);
17 }).catch(function (error) { 18 }).catch(function (error) {
18 res.send('some thing wrong'); 19 res.send('some thing wrong');
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 7
8 module.exports = { 8 module.exports = {
9 domains: { 9 domains: {
10 - api:'http://api.douban.com' 10 + api:'http://192.168.102.202:8088/platform'
11 }, 11 },
12 loggers: { 12 loggers: {
13 file:{ 13 file:{
@@ -10,6 +10,7 @@ const rp = require('request-promise'); @@ -10,6 +10,7 @@ const rp = require('request-promise');
10 const _ = require('lodash'); 10 const _ = require('lodash');
11 const log = require('./logger'); 11 const log = require('./logger');
12 const api = require('../config/common').domains.api; 12 const api = require('../config/common').domains.api;
  13 +const Timer = require('./timer');
13 14
14 const ApiUrl = api; 15 const ApiUrl = api;
15 16
@@ -21,21 +22,23 @@ class API { @@ -21,21 +22,23 @@ class API {
21 * @param data Obejct 22 * @param data Obejct
22 */ 23 */
23 get(url, data) { 24 get(url, data) {
24 -  
25 - log.info('API GET: %s, parms: %j',url,data,{});  
26 - log.profile('%s %j',url,data);  
27 -  
28 - let ret = rp({ 25 +
  26 + let options = {
29 url: `${ApiUrl}${url}`, 27 url: `${ApiUrl}${url}`,
30 qs: data 28 qs: data
31 - }); 29 + };
  30 +
  31 + let timer = new Timer();
  32 + timer.put('getApi');//统计时间开始
  33 +
  34 + let ret = rp(options);
32 35
33 ret.then((body)=>{ 36 ret.then((body)=>{
34 - log.profile('%s %j',url,data);  
35 - log.info('API GET: %s, parms: %j ',url,data,body); 37 + let duration = timer.put('getApi');//接口返回
  38 + log.info('API GET: %s, parms: %j , durationMs: %d ms , body: %s',options.url,options.qs,duration,body);
36 }).catch((error)=>{ 39 }).catch((error)=>{
37 - log.profile('%s %j',url,data);  
38 - log.error('API GET: %s, parms: %j ',url,data,error); 40 + let duration = timer.put('getApi');//接口返回
  41 + log.error('API GET: %s, parms: %j , durationMs: %d ms error: %s , statusCode: %d',options.url,options.qs,duration,error.message,error.statusCode);
39 }); 42 });
40 43
41 return ret; 44 return ret;
  1 +'use strict';
  2 +
  3 +/**
  4 + * 计时类
  5 + * @example
  6 + * let timer = new Timer();
  7 + * timer.put('profile');
  8 + * timer.put('proflie'); // console output: 12.14
  9 + *
  10 + * @author: hbomb<qiqi.zhou@yoho.cn>
  11 + * @date: 2016/05/07
  12 + */
  13 +
  14 +class Timer {
  15 + constructor() {
  16 + this.timers = {};
  17 + }
  18 +
  19 + /**
  20 + * 打点计时
  21 + */
  22 + put(label) {
  23 + let labelTime = this.timers[label];
  24 +
  25 + if (labelTime) {
  26 + let duration = process.hrtime(labelTime);
  27 + return this._round(duration[1]);
  28 + } else {
  29 + this.timers[label] = process.hrtime();
  30 + }
  31 + }
  32 +
  33 + /**
  34 + * 格式化成毫秒
  35 + * @param {Number} value 纳秒
  36 + */
  37 + _round(value) {
  38 + return Math.round(value / 10000) / 100;
  39 + }
  40 +
  41 +}
  42 +
  43 + module.exports = Timer;
@@ -13,7 +13,7 @@ @@ -13,7 +13,7 @@
13 "online": "NODE_ENV=\"production\" node app.js", 13 "online": "NODE_ENV=\"production\" node app.js",
14 "debug": "DEBUG=\"express:*\" node app.js", 14 "debug": "DEBUG=\"express:*\" node app.js",
15 "lint-js": "node_modules/.bin/eslint -c .eslintrc --cache --fix \"git diff --cached --name-only --diff-filter=ACM | grep .js$\" app.js", 15 "lint-js": "node_modules/.bin/eslint -c .eslintrc --cache --fix \"git diff --cached --name-only --diff-filter=ACM | grep .js$\" app.js",
16 - "lint-css": "node_modules/.bin/stylelint --config .stylelintrc \"git diff --cached --name-only --diff-filter=ACM | grep .css$\" public/css/*.css&", 16 + "lint-css": "node_modules/.bin/stylelint --config .stylelintrc \"git diff --cached --name-only --diff-filter=ACM | grep .css$\" &",
17 "precommit": "npm run lint-js && npm run lint-css" 17 "precommit": "npm run lint-js && npm run lint-css"
18 }, 18 },
19 "license": "MIT", 19 "license": "MIT",
@@ -40,6 +40,8 @@ @@ -40,6 +40,8 @@
40 "gulp-sourcemaps": "^2.0.0-alpha", 40 "gulp-sourcemaps": "^2.0.0-alpha",
41 "gulp-util": "^3.0.7", 41 "gulp-util": "^3.0.7",
42 "husky": "^0.11.4", 42 "husky": "^0.11.4",
  43 + "mocha": "^2.4.5",
  44 + "nodemon": "1.9.2",
43 "postcss-assets": "^4.0.1", 45 "postcss-assets": "^4.0.1",
44 "postcss-cachebuster": "^0.1.2", 46 "postcss-cachebuster": "^0.1.2",
45 "postcss-calc": "^5.2.1", 47 "postcss-calc": "^5.2.1",
@@ -52,11 +54,12 @@ @@ -52,11 +54,12 @@
52 "postcss-sprites": "^3.1.2", 54 "postcss-sprites": "^3.1.2",
53 "postcss-use": "^2.0.2", 55 "postcss-use": "^2.0.2",
54 "precss": "^1.4.0", 56 "precss": "^1.4.0",
  57 + "rewire": "^2.5.1",
  58 + "shelljs": "^0.7.0",
55 "stylelint": "^6.2.2", 59 "stylelint": "^6.2.2",
56 "stylelint-config-yoho": "^1.2.0", 60 "stylelint-config-yoho": "^1.2.0",
57 "webpack": "^1.13.0", 61 "webpack": "^1.13.0",
58 "webpack-dev-server": "^1.14.1", 62 "webpack-dev-server": "^1.14.1",
59 - "webpack-stream": "^3.1.0",  
60 - "shelljs": "^0.7.0" 63 + "webpack-stream": "^3.1.0"
61 } 64 }
62 } 65 }
  1 +let expect = require("expect.js");
  2 +let Timer = require("../../library/timer");
  3 +
  4 +
  5 +describe('/library/timer',function(){
  6 + it('延迟100ms,期望大于或等于100ms',function(done){
  7 + let t = new Timer();
  8 + t.put('aa');
  9 + setTimeout(function(){
  10 + let time = t.put('aa');
  11 + expect(Math.round(time)>=100).to.be.ok();
  12 + done();
  13 + },100)
  14 + });
  15 +});
  1 +require('./library/timer.test');