Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execution time measurement patch #923

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Descriptors/GPXDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ class GPXDescriptor : public BaseDescriptor<DataFacadeT> {
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
"xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 gpx.xsd"
"\">");
reply.content.push_back("<metadata>");
reply.content.push_back(
"<metadata><copyright author=\"Project OSRM\"><license>Data (c)"
"<copyright author=\"Project OSRM\"><license>Data (c)"
" OpenStreetMap contributors (ODbL)</license></copyright>"
"</metadata>");
reply.setContentInsIndex(reply.content.size() - 1); // place to insert additional content
reply.content.push_back("<rte>");
bool found_route = (raw_route.lengthOfShortestPath != INT_MAX) &&
(raw_route.unpacked_path_segments[0].size());
Expand Down
6 changes: 3 additions & 3 deletions Descriptors/JSONDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ class JSONDescriptor : public BaseDescriptor<DataFacadeT> {
http::Reply & reply
) {
facade = f;
reply.content.push_back(
"{\"status\":"
);
reply.content.push_back("{");
reply.content.push_back("\"status\":");
reply.setContentInsIndex(reply.content.size() - 1); // place to insert additional content

if(INT_MAX == raw_route.lengthOfShortestPath) {
//We do not need to do much, if there is no route ;-)
Expand Down
9 changes: 9 additions & 0 deletions Include/osrm/Reply.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,18 @@ class Reply {
static Reply StockReply(status_type status);
void setSize(const unsigned size);
Reply();

// Obtain iterator for inserting additional data into the content
std::vector<std::string>::iterator getContentInsIter() throw();

// Assing iterator for inserting data
inline void setContentInsIndex(ssize_t i) throw() { m_contentInsIndex = i; }

private:
static std::string ToString(Reply::status_type status);
boost::asio::const_buffer ToBuffer(Reply::status_type status);

ssize_t m_contentInsIndex;
};

}
Expand Down
6 changes: 6 additions & 0 deletions Include/osrm/RouteParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct RouteParameters {
geometry(true),
compression(true),
deprecatedAPI(false),
exec_time(false),
checkSum(-1)
{ }

Expand All @@ -54,6 +55,7 @@ struct RouteParameters {
bool geometry;
bool compression;
bool deprecatedAPI;
bool exec_time;
unsigned checkSum;
std::string service;
std::string outputFormat;
Expand All @@ -76,6 +78,10 @@ struct RouteParameters {
deprecatedAPI = true;
}

void setExecTimeFlag(const bool b) {
exec_time = b;
}

void setChecksum(const unsigned c) {
checkSum = c;
}
Expand Down
11 changes: 10 additions & 1 deletion Plugins/NearestPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +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/TimingUtil.h"

#include <boost/unordered_map.hpp>

Expand Down Expand Up @@ -65,13 +66,16 @@ class NearestPlugin : public BasePlugin {
return;
}

double _time_from = get_timestamp();
PhantomNode result;
facade->FindPhantomNodeForCoordinate(
routeParameters.coordinates[0],
result,
routeParameters.zoomLevel
);

double _time_diff = (get_timestamp() - _time_from) * 1000; // time difference in ms

std::string temp_string;
//json

Expand All @@ -81,7 +85,12 @@ class NearestPlugin : public BasePlugin {
}

reply.status = http::Reply::ok;
reply.content.push_back("{\"status\":");
reply.content.push_back("{");
if (routeParameters.exec_time) {
int64ToString(_time_diff, 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 {
Expand Down
23 changes: 17 additions & 6 deletions Plugins/ViaRoutePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +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/TimingUtil.h"

#include <boost/unordered_map.hpp>

Expand Down Expand Up @@ -81,6 +82,7 @@ class ViaRoutePlugin : public BasePlugin {
return;
}

double _time_from = get_timestamp();
RawRouteData rawRoute;
rawRoute.checkSum = facade->GetCheckSum();
const bool checksumOK = (routeParameters.checkSum == rawRoute.checkSum);
Expand Down Expand Up @@ -157,17 +159,12 @@ class ViaRoutePlugin : public BasePlugin {
descriptorConfig.encode_geometry = routeParameters.compression;

switch(descriptorType){
case 0:
desc = new JSONDescriptor<DataFacadeT>();

break;
case 1:
desc = new GPXDescriptor<DataFacadeT>();

break;
case 0:
default:
desc = new JSONDescriptor<DataFacadeT>();

break;
}

Expand All @@ -177,6 +174,20 @@ class ViaRoutePlugin : public BasePlugin {
desc->SetConfig(descriptorConfig);

desc->Run(rawRoute, phantomNodes, facade, reply);

if (routeParameters.exec_time) {
double _time_diff = (get_timestamp() - _time_from) * 1000; // time difference in ms
std::vector<std::string>::iterator it = reply.getContentInsIter();
if (it != reply.content.end()) {
std::string temp_string;
intToString((int)(_time_diff), temp_string);
reply.content.insert(it, (1 == descriptorType)?
"<exec_time_ms>" + temp_string + "</exec_time_ms>" : "\"exec_time_ms\":" + temp_string + ","
);
} else {
SimpleLogger().Write(logDEBUG) << "Can't find place to insert exec time metric";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to throw an exception as this is not expected to happen. On the other hand, finding a proper place to add the information should take O(1) time and making this check superfluous.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
}
if("" != routeParameters.jsonpParameter) {
reply.content.push_back(")\n");
}
Expand Down
5 changes: 3 additions & 2 deletions Server/APIGrammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ template <typename Iterator, class HandlerT>
struct APIGrammar : qi::grammar<Iterator> {
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) ) ;
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)];
Expand All @@ -52,6 +52,7 @@ struct APIGrammar : qi::grammar<Iterator> {
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)];
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_.-"));
Expand All @@ -61,7 +62,7 @@ struct APIGrammar : qi::grammar<Iterator> {
qi::rule<Iterator> api_call, query;
qi::rule<Iterator, std::string()> service, zoom, output, string, jsonp, checksum, location, hint,
stringwithDot, stringwithPercent, language, instruction, geometry,
cmp, alt_route, old_API;
cmp, alt_route, old_API, exec_time;

HandlerT * handler;
};
Expand Down
9 changes: 8 additions & 1 deletion Server/Http/Reply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,15 @@ boost::asio::const_buffer Reply::ToBuffer(Reply::status_type status) {
}


Reply::Reply() : status(ok) {
Reply::Reply() : status(ok), m_contentInsIndex(-1) {

}

std::vector<std::string>::iterator Reply::getContentInsIter() throw() {
if ((0 > m_contentInsIndex) || ((size_t)m_contentInsIndex >= content.size())) {
return content.end();
}
return content.begin() + (size_t)m_contentInsIndex;
}

} // end of namespace http