handy timing apps for set effective user id and launching an application.
[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 using namespace application;
29 using namespace basis;
30 using namespace loggers;
31 using namespace filesystem;
32 using namespace textual;
33 using namespace timely;
34
35 #undef BASE_LOG
36 #define BASE_LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
37 #undef LOG
38 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
39
40 class time_running_app : public application_shell
41 {
42 public:
43   time_running_app() : application_shell() {}
44   DEFINE_CLASS_NAME("time_running_app");
45   int execute();
46 };
47
48 int time_running_app::execute()
49 {
50   FUNCDEF("execute");
51   SETUP_COMBO_LOGGER;
52
53   int test_runs = 10000;
54
55   time_stamp start;  // start of test.
56   astring bins = environment::get("FEISTY_MEOW_DIR") + "/production/binaries";
57   astring app = bins + "/example_application";
58   // save real stdout.
59   int real_stdout = dup(1);
60   // dump app's output.
61   freopen("/dev/null", "w", stdout);
62   for (int i = 0; i < test_runs; i++) {
63     // run the app; sorry about the relative path; change to this dir first.
64     int ret = system(app.s());
65     if (ret != 0) {
66       // restore real stdout.
67       dup2(real_stdout, 1);
68       LOG(astring("got an error running the app: ") + app);
69       exit(1);
70     }
71   }
72   time_stamp completion_time;  // end of test.
73   // restore real stdout.
74   dup2(real_stdout, 1);
75
76   double durat = completion_time.value() - start.value();
77   double secs = durat / 1000.0;   // convert to seconds.
78   LOG(a_sprintf("test run took %0.2f milliseconds or %0.2f seconds or %0.2f minutes.", durat, secs, secs / 60.0));
79   LOG(a_sprintf("individual call takes %0.0f milliseconds.", durat / double(test_runs)));
80
81   return 0;
82 }
83
84 HOOPLE_MAIN(time_running_app, )
85
86 #ifdef __BUILD_STATIC_APPLICATION__
87   // static dependencies found by buildor_gen_deps.sh:
88 #endif // __BUILD_STATIC_APPLICATION__
89