Add initial codes
[it/test.git] / simulators / workload_generator / src / ric_receiver.c
1 /*
2 ======================================================================
3         Copyright (c) 2019 Nokia
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
15    implied. See the License for the specific language governing 
16    permissions andlimitations under the License.
17 ======================================================================
18 */
19
20 #include <ctype.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <stdbool.h>
28
29 #define UTA_COMPAT
30 #include <rmr/rmr.h>
31 #include "ric_wg.h"
32
33 //array to save ts for each transaction
34 time_t ts_array[MAX_TRANS];
35 //array to save tns for each transaction
36 long int tns_array[MAX_TRANS];
37 //array to save latency for each transaction
38 double latency_array[MAX_TRANS]={0};
39 //array to save time passed for each transaction
40 double elapse_array[MAX_TRANS]={0};
41 //array to save typeid for each transaction
42 int type_array[MAX_TRANS]={0};
43
44 //they are used to track time in order to get msg rate
45 time_t ts_start_rcv;
46 long int tns_start_rcv;
47
48 int main( int argc, char** argv ) {
49   
50   void* mr;
51   char* proto_port = "tcp:4560";
52   uta_mbuf_t* msg = NULL;
53   msg_t* pm;
54   struct timespec ts;
55   long long delta;
56   long long elapse;
57   double ms_delta;
58   double ms_elapse;
59   double total_ms_delta = 0.0;
60   double ave_latency = 0.0;
61   double rcv_rate = 0.0;
62   long long rcv_count = 0;
63   
64   if((mr = uta_init( proto_port,
65                      UTA_MAX_RCV_BYTES, UTAFL_NONE )) == NULL) {
66     fprintf(stderr,
67             "abort: unable to initialize the message lib: %s\n",
68             strerror(errno));
69     exit(1);
70   }
71   
72   while(rcv_count <= MAX_RCV_COUNT) {
73
74     msg = uta_rcv_msg(mr, msg);
75    
76     if(msg) {
77       type_array[rcv_count] = msg->mtype;
78       pm = (msg_t *) msg->payload;
79       clock_gettime(CLOCK_REALTIME, &ts);
80       //record the time when the first msg is recieved 
81       if(rcv_count == 0) {
82         ts_start_rcv = ts.tv_sec;
83         tns_start_rcv = ts.tv_nsec;
84       }
85         
86       delta = (ts.tv_sec - (pm->ts).tv_sec) * 1000000000 +
87         ts.tv_nsec - (pm->ts).tv_nsec;
88           
89       ms_delta = (double) delta / 1000000;
90       elapse = (ts.tv_sec - ts_start_rcv) * 1000000000
91         + ts.tv_nsec - tns_start_rcv;
92       ms_elapse = (double) elapse / 1000000;
93       elapse_array[rcv_count] = ms_elapse;
94       total_ms_delta += ms_delta;
95       latency_array[rcv_count] = ms_delta;
96       rcv_count++;
97     }
98    
99   }
100   //when test ends dump memory into file
101   total_ms_delta -= latency_array[rcv_count - 1];
102   ms_elapse = elapse_array[rcv_count - 2];
103   rcv_count--;
104   ave_latency = total_ms_delta/rcv_count;
105   rcv_rate = (rcv_count/ms_elapse)*1000.0;
106   char fname[256];
107   snprintf(fname, sizeof fname, "report/xAPP_counters.csv");
108   FILE *fptr = fopen(fname, "w");
109   fprintf(fptr, "average latency %.3fmillisec\n", ave_latency);
110   for(int i = 0; i < rcv_count; i++)
111     fprintf(fptr, "%d %.3fmillisec \n", i, latency_array[i]);
112   return 0;
113 }
114