File: psend.c

package info (click to toggle)
qpid-proton 0.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,632 kB
  • ctags: 20,083
  • sloc: java: 39,624; ansic: 29,389; python: 16,581; cpp: 11,250; ruby: 6,618; perl: 2,641; php: 1,033; xml: 957; sh: 230; pascal: 52; makefile: 32
file content (373 lines) | stat: -rw-r--r-- 10,079 bytes parent folder | download | duplicates (2)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */


/*################################################################
  This program is half of a pair.  Precv and Psend are meant to 
  be simple-as-possible examples of how to use the proton-c
  engine interface to send and receive messages over a single 
  connection and a single session.

  In addition to being examples, these programs or their 
  descendants will be used in performance regression testing
  for both throughput and latency, and long-term soak testing.

  This program, psend, is highly similar to its peer precv.
  I put all the good comments in precv.
*################################################################*/


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>


#include <proton/connection.h>
#include <proton/delivery.h>
#include <proton/driver.h>
#include <proton/event.h>
#include <proton/terminus.h>
#include <proton/link.h>
#include <proton/message.h>
#include <proton/session.h>


#define MY_BUF_SIZE 1000



void
print_timestamp ( FILE * fp, char const * label )
{
  struct timeval tv;
  struct tm      * timeinfo;

  gettimeofday ( & tv, 0 );
  timeinfo = localtime ( & tv.tv_sec );

  int seconds_today = 3600 * timeinfo->tm_hour +
                        60 * timeinfo->tm_min  +
                             timeinfo->tm_sec;

  fprintf ( fp, "time : %d.%.6ld : %s\n", seconds_today, tv.tv_usec, label );
}





static 
double
get_time ( )
{
  struct timeval tv;
  struct tm      * timeinfo;

  gettimeofday ( & tv, 0 );
  timeinfo = localtime ( & tv.tv_sec );

  double time_now = 3600 * timeinfo->tm_hour +
                      60 * timeinfo->tm_min  +
                           timeinfo->tm_sec;

  time_now += ((double)(tv.tv_usec) / 1000000.0);
  return time_now;
}





