a ton of changes to fix the production directory. formerly this directory $FEISTY_ME...
[feisty_meow.git] / nucleus / applications / utilities / time_running_app.cpp
1 /*
2  * Name   : time_running_app
3  * Author : Chris Koeritz
4  * Purpose:
5  *
6  *   A simple test of how long it takes to run a fairly heavy application.
7  *
8  * Copyright (c) 2003-$now By Author.  This program is free software; you can 
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation; either version 2 of
11  * the License or (at your option) any later version.  This is online at:
12  *     http://www.fsf.org/copyleft/gpl.html
13  * Please send any updates to: fred@gruntose.com
14  */
15
16 #include <basis/astring.h>
17 #include <basis/environment.cpp>
18 #include <basis/functions.h>
19 #include <structures/set.h>
20 #include <timely/time_stamp.h>
21 #include <application/hoople_main.h>
22 #include <loggers/console_logger.h>
23 #include <loggers/file_logger.h>
24 #include <structures/static_memory_gremlin.h>
25
26 #include <stdio.h>
27
28 #ifdef __WIN32__
29   #include <io.h>
30 #endif
31
32 using namespace application;
33 using namespace basis;
34 using namespace loggers;
35 using namespace filesystem;
36 using namespace textual;
37 using namespace timely;
38
39 #undef BASE_LOG
40 #define BASE_LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
41 #undef LOG
42 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
43
44 class time_running_app : public application_shell
45 {
46 public:
47   time_running_app() : application_shell() {}
48   DEFINE_CLASS_NAME("time_running_app");
49   int execute();
50 };
51
52 int time_running_app::execute()
53 {
54   FUNCDEF("execute");
55   SETUP_COMBO_LOGGER;
56
57   int test_runs = 10000;
58
59   time_stamp start;  // start of test.
60   astring bins = environment::get("$BINDIR");
61   astring app = bins + "/example_application";
62   // save real stdout.
63   int real_stdout = dup(1);
64   // dump app's output.
65   freopen("/dev/null", "w", stdout);
66   for (int i = 0; i < test_runs; i++) {
67     // run the app; sorry about the relative path; change to this dir first.
68     int ret = system(app.s());
69     if (ret != 0) {
70       // restore real stdout.
71       dup2(real_stdout, 1);
72       LOG(astring("got an error running the app: ") + app);
73       exit(1);
74     }
75   }
76   time_stamp completion_time;  // end of test.
77   // restore real stdout.
78   dup2(real_stdout, 1);
79
80   double durat = completion_time.value() - start.value();
81   double secs = durat / 1000.0;   // convert to seconds.
82   LOG(a_sprintf("test run took %0.2f milliseconds or %0.2f seconds or %0.2f minutes.", durat, secs, secs / 60.0));
83   LOG(a_sprintf("individual call takes %0.0f milliseconds.", durat / double(test_runs)));
84
85   return 0;
86 }
87
88 HOOPLE_MAIN(time_running_app, )
89
90 #ifdef __BUILD_STATIC_APPLICATION__
91   // static dependencies found by buildor_gen_deps.sh:
92 #endif // __BUILD_STATIC_APPLICATION__
93