Skip to content

Commit

Permalink
Adding execution time measurement facility
Browse files Browse the repository at this point in the history
  • Loading branch information
vershov committed Feb 19, 2014
1 parent d73f3eb commit 4efcca3
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
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),
measurement(false),
checkSum(-1)
{ }

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

void setMeasurementFlag(const bool b) {
measurement = b;
}

void setChecksum(const unsigned c) {
checkSum = c;
}
Expand Down
8 changes: 8 additions & 0 deletions 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/TimeMeasurement.h"

#include <boost/unordered_map.hpp>

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

TimeMeasurement measure;
PhantomNode result;
facade->FindPhantomNodeForCoordinate(
routeParameters.coordinates[0],
result,
routeParameters.zoomLevel
);

int64_t time_ms = measure.toNow();

std::string temp_string;
//json

Expand All @@ -87,6 +91,10 @@ class NearestPlugin : public BasePlugin {
} 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);
Expand Down
20 changes: 20 additions & 0 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/TimeMeasurement.h"

#include <boost/unordered_map.hpp>

Expand All @@ -46,6 +47,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <string>
#include <vector>

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 DataFacadeT>
class ViaRoutePlugin : public BasePlugin {
private:
Expand Down Expand Up @@ -81,6 +90,7 @@ class ViaRoutePlugin : public BasePlugin {
return;
}

TimeMeasurement measure;
RawRouteData rawRoute;
rawRoute.checkSum = facade->GetCheckSum();
const bool checksumOK = (routeParameters.checkSum == rawRoute.checkSum);
Expand Down Expand Up @@ -177,6 +187,16 @@ class ViaRoutePlugin : public BasePlugin {
desc->SetConfig(descriptorConfig);

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

if (routeParameters.measurement) {
int64_t time_ms = measure.toNow();
std::vector<std::string>::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.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 | measurement) ) ;

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)];
measurement = (-qi::lit('&')) >> qi::lit("measurement") >> '=' >> qi::bool_[boost::bind(&HandlerT::setMeasurementFlag, 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, measurement;

HandlerT * handler;
};
Expand Down
51 changes: 51 additions & 0 deletions Util/TimeMeasurement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
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 <boost/date_time/posix_time/posix_time.hpp>

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_

0 comments on commit 4efcca3

Please sign in to comment.