This commit is contained in:
Siarhei Fedartsou 2024-06-17 09:35:37 +02:00
parent 930329b7aa
commit d6927af252

View File

@ -117,9 +117,10 @@ class GPSTraces
class Statistics
{
public:
void push(double timeMs, int /*iteration*/ = 0)
explicit Statistics(int iterations) : times(iterations) {}
void push(double timeMs, int iteration)
{
times.push_back(timeMs);
times[iteration].push_back(timeMs);
sorted = false;
}
@ -128,16 +129,16 @@ class Statistics
double sum()
{
double sum = 0;
for (auto time : times)
for (auto time : times[0])
{
sum += time;
}
return sum;
}
double min() { return *std::min_element(times.begin(), times.end()); }
double min() { return *std::min_element(times[0].begin(), times[0].end()); }
double max() { return *std::max_element(times.begin(), times.end()); }
double max() { return *std::max_element(times[0].begin(), times[0].end()); }
double percentile(double p)
{
@ -150,13 +151,14 @@ class Statistics
{
if (!sorted)
{
std::sort(times.begin(), times.end());
std::sort(times[0].begin(), times[0].end());
sorted = true;
}
return times;
return times[0];
}
std::vector<double> times;
// vector of times for each iteration
std::vector<std::vector<double>> times;
bool sorted = false;
};
@ -186,7 +188,7 @@ void runRouteBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces, int iterati
auto run_benchmark = [&](const Benchmark &benchmark)
{
Statistics statistics;
Statistics statistics{iterations};
for (int iteration = 0; iteration < iterations; ++iteration)
{
@ -266,7 +268,7 @@ void runRouteBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces, int iterati
}
}
void runMatchBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
void runMatchBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces, int iterations)
{
struct Benchmark
{
@ -276,9 +278,12 @@ void runMatchBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
auto run_benchmark = [&](const Benchmark &benchmark)
{
Statistics statistics;
Statistics statistics{iterations};
auto NUM = 1000;
for (int iteration = 0; iteration < iterations; ++iteration)
{
gpsTraces.resetSeed();
for (int i = 0; i < NUM; ++i)
{
engine::api::ResultT result = json::Object();
@ -298,7 +303,7 @@ void runMatchBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
const auto rc = osrm.Match(params, result);
TIMER_STOP(match);
statistics.push(TIMER_MSEC(match));
statistics.push(TIMER_MSEC(match), iteration);
auto &json_result = std::get<json::Object>(result);
if (rc != Status::Ok ||
@ -311,6 +316,7 @@ void runMatchBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
}
}
}
}
std::cout << benchmark.name << std::endl;
std::cout << statistics << std::endl;
@ -326,7 +332,7 @@ void runMatchBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
}
}
void runNearestBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
void runNearestBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces, int iterations)
{
struct Benchmark
{
@ -336,8 +342,11 @@ void runNearestBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
auto run_benchmark = [&](const Benchmark &benchmark)
{
Statistics statistics;
Statistics statistics{iterations};
auto NUM = 10000;
for (int iteration = 0; iteration < iterations; ++iteration)
{
gpsTraces.resetSeed();
for (int i = 0; i < NUM; ++i)
{
engine::api::ResultT result = json::Object();
@ -353,7 +362,7 @@ void runNearestBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
const auto rc = osrm.Nearest(params, result);
TIMER_STOP(nearest);
statistics.push(TIMER_MSEC(nearest));
statistics.push(TIMER_MSEC(nearest), iteration);
auto &json_result = std::get<json::Object>(result);
if (rc != Status::Ok ||
@ -366,6 +375,7 @@ void runNearestBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
}
}
}
}
std::cout << benchmark.name << std::endl;
std::cout << statistics << std::endl;
@ -381,7 +391,7 @@ void runNearestBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
}
}
void runTripBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
void runTripBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces, int iterations)
{
struct Benchmark
{
@ -391,8 +401,11 @@ void runTripBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
auto run_benchmark = [&](const Benchmark &benchmark)
{
Statistics statistics;
Statistics statistics{iterations};
auto NUM = 1000;
for (int iteration = 0; iteration < iterations; ++iteration)
{
gpsTraces.resetSeed();
for (int i = 0; i < NUM; ++i)
{
engine::api::ResultT result = json::Object();
@ -408,10 +421,11 @@ void runTripBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
const auto rc = osrm.Trip(params, result);
TIMER_STOP(trip);
statistics.push(TIMER_MSEC(trip));
statistics.push(TIMER_MSEC(trip), iteration);
auto &json_result = std::get<json::Object>(result);
if (rc != Status::Ok || json_result.values.find("trips") == json_result.values.end())
if (rc != Status::Ok ||
json_result.values.find("trips") == json_result.values.end())
{
auto code = std::get<json::String>(json_result.values["code"]).value;
if (code != "NoSegment")
@ -420,6 +434,7 @@ void runTripBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
}
}
}
}
std::cout << benchmark.name << std::endl;
std::cout << statistics << std::endl;
@ -436,7 +451,7 @@ void runTripBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
run_benchmark(benchmark);
}
}
void runTableBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
void runTableBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces, int iterations)
{
struct Benchmark
{
@ -446,8 +461,12 @@ void runTableBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
auto run_benchmark = [&](const Benchmark &benchmark)
{
Statistics statistics;
Statistics statistics{iterations};
auto NUM = 250;
for (int iteration = 0; iteration < iterations; ++iteration)
{
gpsTraces.resetSeed();
for (int i = 0; i < NUM; ++i)
{
engine::api::ResultT result = json::Object();
@ -462,7 +481,7 @@ void runTableBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
const auto rc = osrm.Table(params, result);
TIMER_STOP(table);
statistics.push(TIMER_MSEC(table));
statistics.push(TIMER_MSEC(table), iteration);
auto &json_result = std::get<json::Object>(result);
if (rc != Status::Ok ||
@ -475,6 +494,7 @@ void runTableBenchmark(const OSRM &osrm, const GPSTraces &gpsTraces)
}
}
}
}
std::cout << benchmark.name << std::endl;
std::cout << statistics << std::endl;
@ -526,19 +546,19 @@ try
}
else if (benchmarkToRun == "match")
{
runMatchBenchmark(osrm, gpsTraces);
runMatchBenchmark(osrm, gpsTraces, iterations);
}
else if (benchmarkToRun == "nearest")
{
runNearestBenchmark(osrm, gpsTraces);
runNearestBenchmark(osrm, gpsTraces, iterations);
}
else if (benchmarkToRun == "trip")
{
runTripBenchmark(osrm, gpsTraces);
runTripBenchmark(osrm, gpsTraces, iterations);
}
else if (benchmarkToRun == "table")
{
runTableBenchmark(osrm, gpsTraces);
runTableBenchmark(osrm, gpsTraces, iterations);
}
else
{