summaryrefslogtreecommitdiff
path: root/performance-tests/Misc/test_naming.cpp
blob: a7f3225a552c7a2ae307e26188f74c8f16f83d2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// $Id$

// ============================================================================
//
// = LIBRARY
//    performance_tests
//
// = FILENAME
//    test_naming.cpp
//
// = DESCRIPTION
//      This is an example to do performance testing of the Naming Service
//      using both the normal Memory Pool as well as the light Memory Pool.
//
// = AUTHOR
//    Prashant Jain
//
// ============================================================================

#include "ace/SString.h"
#include "ace/Naming_Context.h"
#include "ace/Profile_Timer.h"

ACE_RCSID(Misc, test_naming, "$Id$")

#if defined (ACE_HAS_WCHAR)

#define ACE_NS_MAX_ENTRIES 4000

static char name[BUFSIZ];
static char value[BUFSIZ];
static char type[BUFSIZ];

void
bind (ACE_Naming_Context *ns_context, int result)
{
  // do the binds
  for (int i = 1; i <= ACE_NS_MAX_ENTRIES; i++)
    {
      if (i % 50 == 0)
        ACE_DEBUG ((LM_DEBUG, "."));
      ACE_OS::sprintf (name, "%s%d", "name", i);
      ACE_WString w_name (name);

      ACE_OS::sprintf (value, "%s%d", "value", i);
      ACE_WString w_value (value);

      ACE_OS::sprintf (type, "%s%d", "type", i);
      ACE_ASSERT (ns_context->bind (w_name, w_value, type) == result);
    }
  ACE_DEBUG ((LM_DEBUG, "\n"));
}

void
rebind (ACE_Naming_Context *ns_context, int result)
{
  // do the rebinds
  for (int i = 1; i <= ACE_NS_MAX_ENTRIES; i++)
    {
      ACE_OS::sprintf (name, "%s%d", "name", i);
      ACE_WString w_name (name);
      ACE_OS::sprintf (value, "%s%d", "value", -i);
      ACE_WString w_value (value);
      ACE_OS::sprintf (type, "%s%d", "type", -i);
      ACE_ASSERT (ns_context->rebind (w_name, w_value, type) == result);
    }
}

void
unbind (ACE_Naming_Context *ns_context, int result)
{
  // do the unbinds
  for (int i = 1; i <= ACE_NS_MAX_ENTRIES; i++)
    {
      ACE_OS::sprintf (name, "%s%d", "name", i);
      ACE_WString w_name (name);
      ACE_ASSERT (ns_context->unbind (w_name) == result);
    }
}

void
find (ACE_Naming_Context *ns_context, int sign, int result)
{
  char temp_val[BUFSIZ];
  char temp_type[BUFSIZ];

  // do the finds
  for (int i = 1; i <= ACE_NS_MAX_ENTRIES; i++)
    {
      ACE_OS::sprintf (name, "%s%d", "name", i);
      ACE_WString w_name (name);

      ACE_WString w_value;
      char *type_out;

      if (sign == 1)
        {
          ACE_OS::sprintf (temp_val, "%s%d", "value", i);
          ACE_OS::sprintf (temp_type, "%s%d", "type", i);
        }
      else
        {
          ACE_OS::sprintf (temp_val, "%s%d", "value", -i);
          ACE_OS::sprintf (temp_type, "%s%d", "type", -i);
        }

      ACE_WString val (temp_val);

      int resolve_result = ns_context->resolve (w_name, w_value, type_out);
      ACE_ASSERT (resolve_result == result);
      ACE_UNUSED_ARG (resolve_result); // To avoid compile warning
                                       // with ACE_NDEBUG.

      if (w_value.char_rep ())
        {
          ACE_DEBUG ((LM_DEBUG, "Name: %s\tValue: %s\tType: %s\n",
                      name, w_value.char_rep (), type_out));
          ACE_ASSERT (w_value == val);
          if (type_out)
            {
              ACE_ASSERT (::strcmp (type_out, temp_type) == 0);
              delete[] type_out;
            }
        }
    }
}

void do_testing (int argc, char *argv[], int light)
{
  ACE_Profile_Timer timer;

  ACE_Naming_Context ns_context;
  ACE_Name_Options *name_options = ns_context.name_options ();
  name_options->parse_args (argc, argv);

  if (light == 0)  // Use SYNC
    {
      name_options->database (ACE::basename (name_options->process_name (),
                                             ACE_DIRECTORY_SEPARATOR_CHAR));
      ns_context.open (ACE_Naming_Context::PROC_LOCAL);
    }
  else  // Use NO-SYNC
    {
      const char *p = ACE::basename (name_options->process_name (),
                                     ACE_DIRECTORY_SEPARATOR_CHAR);
      char s[5 /* strlen ("light") */ + MAXNAMELEN + 1];
      ACE_OS::sprintf (s, "light%s", p);
      name_options->database (s);
      ns_context.open (ACE_Naming_Context::PROC_LOCAL, 1);
    }

  // Add bindings to the database
  ACE_DEBUG ((LM_DEBUG, "Binding\n"));

  timer.start ();
  bind (&ns_context, 0);

  ACE_DEBUG ((LM_DEBUG, "Unbinding\n"));
  unbind (&ns_context, 0);
  timer.stop ();

  ACE_Profile_Timer::ACE_Elapsed_Time et;

  timer.elapsed_time (et);
  ACE_DEBUG ((LM_DEBUG, "real time = %f secs, user time = %f secs, system time = %f secs\n",
              et.real_time, et.user_time, et.system_time));
}

#endif /* ACE_HAS_WCHAR */

int
main (int argc, char *argv[])
{
#if defined (ACE_HAS_WCHAR)
  // Do testing with SYNC on
  ACE_DEBUG ((LM_DEBUG, "SYNC is ON\n"));
  do_testing (argc, argv, 0);

  // Do testing with SYNC off
  ACE_DEBUG ((LM_DEBUG, "SYNC is OFF\n"));
  do_testing (argc, argv, 1);
#else /* ACE_HAS_WCHAR */
  ACE_ERROR ((LM_INFO, "Naming isn't supported without wchar_t\n"));
#endif /* ACE_HAS_WCHAR */
  return 0;
}