From 0c57e063777a97a1f402103c2875222fac6f5a09 Mon Sep 17 00:00:00 2001
From: Scott Giese <scttgs0@gmail.com>
Date: Mon, 16 Jul 2018 12:31:46 -0500
Subject: [PATCH] Benchmarking: Measure execution time

---
 src/Airports/GenAirports850/main.cxx | 19 +++++++----
 src/BuildTiles/Main/main.cxx         | 14 ++++++--
 src/Prep/DemChop/demchop.cxx         | 48 +++++++++++++++-------------
 src/Prep/DemChop/hgtchop.cxx         |  7 ++++
 src/Prep/DemChop/srtmchop.cxx        | 16 +++++++---
 src/Prep/GDALChop/gdalchop.cxx       | 14 ++++++--
 src/Prep/OGRDecode/ogr-decode.cxx    |  9 +++++-
 src/Prep/TerraFit/terrafit.cc        |  9 ++++++
 8 files changed, 97 insertions(+), 39 deletions(-)

diff --git a/src/Airports/GenAirports850/main.cxx b/src/Airports/GenAirports850/main.cxx
index c3fa36d0..b6abdb88 100644
--- a/src/Airports/GenAirports850/main.cxx
+++ b/src/Airports/GenAirports850/main.cxx
@@ -13,6 +13,7 @@
 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
 
+#include <chrono>
 #include <memory>
 #include <string>
 #include <iostream>
@@ -126,6 +127,8 @@ double slope_eps = 0.00001;
 
 int main(int argc, char **argv)
 {
+    auto start_time = std::chrono::high_resolution_clock::now();
+
     SGGeod min = SGGeod::fromDeg( -180, -90 );
     SGGeod max = SGGeod::fromDeg( 180, 90 );
     long  position = 0;
@@ -277,7 +280,7 @@ int main(int argc, char **argv)
         else 
         {
     	    usage( argc, argv );
-    	    exit(-1);
+    	    return EXIT_FAILURE;
     	}
     }
 
@@ -318,20 +321,20 @@ int main(int argc, char **argv)
     {
     	TG_LOG( SG_GENERAL, SG_ALERT, "Error: no work directory specified." );
     	usage( argc, argv );
-	    exit(-1);
+	    return EXIT_FAILURE;
     }
 
     if ( input_file == "" ) 
     {
     	TG_LOG( SG_GENERAL, SG_ALERT,  "Error: no input file." );
-    	exit(-1);
+    	return EXIT_FAILURE;
     }
 
     sg_gzifstream in( input_file );
     if ( !in.is_open() ) 
     {
         TG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << input_file );
-        exit(-1);
+        return EXIT_FAILURE;
     }
 
     // try to figure out which apt.dat version we are reading and bail if version is unsupported
@@ -342,7 +345,7 @@ int main(int argc, char **argv)
     int code = atoi(line);
     if (code == 810 || code > 1100) {
         TG_LOG(SG_GENERAL, SG_ALERT, "ERROR: This genapts version does not support apt.data version " << code << " files.");
-        exit(-1);
+        return EXIT_FAILURE;
     }
 
     // Create the scheduler
@@ -390,5 +393,9 @@ int main(int argc, char **argv)
 
     TG_LOG(SG_GENERAL, SG_INFO, "Genapts finished successfully");
 
-    return 0;
+    auto finish_time = std::chrono::high_resolution_clock::now();
+    std::chrono::duration<double> elapsed = finish_time - start_time;
+    std::cout << std::endl << "Elapsed time: " << elapsed.count() << " seconds" << std::endl << std::endl;
+
+    return EXIT_SUCCESS;
 }
diff --git a/src/BuildTiles/Main/main.cxx b/src/BuildTiles/Main/main.cxx
index cf2ad9b3..5e58c3f8 100644
--- a/src/BuildTiles/Main/main.cxx
+++ b/src/BuildTiles/Main/main.cxx
@@ -29,6 +29,9 @@
 #  define tgSleep(x) sleep(x)
 #endif
 
