Skip to content

Commit

Permalink
console, feat: add console.timeElapse() (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmxyb authored and xicilion committed Jun 11, 2018
1 parent 27b5f17 commit 312df05
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
17 changes: 17 additions & 0 deletions fibjs/include/ifs/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class console_base : public object_base {
static result_t alert(OptArgs args);
static result_t dir(v8::Local<v8::Value> obj);
static result_t time(exlib::string label);
static result_t timeElapse(exlib::string label);
static result_t timeEnd(exlib::string label);
static result_t trace(exlib::string label);
static result_t _assert(v8::Local<v8::Value> value, exlib::string msg);
Expand Down Expand Up @@ -111,6 +112,7 @@ class console_base : public object_base {
static void s_alert(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_dir(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_time(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_timeElapse(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_timeEnd(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_trace(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_assert(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down Expand Up @@ -150,6 +152,7 @@ inline ClassInfo& console_base::class_info()
{ "alert", s_alert, true },
{ "dir", s_dir, true },
{ "time", s_time, true },
{ "timeElapse", s_timeElapse, true },
{ "timeEnd", s_timeEnd, true },
{ "trace", s_trace, true },
{ "assert", s_assert, true },
Expand Down Expand Up @@ -480,6 +483,20 @@ inline void console_base::s_time(const v8::FunctionCallbackInfo<v8::Value>& args
METHOD_VOID();
}

inline void console_base::s_timeElapse(const v8::FunctionCallbackInfo<v8::Value>& args)
{
METHOD_NAME("console.timeElapse");
METHOD_ENTER();

METHOD_OVER(1, 0);

OPT_ARG(exlib::string, 0, "time");

hr = timeElapse(v0);

METHOD_VOID();
}

inline void console_base::s_timeEnd(const v8::FunctionCallbackInfo<v8::Value>& args)
{
METHOD_NAME("console.timeEnd");
Expand Down
18 changes: 18 additions & 0 deletions fibjs/src/console/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,24 @@ result_t console_base::time(exlib::string label)
return 0;
}

result_t console_base::timeElapse(exlib::string label)
{
int64_t t = Ticks() - s_timers[label];

exlib::string strBuffer;
char numStr[64];

sprintf(numStr, "%.10g", t / 1000.0);

strBuffer.append(label);
strBuffer.append(": ", 2);
strBuffer.append(numStr);
strBuffer.append("ms", 2);

asyncLog(_INFO, strBuffer);
return 0;
}

result_t console_base::timeEnd(exlib::string label)
{
int64_t t = Ticks() - s_timers[label];
Expand Down
8 changes: 7 additions & 1 deletion idl/us-en/console.idl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,13 @@ module console
*/
static time(String label = "time");

/*! @brief Value of specified timer
/*! @brief Print elapse of specified timer

@param label Title, default is an empty string.
*/
static timeElapse(String label = "time");

/*! @brief Stop the specified timer, and print last value.

@param label Title, default is an empty string.
*/
Expand Down
8 changes: 7 additions & 1 deletion idl/zh-cn/console.idl
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,13 @@ module console
*/
static time(String label = "time");

/*! @brief 统计指定计时器的数值
/*! @brief 输出指定计时器当前计时值

@param label 标题,缺省为空字符串。
*/
static timeElapse(String label = "time");

/*! @brief 结束指定计时器,并输出最后计时值

@param label 标题,缺省为空字符串。
*/
Expand Down

0 comments on commit 312df05

Please sign in to comment.