summaryrefslogtreecommitdiff
path: root/TAO/tao/t-xdr.cpp
blob: 6847c93ef2384e75b2d6d25ec2868e40caf86758 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// XDR record stream ... test encode performance against CDR, using
// normal kinds of inlining performance hacks
//
// XXX as of 3-Nov-95 XDR_stream should only be relied on to marshal
// the simplest primitives ... not objrefs, typecodes, etc.  Also, the
// handling of sequences of chars/octets/shorts/wchars is wrong.
//
// It's suitable only for a simple performance test just now ...

#include "tao/corba.h"
#include "tao/xdr.h"

// A structure that's somewhat representative of an IIOP message in
// terms of overall complexity, so that its encoding cost is
// "interesting"

typedef CORBA_SEQUENCE <CORBA_Octet> opaque;

struct interesting 
{
  // A call that's mostly going to be "on the mark" for IIOP-ish
  // messages with small numbers of parameters:

  // 4 chars magic
  // 2 bytes version
  // 1 byte byte-order
  // 1 byte message type		---> 8 bytes "pure header"
  CORBA_Char		hdr_bytes [8];
  // 1 word message size		--=> end of GIOP::MessageHeader
  CORBA_ULong		hdr_len;

  // Service Context (1 word min)
  CORBA_ULong		empty_ctx;
  // 1 word request id
  CORBA_ULong		request_id;
  // 1 byte response-expected flag
  CORBA_Boolean	response_expected;
  // [ CDR:  3 bytes padding ]
  // opaque object key (1 word min; typically less than 16 bytes)
  opaque		object_key;
  // string operation name (non-empty ... often less than 10 bytes)
  CORBA_String	opname;
  // Principal client (1 word min)
  opaque		client_id;

  //				--=> END OF GIOP::RequestHeader

  // ... small number of parameters with any significance

  // 2 word parameters
  CORBA_ULong		param1;
  CORBA_ULong		param2;
  // 1 string parameter
  CORBA_String	param3;

};

// XXX declare CDR typecode for above type ... initialize and use
// one instance in the test below

extern CORBA_TypeCode	TC_opaque;

static void
do_test (int use_XDR,
	 CORBA_TypeCode_ptr tc,
	 void *data)
{
  u_int loopcount = 100 * 1000;
  u_int i;
  u_int error_count = 0;
  ACE_Time_Value before, after;
  CORBA_String opname = "kill_husband";
  opaque key;
  CORBA_Boolean status;

  key.buffer = (CORBA_Octet *) "jacqueline";
  key.length = key.maximum = 10;

  before = ACE_OS::gettimeofday ();

  if (use_XDR) 
    {
      // Using XDR APIs and encoding rules ...  encode the structure
      // repeatedly

      for (i = 0; i < loopcount; i++) 
        {
          CORBA_Environment env;
          XDR_stream stream (-1);

          // GIOP header plus most of request header
          status = status
            && stream.put_long ('GIOP')		// magic
            && stream.put_long ('\01\01\01\01')	// version etc
            && stream.put_long (99)		// msg len
            && stream.put_long (0)		// no svc ctx
            && stream.put_long (42)		// request ID
            && stream.put_boolean (CORBA_B_TRUE)// response?
            ;

          if (status)
            status = XDR_stream::encoder (&TC_opaque, &key, 0, &stream, env)
              == CORBA_TypeCode::TRAVERSE_CONTINUE;

          if (status)
            status = XDR_stream::encoder (_tc_CORBA_String, &opname,
                                          0, &stream, env)
              == CORBA_TypeCode::TRAVERSE_CONTINUE;

          // Parameters:  two longs, a string
          status = status
            && stream.put_long (99)
            && stream.put_long (-3455);
          if (status)
            status = XDR_stream::encoder (_tc_CORBA_String, &opname,
                                          0, &stream, env)
              == CORBA_TypeCode::TRAVERSE_CONTINUE;

          // Gratuitous extra "interesting" data
          status = XDR_stream::encoder (tc, data, 0, &stream, env)
            == CORBA_TypeCode::TRAVERSE_CONTINUE;

          if (status != CORBA_B_TRUE)
            error_count++;
        }
    } 
  else 
    {
      // This branch is the same, but using CDR APIs and encoding ...
      // encode the structure repeatedly

      for (i = 0; i < loopcount; i++) 
        {
          CORBA_Environment	env;
          u_char	buffer [CDR::DEFAULT_BUFSIZE];
          CDR			stream (buffer, sizeof buffer);

          // GIOP header plus most of request header
          status = status
            && stream.put_long ('GIOP')		// magic
            && stream.put_long ('\01\01\01\01')	// version etc
            && stream.put_long (99)		// msg len
            && stream.put_long (0)		// no svc ctx
            && stream.put_long (42)		// request ID
            && stream.put_boolean (CORBA_B_TRUE)// response?
            ;

          if (status)
            status = stream.encode (&TC_opaque, &key, 0, env)
              == CORBA_TypeCode::TRAVERSE_CONTINUE;

          if (status)
            status = stream.encode (_tc_CORBA_String, &opname,
                                    0, env)
              == CORBA_TypeCode::TRAVERSE_CONTINUE;

          // Parameters:  two longs, a string
          status = status
            && stream.put_long (99)
            && stream.put_long (-3455);
          if (status)
            status = stream.encode (_tc_CORBA_String, &opname,
                                    0, env)
              == CORBA_TypeCode::TRAVERSE_CONTINUE;

          // Gratuitous extra "interesting" data
          status = stream.encode (tc, data, 0, env)
            == CORBA_TypeCode::TRAVERSE_CONTINUE;


          if (status != CORBA_B_TRUE)
            error_count++;
        }

    }

  after = ACE_OS::gettimeofday ();

  if (loopcount > 0) 
    {
      if (error_count == 0) 
        {
          ACE_Time_Value diff = after - before;
          u_long usecs = diff.sec() * 1000 * 1000 + diff.usec();

          ACE_OS::printf ("%s average encode time\t= %ld.%.03ldms, \t"
                          "%ld calls/second\n",
                          use_XDR ? "XDR" : "CDR",
                          usecs / 1000, usecs % 1000,
                          1000000L / usecs);
        }

      ACE_OS::printf ("%d calls, %d errors\n", loopcount, error_count);
    }
}

int
main (int argc,
      char **argv)
{
  int c;
  int use_XDR = 1;
  CORBA_TypeCode_ptr tc = _tc_CORBA_TypeCode;
  void *data = tc;

  while ((c = ACE_OS::getopt (argc, argv, "cx")) != EOF) 
    {
      switch (c) 
	{
	case 'c':
	  use_XDR = 0;
	  continue;

	case 'x':
	  use_XDR = 1;
	  continue;

	case '?':
	default:
	  // usage:
	  ACE_OS::fprintf (stderr, "usage:  %s"
			   , " [-cx]"
			   , "\n"
			   , argv [0]);
	}
    }

  do_test (1, tc, data);	// XDR-ish
  do_test (0, tc, data);	// CDR

  return 0;
}