21a7c59d70e01ee6ed6015a4754f77f857ddfac4
[feisty_meow.git] / nucleus / applications / utilities / time_set_effective_id.cpp
1 /*
2  * Name   : time_set_effective_id
3  * Author : Chris Koeritz
4  * Purpose:
5  *
6  *   A simple test of how long it takes to call set effective id on Unix.
7  *   For this to really work as designed, it should be run as root.
8  *
9  * Copyright (c) 2003-$now By Author.  This program is free software; you can 
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation; either version 2 of
12  * the License or (at your option) any later version.  This is online at:
13  *     http://www.fsf.org/copyleft/gpl.html
14  * Please send any updates to: fred@gruntose.com
15  */
16
17 #include <basis/functions.h>
18 #include <basis/astring.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 using namespace application;
27 using namespace basis;
28 using namespace loggers;
29 using namespace filesystem;
30 using namespace textual;
31 using namespace timely;
32
33 #undef BASE_LOG
34 #define BASE_LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
35 #undef LOG
36 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
37
38 class time_set_effective_id : public application_shell
39 {
40 public:
41   time_set_effective_id() : application_shell() {}
42   DEFINE_CLASS_NAME("time_set_effective_id");
43   int execute();
44 };
45
46 int time_set_effective_id::execute()
47 {
48   FUNCDEF("execute");
49   SETUP_COMBO_LOGGER;
50
51   int test_runs = 1000000;
52
53   time_stamp start;  // start of test.
54   for (int i = 0; i < test_runs; i++) {
55     // set effective id to fred.
56     int ret = seteuid(1008);
57     if (ret != 0) {
58       LOG("failure to change effective user id to normal user.");
59       exit(1);
60     }
61     // set effective id to root.
62     ret = seteuid(0);
63     if (ret != 0) {
64       LOG("failure to change effective user id to root.");
65       exit(1);
66     }
67   }
68   time_stamp completion_time;  // end of test.
69
70   double durat = completion_time.value() - start.value();
71   double secs = durat / 1000.0;   // convert to seconds.
72   LOG(a_sprintf("test run took %0.2f milliseconds or %0.2f seconds or %0.2f minutes.", durat, secs, secs / 60.0));
73   // divide by two because we're doing two calls above.
74   LOG(a_sprintf("individual call takes %0.0f milliseconds.", durat / double(test_runs) / 2.0));
75
76   return 0;
77 }
78
79 HOOPLE_MAIN(time_set_effective_id, )
80
81 #ifdef __BUILD_STATIC_APPLICATION__
82   // static dependencies found by buildor_gen_deps.sh:
83 #endif // __BUILD_STATIC_APPLICATION__
84