Skip to content

Commit

Permalink
Provide the way to measure running time
Browse files Browse the repository at this point in the history
Add ZSH_VCS_PROMPT_DEBUG_THRESHOLD_MICRO_SEC option
  • Loading branch information
yonchu committed Feb 3, 2013
1 parent 432c050 commit c9cf97c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/curret_time.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Time::Local;
use Time::HiRes qw(gettimeofday);

sub date2timestamp {
my $date = shift;
my ($year, $mon, $day, $hour, $min, $sec, $microsec) = ($date =~ /(\d{4})\-([01]\d)\-([0-3]\d) ([0-2]\d):([0-5]\d):([0-5]\d).(\d*)/);
my $timestamp = timelocal($sec, $min, $hour, $day, $mon - 1, $year);
$timestamp = $timestamp * 1000 * 1000 + $microsec;
return $timestamp;
}

my($fullSec,$microsec) = gettimeofday();
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($fullSec);
my $now = sprintf("%04d-%02d-%02d %02d:%02d:%02d.%06d",$year+1900,$mon+1,$mday,$hour,$min,$sec,$microsec);
my $timestamp = &date2timestamp($now);
print "$timestamp\n";
31 changes: 30 additions & 1 deletion zshrc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ fi

## Logging level.
# 2:Output error to stderr.
# 1:Output error to a file.
# 1:Output error to the file (ZSH_VCS_PROMPT_DIR/zsh-vcs-prompt.log).
# O/W:Suppress error .
ZSH_VCS_PROMPT_LOGGING_LEVEL=${ZSH_VCS_PROMPT_LOGGING_LEVEL:-''}

## Threshold micro sec to logging running time of zsh-vcs-prompt.
# If not set, don't print and measure running time.
ZSH_VCS_PROMPT_LOGGING_THRESHOLD_MICRO_SEC=${ZSH_VCS_PROMPT_LOGGING_THRESHOLD_MICRO_SEC:-''}

## Enable caching, if set 'true'.
ZSH_VCS_PROMPT_ENABLE_CACHING=${ZSH_VCS_PROMPT_ENABLE_CACHING:-'false'}

Expand Down Expand Up @@ -264,6 +268,10 @@ function _zsh_vcs_prompt_precmd_hook_func() {
}

function _zsh_vcs_prompt_update_vcs_status() {
if [ -n "$ZSH_VCS_PROMPT_LOGGING_THRESHOLD_MICRO_SEC" ]; then
local start=$("$ZSH_VCS_PROMPT_DIR/lib/curret_time.pl")
fi

# Parse raw data.
local raw_data
raw_data=$(vcs_super_info_raw_data)
Expand Down Expand Up @@ -353,6 +361,27 @@ function _zsh_vcs_prompt_update_vcs_status() {
-e "s/#j/$clean/")

ZSH_VCS_PROMPT_VCS_STATUS=$prompt_info

# Check running time.
if [ -n "$ZSH_VCS_PROMPT_LOGGING_THRESHOLD_MICRO_SEC" ]; then
local end=$("$ZSH_VCS_PROMPT_DIR/lib/curret_time.pl")
local elapse=$(($end - $start))
if [ "$elapse" -gt "$ZSH_VCS_PROMPT_LOGGING_THRESHOLD_MICRO_SEC" ]; then
elapse=$(_zsh_vcs_prompt_microsec2datetime "$elapse")
echo "[zsh-vcs-prompt][$(date +'%D %H:%M:%S')] Running time: $elapse ($(pwd))" 1>&2
fi
fi
}

function _zsh_vcs_prompt_microsec2datetime() {
local elapse=$1
local mc=$(($elapse % 1000000))
local elapse_sec=$(($elapse / 1000000))
local hh=$(($elapse_sec / 3600))
local ss=$(($elapse_sec % 3600))
local mm=$(($elapse_sec / 60))
local ss=$(($elapse_sec % 60))
echo "$hh:$mm:$ss.$mc"
}

# Helper function to set sigil.
Expand Down

0 comments on commit c9cf97c

Please sign in to comment.