From: Kai Koehne Date: Thu, 3 Nov 2011 16:58:07 +0000 (+0100) Subject: Add console.time / console.timeEnd API X-Git-Tag: qt-v5.0.0-alpha1~1196 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cd633df026733f420b5b63ea8b6a4ab943408db9;p=profile%2Fivi%2Fqtdeclarative.git Add console.time / console.timeEnd API Implement console.time() / console.timeEnd() as also provided by FireBug / Safari. Task-number: QTBUG-22347 Change-Id: I94fcadbb0c54fdf60dc2559e3ae63d613e29630b Reviewed-by: Michael Brasser --- diff --git a/doc/src/declarative/globalobject.qdoc b/doc/src/declarative/globalobject.qdoc index f4e9d0d..ced797b 100644 --- a/doc/src/declarative/globalobject.qdoc +++ b/doc/src/declarative/globalobject.qdoc @@ -203,7 +203,7 @@ May throw exception with code property SQLException.DATABASE_ERR, SQLException.S \section1 Logging -\c console.log() and \c console.debug() can be used to print information +\c console.log(), \c console.debug(), \c console.time(), and \c console.timeEnd() can be used to print information to the console. See \l{Debugging QML} for more information. */ diff --git a/doc/src/declarative/qdeclarativedebugging.qdoc b/doc/src/declarative/qdeclarativedebugging.qdoc index 189d444..a34c2fa 100644 --- a/doc/src/declarative/qdeclarativedebugging.qdoc +++ b/doc/src/declarative/qdeclarativedebugging.qdoc @@ -43,6 +43,20 @@ Rectangle { } \endqml +\c console.time and console.timeEnd log the time (in milliseconds) that was spent between +the calls. Both take a string argument that identifies the measurement. For example: + +\qml +function f() { + console.time("wholeFunction"); + console.time("firstPart"); + // first part + console.timeEnd("firstPart"); + // second part + console.timeEnd("wholeFunction"); +} +\endqml + \section1 Debugging Transitions When a transition doesn't look quite right, it can be helpful to view it in slow diff --git a/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp b/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp index 96c51da..1ced868 100644 --- a/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp +++ b/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp @@ -92,6 +92,28 @@ v8::Handle print(const v8::Arguments &args) return v8::Undefined(); } +v8::Handle consoleTime(const v8::Arguments &args) +{ + if (args.Length() != 1) + V8THROW_ERROR("console.time(): Invalid arguments"); + QString name = V8ENGINE()->toString(args[0]); + V8ENGINE()->startTimer(name); + return v8::Undefined(); +} + +v8::Handle consoleTimeEnd(const v8::Arguments &args) +{ + if (args.Length() != 1) + V8THROW_ERROR("console.time(): Invalid arguments"); + QString name = V8ENGINE()->toString(args[0]); + bool wasRunning; + qint64 elapsed = V8ENGINE()->stopTimer(name, &wasRunning); + if (wasRunning) { + qDebug("%s: %llims", qPrintable(name), elapsed); + } + return v8::Undefined(); +} + v8::Handle stringArg(const v8::Arguments &args) { QString value = V8ENGINE()->toString(args.This()->ToString()); diff --git a/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h b/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h index 847dc66..35ea885 100644 --- a/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h +++ b/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h @@ -91,6 +91,8 @@ v8::Handle qsTr(const v8::Arguments &args); v8::Handle qsTrNoOp(const v8::Arguments &args); v8::Handle qsTrId(const v8::Arguments &args); v8::Handle qsTrIdNoOp(const v8::Arguments &args); +v8::Handle consoleTime(const v8::Arguments &args); +v8::Handle consoleTimeEnd(const v8::Arguments &args); v8::Handle stringArg(const v8::Arguments &args); } diff --git a/src/declarative/qml/v8/qv8engine.cpp b/src/declarative/qml/v8/qv8engine.cpp index d239f30..6f744e5 100644 --- a/src/declarative/qml/v8/qv8engine.cpp +++ b/src/declarative/qml/v8/qv8engine.cpp @@ -519,10 +519,14 @@ void QV8Engine::initializeGlobal(v8::Handle global) { using namespace QDeclarativeBuiltinFunctions; v8::Local printFn = V8FUNCTION(print, this); + v8::Local consoleTimeFn = V8FUNCTION(consoleTime, this); + v8::Local consoleTimeEndFn = V8FUNCTION(consoleTimeEnd, this); v8::Local console = v8::Object::New(); console->Set(v8::String::New("log"), printFn); console->Set(v8::String::New("debug"), printFn); + console->Set(v8::String::New("time"), consoleTimeFn); + console->Set(v8::String::New("timeEnd"), consoleTimeEndFn); v8::Local qt = v8::Object::New(); @@ -1398,6 +1402,24 @@ void QV8Engine::emitSignalHandlerException() emit q->signalHandlerException(scriptValueFromInternal(uncaughtException())); } +void QV8Engine::startTimer(const QString &timerName) +{ + if (!m_time.isValid()) + m_time.start(); + m_startedTimers[timerName] = m_time.elapsed(); +} + +qint64 QV8Engine::stopTimer(const QString &timerName, bool *wasRunning) +{ + if (!m_startedTimers.contains(timerName)) { + *wasRunning = false; + return 0; + } + *wasRunning = true; + qint64 startedAt = m_startedTimers.take(timerName); + return m_time.elapsed() - startedAt; +} + QThreadStorage QV8GCCallback::threadData; void QV8GCCallback::initializeThreadData() { diff --git a/src/declarative/qml/v8/qv8engine_p.h b/src/declarative/qml/v8/qv8engine_p.h index 92e6123..7b85cfc 100644 --- a/src/declarative/qml/v8/qv8engine_p.h +++ b/src/declarative/qml/v8/qv8engine_p.h @@ -59,6 +59,7 @@ #include #include #include +#include #include #include @@ -405,6 +406,10 @@ public: void emitSignalHandlerException(); + // used for console.time(), console.timeEnd() + void startTimer(const QString &timerName); + qint64 stopTimer(const QString &timerName, bool *wasRunning); + QObject *qtObjectFromJS(v8::Handle value); QSet visitedConversionObjects; protected: @@ -436,6 +441,9 @@ protected: Exception m_exception; + QElapsedTimer m_time; + QHash m_startedTimers; + QVariant toBasicVariant(v8::Handle); void initializeGlobal(v8::Handle);