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()
    }
    }
  • 相关阅读:
    从rnn到lstm,再到seq2seq(一)
    tensorflow world language model
    sparse_tensor feed_dict的时候十分不方便。
    MAC OS X 的环境配置加载顺序
    MAC连接HHKB/其他外接键盘的时候禁用自带键盘的设置
    linux suspend的进程如何恢复?
    ubuntu16 升级 tmux 2.9
    C++ 统计运行时间之弱智方法
    shell之引号嵌套引号大全
    统一化命名
  • 原文地址:https://www.cnblogs.com/barrywxx/p/9936648.html
Copyright © 2011-2022 走看看