首页  :: 新随笔  :: 管理

使用time分析命令耗时

Posted on 2021-11-25 22:49  高&玉  阅读(226)  评论(0编辑  收藏  举报

1 前言

   在工作中,我有时候需要获取一些命令的执行时间。我们可以通过time命令查看相应命令(real、user、sys)耗时时间。

2 使用方法

1 [root]# time <command>
3 real    0m0.001s
4 user    0m0.000s
5 sys     0m0.001s

注释:

  • real:进程从开始到结束的耗时。
  • user:进程消耗在用户模式中的CPU时间。
  • sys:内核调用系统消耗的时间。

2.1 查看hostname耗时

1 [root]# time hostname >> /dev/null
2 
3 real    0m0.001s
4 user    0m0.000s
5 sys     0m0.001s

解释:

  • real:总耗时1毫秒。
  • user:进程消耗在用户模式中的CPU时间为0。
  • sys:内核调用系统消耗的时间为1毫秒。

2.2 分析PG Client连接Server耗时

1 [root]# time psql -h 192.168.5.74 -U postgres -t -c 'select now();' >> /dev/null
2 
3 real    0m0.042s
4 user    0m0.003s
5 sys     0m0.002s

解释:

  • real:总耗时42毫秒。
  • user:进程消耗在用户模式中的CPU时间3毫秒。
  • sys:内核调用系统消耗的时间为2毫秒。
  • 还有37毫秒耗时,是Client端连接Server端TCP连接耗时。