zoukankan      html  css  js  c++  java
  • 监控程序运行两种方法

    1.监控程序运行堆栈

    val currentThread = Thread.currentThread()
    @volatile
    var flag = false
    val t = new Thread(
    new Runnable {
    override def run(): Unit = {
    while(!flag) {
    val stackElements = currentThread.getStackTrace
    logger.info(stackElements.map(_.toString).mkString(" "))
    Thread.sleep(100)
    }
    }
    }
    )
    t.start()

    ......//do something

    var flag = true

    2. 计时及运行次数;

    定义计时:

    import org.slf4j.LoggerFactory

    /**
    * 类功能描述:Debug日志追踪
    *
    * @author barry create at 18-8-29 下午3:41
    * @version 1.0.0
    */
    object Debug {
    val LOGGER = LoggerFactory.getLogger(getClass)
    val counter = collection.mutable.Map[String, Int]() // label -> count
    val times = collection.mutable.Map[String, Long]() // label - time(ns)

    /**
    *追踪代码块
    * @param label 标签名
    * @param codeBlock 代码块
    * @tparam T 返回结果类型
    * @return
    */
    def trace[T](label: String)(codeBlock: => T) = {
    val t0 = System.nanoTime()
    val result = codeBlock
    val t1 = System.nanoTime()
    counter.get(label).map(_counter => counter.put(label, _counter + 1)).orElse(counter.put(label, 1))
    times.get(label).map(cost => times.put(label, cost + (t1 - t0))).orElse(times.put(label, t1 - t0))
    result
    }

    /**
    * 打印日志
    */
    def info(): Unit = {
    LOGGER.warn("FinallyDone...")
    LOGGER.warn(s"counter:${counter}")
    LOGGER.warn(s"times:${times.map { case (label, cost) => (label, cost / 1000000)}}ms")
    }

    /**
    * 重新计数
    */
    def reset(): Unit = {
    counter.clear()
    times.clear()
    }
    }
  • 相关阅读:
    Azure 云助手主要功能
    静态dll的问题终于搞定了
    青云QingCloud黄允松:最高效的研发管理就是没有管理
    开源libco库:单机千万连接、支撑微信8亿用户的后台框架基石
    青云QingCloud宣布完成C轮融资,金额1亿美元
    NET MVC权限验证
    Fizz-Buzz-Whizz
    API访问客户端
    使用IronPython给.Net程序
    Smart Framework
  • 原文地址:https://www.cnblogs.com/barrywxx/p/9936648.html
Copyright © 2011-2022 走看看