diff --git a/Descriptors/GPXDescriptor.h b/Descriptors/GPXDescriptor.h index 105a09abf67..47041deb236 100644 --- a/Descriptors/GPXDescriptor.h +++ b/Descriptors/GPXDescriptor.h @@ -56,8 +56,9 @@ class GPXDescriptor : public BaseDescriptor { "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 gpx.xsd" "\">"); + reply.content.push_back(""); reply.content.push_back( - "Data (c)" + "Data (c)" " OpenStreetMap contributors (ODbL)" ""); reply.content.push_back(""); diff --git a/Descriptors/JSONDescriptor.h b/Descriptors/JSONDescriptor.h index c42482c23cb..8952cf34a54 100644 --- a/Descriptors/JSONDescriptor.h +++ b/Descriptors/JSONDescriptor.h @@ -115,9 +115,8 @@ class JSONDescriptor : public BaseDescriptor { http::Reply & reply ) { facade = f; - reply.content.push_back( - "{\"status\":" - ); + reply.content.push_back("{"); + reply.content.push_back("\"status\":"); if(INT_MAX == raw_route.lengthOfShortestPath) { //We do not need to do much, if there is no route ;-) diff --git a/Include/osrm/RouteParameters.h b/Include/osrm/RouteParameters.h index f95d7aa651f..a4b99d90288 100644 --- a/Include/osrm/RouteParameters.h +++ b/Include/osrm/RouteParameters.h @@ -45,6 +45,7 @@ struct RouteParameters { geometry(true), compression(true), deprecatedAPI(false), + exec_time(false), measurement(false), checkSum(-1) { } @@ -55,7 +56,7 @@ struct RouteParameters { bool geometry; bool compression; bool deprecatedAPI; - bool measurement; + bool exec_time; unsigned checkSum; std::string service; std::string outputFormat; @@ -78,9 +79,9 @@ struct RouteParameters { deprecatedAPI = true; } - void setMeasurementFlag(const bool b) { - measurement = b; - } + void setExecTimeFlag(const bool b) { + exec_time = b; + } void setChecksum(const unsigned c) { checkSum = c; diff --git a/Plugins/NearestPlugin.h b/Plugins/NearestPlugin.h index ab4e3917aac..b516a9c6994 100644 --- a/Plugins/NearestPlugin.h +++ b/Plugins/NearestPlugin.h @@ -31,7 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "BasePlugin.h" #include "../DataStructures/PhantomNodes.h" #include "../Util/StringUtil.h" -#include "../Util/TimeMeasurement.h" +#include "../Util/TimingUtil.h" #include @@ -66,7 +66,7 @@ class NearestPlugin : public BasePlugin { return; } - TimeMeasurement measure; + TimeMeasurement exec_time; PhantomNode result; facade->FindPhantomNodeForCoordinate( routeParameters.coordinates[0], @@ -74,7 +74,7 @@ class NearestPlugin : public BasePlugin { routeParameters.zoomLevel ); - int64_t time_ms = measure.toNow(); + int64_t time_ms = exec_time.toNowInMs(); std::string temp_string; //json @@ -85,16 +85,17 @@ class NearestPlugin : public BasePlugin { } reply.status = http::Reply::ok; - reply.content.push_back("{\"status\":"); + reply.content.push_back("{"); + if (routeParameters.exec_time) { + int64ToString(time_ms, temp_string); + reply.content.push_back("\"exec_time_ms\":" + temp_string + ","); + } + reply.content.push_back("\"status\":"); if(UINT_MAX != result.edgeBasedNode) { reply.content.push_back("0,"); } else { reply.content.push_back("207,"); } - if (routeParameters.measurement) { - int64ToString(time_ms, temp_string); - reply.content.push_back("\"measurement_ms\":" + temp_string + ","); - } reply.content.push_back("\"mapped_coordinate\":["); if(UINT_MAX != result.edgeBasedNode) { FixedPointCoordinate::convertInternalLatLonToString(result.location.lat, temp_string); diff --git a/Plugins/ViaRoutePlugin.h b/Plugins/ViaRoutePlugin.h index 9dc6a73f273..ef42353b02b 100644 --- a/Plugins/ViaRoutePlugin.h +++ b/Plugins/ViaRoutePlugin.h @@ -38,7 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../Descriptors/JSONDescriptor.h" #include "../Util/SimpleLogger.h" #include "../Util/StringUtil.h" -#include "../Util/TimeMeasurement.h" +#include "../Util/TimingUtil.h" #include @@ -47,14 +47,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -static bool IsTransactionStr (const std::string &s) { - static std::string trans("transactionId"); - if (s.size() < trans.size()) { - return false; - } - return (0 == s.compare(1, trans.size(), trans)); -} - template class ViaRoutePlugin : public BasePlugin { private: @@ -90,7 +82,7 @@ class ViaRoutePlugin : public BasePlugin { return; } - TimeMeasurement measure; + TimeMeasurement exec_time; RawRouteData rawRoute; rawRoute.checkSum = facade->GetCheckSum(); const bool checksumOK = (routeParameters.checkSum == rawRoute.checkSum); @@ -166,17 +158,16 @@ class ViaRoutePlugin : public BasePlugin { descriptorConfig.geometry = routeParameters.geometry; descriptorConfig.encode_geometry = routeParameters.compression; + std::string exec_time_token; switch(descriptorType){ - case 0: - desc = new JSONDescriptor(); - - break; case 1: desc = new GPXDescriptor(); - + exec_time_token = ""; break; + case 0: default: desc = new JSONDescriptor(); + exec_time_token = "{"; break; } @@ -188,15 +179,24 @@ class ViaRoutePlugin : public BasePlugin { desc->Run(rawRoute, phantomNodes, facade, reply); - if (routeParameters.measurement) { - int64_t time_ms = measure.toNow(); - std::vector::iterator it = std::find_if(reply.content.begin(), reply.content.end(), IsTransactionStr); - if (it != reply.content.end()) { - std::string temp_string; - int64ToString(time_ms, temp_string); - reply.content.insert(it, "\"measurement_ms\":" + temp_string + ","); - } - } + if (routeParameters.exec_time) { + int64_t time_ms = exec_time.toNowInMs(); + std::vector::iterator it = reply.content.begin(); + for (; it != reply.content.end(); ++it) { + if (0 == (*it).compare(0, exec_time_token.size(), exec_time_token)) { + break; // we have found place to insert exec time metric + } + } + if (it != reply.content.end()) { + std::string temp_string; + int64ToString(time_ms, temp_string); + reply.content.insert(++it, (1 == descriptorType)? + "" + temp_string + "" : "\"exec_time_ms\":" + temp_string + "," + ); + } else { + SimpleLogger().Write(logDEBUG) << "Can't find place to insert exec time metric"; + } + } if("" != routeParameters.jsonpParameter) { reply.content.push_back(")\n"); } diff --git a/Server/APIGrammar.h b/Server/APIGrammar.h index 9f37208a18b..ce4e345b4d8 100644 --- a/Server/APIGrammar.h +++ b/Server/APIGrammar.h @@ -38,7 +38,7 @@ template struct APIGrammar : qi::grammar { APIGrammar(HandlerT * h) : APIGrammar::base_type(api_call), handler(h) { api_call = qi::lit('/') >> string[boost::bind(&HandlerT::setService, handler, ::_1)] >> *(query); - query = ('?') >> (+(zoom | output | jsonp | checksum | location | hint | cmp | language | instruction | geometry | alt_route | old_API | measurement) ) ; + query = ('?') >> (+(zoom | output | jsonp | checksum | location | hint | cmp | language | instruction | geometry | alt_route | old_API | exec_time) ) ; zoom = (-qi::lit('&')) >> qi::lit('z') >> '=' >> qi::short_[boost::bind(&HandlerT::setZoomLevel, handler, ::_1)]; output = (-qi::lit('&')) >> qi::lit("output") >> '=' >> string[boost::bind(&HandlerT::setOutputFormat, handler, ::_1)]; @@ -52,7 +52,7 @@ struct APIGrammar : qi::grammar { language = (-qi::lit('&')) >> qi::lit("hl") >> '=' >> string[boost::bind(&HandlerT::setLanguage, handler, ::_1)]; alt_route = (-qi::lit('&')) >> qi::lit("alt") >> '=' >> qi::bool_[boost::bind(&HandlerT::setAlternateRouteFlag, handler, ::_1)]; old_API = (-qi::lit('&')) >> qi::lit("geomformat") >> '=' >> string[boost::bind(&HandlerT::setDeprecatedAPIFlag, handler, ::_1)]; - measurement = (-qi::lit('&')) >> qi::lit("measurement") >> '=' >> qi::bool_[boost::bind(&HandlerT::setMeasurementFlag, handler, ::_1)]; + exec_time = (-qi::lit('&')) >> qi::lit("exec_time") >> '=' >> qi::bool_[boost::bind(&HandlerT::setExecTimeFlag, handler, ::_1)]; string = +(qi::char_("a-zA-Z")); stringwithDot = +(qi::char_("a-zA-Z0-9_.-")); @@ -62,7 +62,7 @@ struct APIGrammar : qi::grammar { qi::rule api_call, query; qi::rule service, zoom, output, string, jsonp, checksum, location, hint, stringwithDot, stringwithPercent, language, instruction, geometry, - cmp, alt_route, old_API, measurement; + cmp, alt_route, old_API, exec_time; HandlerT * handler; }; diff --git a/Util/TimeMeasurement.h b/Util/TimeMeasurement.h deleted file mode 100644 index 044a6d0835b..00000000000 --- a/Util/TimeMeasurement.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - -Copyright (c) 2013, Project OSRM, Dennis Luxen, others -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -Redistributions of source code must retain the above copyright notice, this list -of conditions and the following disclaimer. -Redistributions in binary form must reproduce the above copyright notice, this -list of conditions and the following disclaimer in the documentation and/or -other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*/ - -#ifndef TIME_MEASUREMENT_H_ -#define TIME_MEASUREMENT_H_ - -#include "../typedefs.h" - -#include - -class TimeMeasurement { -public: - TimeMeasurement() throw() { - fromNow(); - } - inline void fromNow() throw() { - _from = boost::posix_time::microsec_clock::local_time(); - } - inline int64_t toNow() const throw() { - boost::posix_time::time_duration diff = boost::posix_time::microsec_clock::local_time() - _from; - return diff.total_milliseconds(); - } -private: - boost::posix_time::ptime _from; -}; - -#endif // TIME_MEASUREMENT_H_ diff --git a/Util/TimingUtil.h b/Util/TimingUtil.h index 290d37799f9..7d36d601021 100644 --- a/Util/TimingUtil.h +++ b/Util/TimingUtil.h @@ -28,6 +28,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef TIMINGUTIL_H_ #define TIMINGUTIL_H_ +#include "../typedefs.h" + +#include + // excluded as this requires boost 1.47 (for now) // #include // #include @@ -68,4 +72,25 @@ static inline double get_timestamp() { return double(tp.tv_sec) + tp.tv_usec / 1000000.; } +class TimeMeasurement { +public: + TimeMeasurement() throw() { fromNow(); } + inline void fromNow() throw() { + m_from = boost::posix_time::microsec_clock::local_time(); + } + // get time period from the fromNow() func. calling to now in milliseconds + inline int64_t toNowInMs() const throw() { + boost::posix_time::time_duration diff = boost::posix_time::microsec_clock::local_time() - m_from; + return diff.total_milliseconds(); + } + // get time period from the fromNow() func. calling to now in seconds + inline int64_t toNowInSec() const throw() { + boost::posix_time::time_duration diff = boost::posix_time::microsec_clock::local_time() - m_from; + return diff.total_seconds(); + } + +private: + boost::posix_time::ptime m_from; +}; + #endif /* TIMINGUTIL_H_ */