+#include <chrono>
+#include <iostream>
+
 #include <boost/thread.hpp>
 
 #include <simgear/debug/logstream.hxx>
@@ -96,6 +99,8 @@ int main(int argc, char **argv) {
     vector<string> debug_shape_defs;
     vector<string> debug_area_defs;
 
+    auto start_time = std::chrono::high_resolution_clock::now();
+
     sglog().setLogLevels( SG_ALL, SG_INFO );
 
     //
@@ -181,7 +186,7 @@ int main(int argc, char **argv) {
 
     TGAreaDefinitions areas;
     if ( areas.init( priorities_file ) ) {
-        exit( -1 );
+        return EXIT_FAILURE;
     }
 
     // tile work queue 
@@ -336,5 +341,10 @@ int main(int argc, char **argv) {
     constructs.clear();
 
     SG_LOG(SG_GENERAL, SG_ALERT, "[Finished successfully]");
-    return 0;
+    
+    auto finish_time = std::chrono::high_resolution_clock::now();
+    std::chrono::duration<double> elapsed = finish_time - start_time;
+    std::cout << std::endl << "Elapsed time: " << elapsed.count() << " seconds" << std::endl << std::endl;
+
+    return EXIT_SUCCESS;
 }
diff --git a/src/Prep/DemChop/demchop.cxx b/src/Prep/DemChop/demchop.cxx
index 7bd9d11c..753fc0c0 100644
--- a/src/Prep/DemChop/demchop.cxx
+++ b/src/Prep/DemChop/demchop.cxx
@@ -27,6 +27,7 @@
 
 #include <simgear/compiler.h>
 
+#include <chrono>
 #include <iostream>
 #include <string>
 
@@ -47,11 +48,12 @@ int main(int argc, char **argv) {
     sglog().setLogLevels( SG_ALL, SG_WARN );
 
     if ( argc != 3 ) {
-	SG_LOG( SG_GENERAL, SG_ALERT, 
-		"Usage " << argv[0] << " <dem_file> <work_dir>" );
-	exit(-1);
+        SG_LOG( SG_GENERAL, SG_ALERT, "Usage " << argv[0] << " <dem_file> <work_dir>" );
+        return EXIT_FAILURE;
     }
 
+    auto start_time = std::chrono::high_resolution_clock::now();
+
     string dem_name = argv[1];
     string work_dir = argv[2];
 
@@ -72,29 +74,31 @@ int main(int argc, char **argv) {
     SGBucket b_max( max );
 
     if ( b_min == b_max ) {
-	dem.write_area( work_dir, b_min );
+        dem.write_area( work_dir, b_min );
     } else {
-	SGBucket b_cur;
-	int dx, dy, i, j;
+        SGBucket b_cur;
+        int dx, dy, i, j;
 
-	sgBucketDiff(b_min, b_max, &dx, &dy);
-	cout << "DEM file spans tile boundaries" << endl;
-	cout << "  dx = " << dx << "  dy = " << dy << endl;
+        sgBucketDiff(b_min, b_max, &dx, &dy);
+        cout << "DEM file spans tile boundaries" << endl;
+        cout << "  dx = " << dx << "  dy = " << dy << endl;
 
-	if ( (dx > 20) || (dy > 20) ) {
-	    cout << "somethings really wrong!!!!" << endl;
-	    exit(-1);
-	}
+        if ( (dx > 20) || (dy > 20) ) {
+            cout << "somethings really wrong!!!!" << endl;
+            return EXIT_FAILURE;
+        }
 
-	for ( j = 0; j <= dy; j++ ) {
-	    for ( i = 0; i <= dx; i++ ) {
-		b_cur = b_min.sibling(i, j);
-		dem.write_area( work_dir, b_cur );
-	    }
-	}
+        for ( j = 0; j <= dy; j++ ) {
+            for ( i = 0; i <= dx; i++ ) {
+                b_cur = b_min.sibling(i, j);
+                dem.write_area( work_dir, b_cur );
+            }
+        }
     }
 
-    return 0;
+    auto finish_time = std::chrono::high_resolution_clock::now();
+    std::chrono::duration<double> elapsed = finish_time - start_time;
+    std::cout << std::endl << "Elapsed time: " << elapsed.count() << " seconds" << std::endl << std::endl;
+
+    return EXIT_SUCCESS;
 }
-
-
diff --git a/src/Prep/DemChop/hgtchop.cxx b/src/Prep/DemChop/hgtchop.cxx
index 9217a453..637b8c80 100644
--- a/src/Prep/DemChop/hgtchop.cxx
+++ b/src/Prep/DemChop/hgtchop.cxx
@@ -26,6 +26,7 @@
 
 #include <simgear/compiler.h>
 
+#include <chrono>
 #include <cstdlib>
 #include <string>
 #include <iostream>
@@ -55,6 +56,8 @@ int main(int argc, char **argv) {
         return EXIT_FAILURE;
     }
 
+    auto start_time = std::chrono::high_resolution_clock::now();
+
     int resolution = std::stoi(string(argv[1]));
     string hgt_name = string(argv[2]);
     string work_dir = string(argv[3]);
@@ -104,5 +107,9 @@ int main(int argc, char **argv) {
         }
     }
 
+    auto finish_time = std::chrono::high_resolution_clock::now();
+    std::chrono::duration<double> elapsed = finish_time - start_time;
+    std::cout << std::endl << "Elapsed time: " << elapsed.count() << " seconds" << std::endl << std::endl;
+
     return EXIT_SUCCESS;
 }
diff --git a/src/Prep/DemChop/srtmchop.cxx b/src/Prep/DemChop/srtmchop.cxx
index bd5539e2..5866951a 100644
--- a/src/Prep/DemChop/srtmchop.cxx
+++ b/src/Prep/DemChop/srtmchop.cxx
@@ -27,6 +27,7 @@
 
 #include <simgear/compiler.h>
 
+#include <chrono>
 #include <string>
 #include <iostream>
 #include <iomanip>
@@ -331,12 +332,13 @@ int main(int argc, char **argv) {
     sglog().setLogLevels( SG_ALL, SG_WARN );
 
     if ( argc != 3 ) {
-        cout << "Usage " << argv[0] << " <hgt_file> <work_dir>"
-             << endl;
+        cout << "Usage " << argv[0] << " <hgt_file> <work_dir>" << endl;
         cout << endl;
-        exit(-1);
+        return EXIT_FAILURE;
     }
 
+    auto start_time = std::chrono::high_resolution_clock::now();
+
     string hgt_name = argv[1];
     string work_dir = argv[2];
 
@@ -368,7 +370,7 @@ int main(int argc, char **argv) {
 
         if ( (dx > 50) || (dy > 50) ) {
             cout << "somethings really wrong!!!!" << endl;
-            exit(-1);
+            return EXIT_FAILURE;
         }
 
         for ( j = 0; j <= dy; j++ ) {
@@ -379,5 +381,9 @@ int main(int argc, char **argv) {
         }
     }
 
-    return 0;
+    auto finish_time = std::chrono::high_resolution_clock::now();
+    std::chrono::duration<double> elapsed = finish_time - start_time;
+    std::cout << std::endl << "Elapsed time: " << elapsed.count() << " seconds" << std::endl << std::endl;
+
+    return EXIT_SUCCESS;
 }
diff --git a/src/Prep/GDALChop/gdalchop.cxx b/src/Prep/GDALChop/gdalchop.cxx
index cfd87886..a89f7bee 100644
--- a/src/Prep/GDALChop/gdalchop.cxx
+++ b/src/Prep/GDALChop/gdalchop.cxx
@@ -23,6 +23,8 @@
 //
 
 #include <algorithm>
+#include <chrono>
+#include <iostream>
 
 #include <simgear/compiler.h>
 #include <simgear/bucket/newbucket.hxx>
@@ -426,9 +428,11 @@ int main(int argc, const char **argv)
     if ( argc < 3 ) {
         SG_LOG(SG_GENERAL, SG_ALERT,
                "Usage " << argv[0] << " <work_dir> <datasetname...> [-- <bucket-idx> ...]");
-        exit(-1);
+        return EXIT_FAILURE;
     }
 
+    auto start_time = std::chrono::high_resolution_clock::now();
+
     SGPath work_dir(argv[1]);
     work_dir.create_dir( 0755 );
 
@@ -471,7 +475,7 @@ int main(int argc, const char **argv)
             SG_LOG(SG_GENERAL, SG_ALERT,
                    "Could not open dataset '" << datasetnames[i] << "'"
                    ":" << CPLGetLastErrorMsg());
-            exit(1);
+            return EXIT_FAILURE;
         }
 
         images[i] = new ImageInfo(dataset);
@@ -528,5 +532,9 @@ int main(int argc, const char **argv)
         }
     }
 