int 
main ( int argc, char ** argv )
{
  char addr [ 1000 ];
  char host [ 1000 ];
  char port [ 1000 ];
  char output_file_name[1000];


  uint64_t messages       = 2000000,
           delivery_count = 0;

  int message_length = 100;

  bool done           = false;
  int  sent_count     = 0;
  int  n_links        = 5;
  int const max_links = 100;

  strcpy ( addr, "queue" );
  strcpy ( host, "0.0.0.0" );
  strcpy ( port, "5672" );

  FILE * output_fp;


  for ( int i = 1; i < argc; ++ i )
  {
    if ( ! strcmp ( "--host", argv[i] ) )
    {
      strcpy ( host, argv[i+1] );
      ++ i;
    }
    else
    if ( ! strcmp ( "--port", argv[i] ) )
    {
      strcpy ( port, argv[i+1] );
      ++ i;
    }
    else
    if ( ! strcmp ( "--messages", argv[i] ) )
    {
      sscanf ( argv [ i+1 ], "%" SCNd64 , & messages );
      ++ i;
    }
    else
    if ( ! strcmp ( "--message_length", argv[i] ) )
    {
      sscanf ( argv [ i+1 ], "%d", & message_length );
      ++ i;
    }
    else
    if ( ! strcmp ( "--n_links", argv[i] ) )
    {
      sscanf ( argv [ i+1 ], "%d", & n_links );
      ++ i;
    }
    if ( ! strcmp ( "--output", argv[i] ) )
    {
      if ( ! strcmp ( "stderr", argv[i+1] ) )
      {
        output_fp = stderr;
        strcpy ( output_file_name, "stderr");
      }
      else
      if ( ! strcmp ( "stdout", argv[i+1] ) )
      {
        output_fp = stdout;
        strcpy ( output_file_name, "stdout");
      }
      else
      {
        output_fp = fopen ( argv[i+1], "w" );
        strcpy ( output_file_name, argv[i+1] );
        if ( ! output_fp )
        {
          fprintf ( stderr, "Can't open |%s| for writing.\n", argv[i+1] );
          exit ( 1 );
        }
      }
      ++ i;
    }
    else
    {
      fprintf ( output_fp, "unknown arg %s", argv[i] );
    }
  }


  fprintf ( output_fp, "host            %s\n",          host );
  fprintf ( output_fp, "port            %s\n",          port );
  fprintf ( output_fp, "messages        %" PRId64 "\n", messages );
  fprintf ( output_fp, "message_length  %d\n",          message_length );
  fprintf ( output_fp, "n_links         %d\n",          n_links );
  fprintf ( output_fp, "output          %s\n",          output_file_name );


  if ( n_links > max_links )
  {
    fprintf ( output_fp, "You can't have more than %d links.\n", max_links );
    exit ( 1 );
  }

  pn_driver_t     * driver;
  pn_connector_t  * connector;
  pn_connector_t  * driver_connector;
  pn_connection_t * connection;
  pn_collector_t  * collector;
  pn_link_t       * links [ max_links ];
  pn_session_t    * session;
  pn_event_t      * event;
  pn_delivery_t   * delivery;


  char * message = (char *) malloc(message_length);
  memset ( message, 13, message_length );

  /*----------------------------------------------------
    Get everything set up.
    We will have a single connector, a single 
    connection, a single session, and a single link.
  ----------------------------------------------------*/
  driver = pn_driver ( );
  connector = pn_connector ( driver, host, port, 0 );

  connection = pn_connection();
  collector  = pn_collector  ( );
  pn_connection_collect ( connection, collector );
  pn_connector_set_connection ( connector, connection );

  session = pn_session ( connection );
  pn_connection_open ( connection );
  pn_session_open ( session );

  for ( int i = 0; i < n_links; ++ i )
  {
    char name[100];
    sprintf ( name, "tvc_15_%d", i );
    links[i] = pn_sender ( session, name );
    pn_terminus_set_address ( pn_link_target(links[i]), addr );
    pn_link_open ( links[i] );
  }

  /*-----------------------------------------------------------
    For my speed tests, I do not want to count setup time.
    Start timing here.  The receiver will print out a similar
    timestamp when he receives the final message.
  -----------------------------------------------------------*/
  fprintf ( output_fp, "psend: sending %llu messages.\n", messages );

  // Just before we start sending, print the start timestamp.
  fprintf ( output_fp, "psend_start %.3lf\n", get_time() );

  while ( 1 )
  {
    pn_driver_wait ( driver, -1 );

    int event_count = 1;
    while ( event_count > 0 )
    {
      event_count = 0;
      pn_connector_process ( connector );

      event = pn_collector_peek(collector);
      while ( event )
      {
        ++ event_count;
        pn_event_type_t event_type = pn_event_type ( event );
        //fprintf ( output_fp, "event: %s\n", pn_event_type_name ( event_type ) );

        switch ( event_type )
        {
          case PN_LINK_FLOW:
          {
            if ( delivery_count < messages )
            {
              /*---------------------------------------------------
                We may have opened multiple links.
                The event will tell us which one this flow-event
                happened on.  If the flow event gave us some 
                credit, we will greedily send messages until it
                is all used up.
              ---------------------------------------------------*/
              pn_link_t * link = pn_event_link ( event );
              int credit = pn_link_credit ( link );

              while ( credit > 0 )
              {
                // Every delivery we create needs a unique tag.
                char str [ 100 ];
                sprintf ( str, "%x", delivery_count ++ );
                delivery = pn_delivery ( link, pn_dtag(str, strlen(str)) );

                // If you settle the delivery before sending it,
                // you will spend some time wondering why your 
                // messages don't have any content when they arrive 
                // at the receiver.
                pn_link_send ( link, message, message_length );
                pn_delivery_settle ( delivery );
                pn_link_advance ( link );
                credit = pn_link_credit ( link );
              }
              
              if ( delivery_count >= messages )
              {
                fprintf ( output_fp, 
                          "I have sent all %d messages.\n" ,
                          delivery_count
                        );
                /*
                  I'm still kind of hazy on how to shut down the
                  psend / precv system properly ....
                  I can't go to all_done here, or precv will never
                  get all its messages and terminate.
                  So I let precv terminate properly ... which means
                  that this program, psend, dies with an error.
                  Hmm.
                */
                // goto all_done;
              }
            }
          }
          break;


          case PN_TRANSPORT:
            // I don't know what this means here, either.
          break;


          case PN_TRANSPORT_TAIL_CLOSED:
            goto all_done;
          break;


          default:
            /*
            fprintf ( output_fp,
                      "precv unhandled event: %s\n",
                      pn_event_type_name(event_type)
                    );
            */
          break;

        }

        pn_collector_pop ( collector );
        event = pn_collector_peek(collector);
      }
    }
  }

  all_done:

  for ( int i = 0; i < n_links; ++ i )
  {
    pn_link_close ( links[i] );
  }

  pn_session_close ( session );
  pn_connection_close ( connection );

  return 0;
}