-    return 0;
+    auto finish_time = std::chrono::high_resolution_clock::now();
+    std::chrono::duration<double> elapsed = finish_time - start_time;
+    std::cout << std::endl << "Elapsed time: " << elapsed.count() << " seconds" << std::endl << std::endl;
+
+    return EXIT_SUCCESS;
 }
diff --git a/src/Prep/OGRDecode/ogr-decode.cxx b/src/Prep/OGRDecode/ogr-decode.cxx
index d32dd553..ced8f8de 100644
--- a/src/Prep/OGRDecode/ogr-decode.cxx
+++ b/src/Prep/OGRDecode/ogr-decode.cxx
@@ -22,6 +22,7 @@
 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 //
 
+#include <chrono>
 #include <string>
 #include <map>
 
@@ -536,6 +537,8 @@ int main( int argc, char **argv ) {
     char* progname=argv[0];
     string datasource,work_dir;
 
+    auto start_time = std::chrono::high_resolution_clock::now();
+
     sglog().setLogLevels( SG_ALL, SG_INFO );
 
     while (argc>1) {
@@ -718,5 +721,9 @@ int main( int argc, char **argv ) {
     SG_LOG(SG_GENERAL, SG_ALERT, "Saving to buckets");
     results.Save( save_shapefiles );
 
-    return 0;
+    auto finish_time = std::chrono::high_resolution_clock::now();
+    std::chrono::duration<double> elapsed = finish_time - start_time;
+    std::cout << std::endl << "Elapsed time: " << elapsed.count() << " seconds" << std::endl << std::endl;
+
+    return EXIT_SUCCESS;
 }
diff --git a/src/Prep/TerraFit/terrafit.cc b/src/Prep/TerraFit/terrafit.cc
index b77f108e..071c5d7b 100644
--- a/src/Prep/TerraFit/terrafit.cc
+++ b/src/Prep/TerraFit/terrafit.cc
@@ -23,6 +23,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#include <chrono>
 #include <string>
 #include <stdio.h>
 #include <errno.h>
@@ -301,6 +302,8 @@ int main(int argc, char** argv) {
     sglog().setLogLevels( SG_ALL, SG_INFO );
     int option;
 
+    auto start_time = std::chrono::high_resolution_clock::now();
+
     while ((option=getopt_long(argc,argv,"hm:x:e:fvj:",options,NULL))!=-1) {
         switch (option) {
             case 'h':
@@ -362,4 +365,10 @@ int main(int argc, char** argv) {
     }
 
     SG_LOG(SG_GENERAL, SG_INFO, "Work queue is empty\n");
+
+    auto finish_time = std::chrono::high_resolution_clock::now();
+    std::chrono::duration<double> elapsed = finish_time - start_time;
+    std::cout << std::endl << "Elapsed time: " << elapsed.count() << " seconds" << std::endl << std::endl;
+
+    return EXIT_SUCCESS;
 }