fipa_acl  1.4
benchmark.cpp
Go to the documentation of this file.
1 #include <fipa_acl/fipa_acl.h>
2 #include <string.h>
3 #include <algorithm>
4 #include <math.h>
5 #include <sys/time.h>
6 #include <numeric/Stats.hpp>
7 
8 /* Subtract the `struct timeval' values X and Y,
9  storing the result in RESULT.
10  Return 1 if the difference is negative, otherwise 0. */
11 // from http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_19.html#SEC313
12 int timeval_subtract (struct timeval* result, struct timeval* x,struct timeval* y)
13 {
14  /* Perform the carry for the later subtraction by updating y. */
15  if (x->tv_usec < y->tv_usec)
16  {
17  int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
18  y->tv_usec -= 1000000 * nsec;
19  y->tv_sec += nsec;
20  }
21 
22  if (x->tv_usec - y->tv_usec > 1000000)
23  {
24  int nsec = (y->tv_usec - x->tv_usec) / 1000000;
25  y->tv_usec += 1000000 * nsec;
26  y->tv_sec -= nsec;
27  }
28 
29  /* Compute the time remaining to wait.
30  tv_usec is certainly positive. */
31  result->tv_sec = x->tv_sec - y->tv_sec;
32  result->tv_usec = x->tv_usec - y->tv_usec;
33 
34  /* Return 1 if result is negative. */
35  return x->tv_sec < y->tv_sec;
36 }
37 
38 
39 int main(int argc, char** argv)
40 {
41  if(argc < 3)
42  {
43  printf("usage: %s <content-size-in-byte> <epochs>\n", argv[0]);
44  printf("output will be: <encoding> <content-size in byte> <encoded-msg-size in bytes > <overhead-percent> <encoding-time in ms/msg> <decoding-time in ms/msg> <epochs>\n");
45  exit(0);
46  }
47 
48  // 1 MB ~ 10 ms > 200 runs
49  // 0 MB ~ 0 ms > 200000
50  uint32_t BUFFER_MAX = atoi(argv[1]);
51  int32_t epochs = atoi(argv[2]);
52 
53  using namespace fipa::acl;
54  ACLMessage msg("inform");
55  AgentID origin("proxy");
56  AgentID receiver("crex_0");
57 
58  msg.setSender(origin);
59  msg.addReceiver(receiver);
60  msg.addReplyTo(origin);
62  msg.setProtocol(std::string("RIMRES"));
63  msg.setLanguage(std::string("test language"));
64  msg.setEncoding(std::string("test encoding"));
65  msg.setOntology(std::string("test ontology"));
66  msg.setReplyWith(std::string("test reply_with"));
67  base::Time time = base::Time::fromString("20101223-12:00:37:980", base::Time::Milliseconds);
68  msg.setReplyBy(time);
69  msg.setConversationID(std::string("test conversationID"));
70 
71  // leaving out performative but assuming 1 bytes for encoding (23
72  // performatives)
73  // leaving out time but assuming 8 bytes for encoding, s. below
74  std::string info;
75  info += origin.getName();
76  info += receiver.getName();
77  info += origin.getName();
78  info += msg.getLanguage();
79  info += msg.getEncoding();
80  info += msg.getOntology();
81  info += msg.getReplyWith();
82  // leaving out time but assuming 8 bytes for encoding, s. below
83  info += msg.getConversationID();
84 
85 
86 
87  char* buffer = (char*) calloc(1,BUFFER_MAX+1);
88  if(!buffer)
89  {
90  fprintf(stderr, "Allocation of memory for test message failed\n");
91  exit(0);
92  }
93  srand(::time(NULL));
94  for(size_t i = 0; i < BUFFER_MAX; ++i)
95  {
96  buffer[i] = (char) 48 + rand() % 40;
97  }
98 
99  // memset(buffer, 1, BUFFER_MAX);
100  buffer[BUFFER_MAX] = '\0';
101  std::string content(buffer, BUFFER_MAX);
102  msg.setContent(content);
103 
104  uint32_t payloadsize = info.size() + BUFFER_MAX + 8 + 1; // s. comment above for performative and replyBy
105  printf("# PAYLOAD SIZE: %d\n", payloadsize);
106  printf("# CONTENT SIZE: %lu\n", content.size());
107  printf("# MSG CONTENT SIZE: %lu\n", msg.getContent().size());
108 
109 
110  MessageParser inputParser;
111 
112  std::string encodedMsg;
113  printf("#<encoding> <content-size in byte> <encoded-msg-size in bytes > <overhead-percent> <encoding-time in ms/msg> <decoding-time in ms/msg> <epochs>\n");
114  for(int i = static_cast<int>(representation::BITEFFICIENT); i < static_cast<int>(representation::END_MARKER); ++i)
115  {
116  numeric::Stats<double> msgEncodingStats;
117  numeric::Stats<double> msgDecodingStats;
118 
119  numeric::Stats<double> envEncodingStats;
120  numeric::Stats<double> envDecodingStats;
121 
122  representation::Type representationType = static_cast<representation::Type>(i);
123  {
124  struct timeval start, stop, diff;
125  for(int i = 0; i < epochs; ++i)
126  {
127  gettimeofday(&start, 0);
128  encodedMsg = MessageGenerator::create(msg, representationType);
129  gettimeofday(&stop, 0);
130  timeval_subtract(&diff, &stop, &start);
131  double totaltime = diff.tv_sec*1000 + diff.tv_usec/1000.0;
132  msgEncodingStats.update(totaltime);
133  }
134  }
135  {
136  ACLMessage msg;
137  struct timeval start, stop, diff;
138  for(int i = 0; i < epochs; ++i)
139  {
140  gettimeofday(&start, 0);
141  if(!inputParser.parseData(encodedMsg, msg, representationType))
142  {
143  printf("Could not parse: using %s\n", representation::TypeTxt[representationType].c_str());
144  }
145  gettimeofday(&stop, 0);
146  timeval_subtract(&diff, &stop, &start);
147  double totaltime = diff.tv_sec*1000 + diff.tv_usec/1000.0;
148  msgDecodingStats.update(totaltime);
149  }
150  }
151 
152  double overheadInPercent = (encodedMsg.size() - payloadsize)*1.0/(1.0*encodedMsg.size());
153 
154  printf("message %d %d %10.6f %10.6f %10.6f %10.6f %10.6f %d %s\n", BUFFER_MAX, (int) encodedMsg.size(), overheadInPercent, msgEncodingStats.mean(), msgEncodingStats.stdev(), msgDecodingStats.mean(), msgDecodingStats.stdev(), epochs, representation::TypeTxt[representationType].c_str());
155 
156  // envelope
157  //printf("#<encoding> <content-size in byte> <encoded-env-size in bytes > <overhead-percent> <encoding-time in ms/msg> <decoding-time in ms/msg> <epochs>\n");
158  std::string encodedEnvelope;
159  for(int e = static_cast<int>(representation::BITEFFICIENT); e < static_cast<int>(representation::END_MARKER); ++e)
160  {
161  if( e == static_cast<int>(representation::STRING_REP))
162  {
163  continue;
164  }
165 
166  representation::Type envRepresentationType = static_cast<representation::Type>(e);
167 
168  for(int i = 0; i < epochs; ++i)
169  {
170  {
171  ACLEnvelope env(msg, representationType);
172  struct timeval start, stop, diff;
173  gettimeofday(&start, 0);
174  encodedEnvelope = EnvelopeGenerator::create(env, envRepresentationType);
175 
176  gettimeofday(&stop, 0);
177  timeval_subtract(&diff, &stop, &start);
178 
179  double totaltime = diff.tv_sec*1000 + diff.tv_usec/1000.0;
180  envEncodingStats.update(totaltime);
181  }
182 
183  ACLEnvelope decodedEnvelope;
184  {
185  struct timeval start, stop, diff;
186  gettimeofday(&start, 0);
187  if(!EnvelopeParser::parseData(encodedEnvelope, decodedEnvelope, envRepresentationType))
188  {
189  printf("Decoding envelope failed using: %s\n", representation::TypeTxt[envRepresentationType].c_str());
190  break;
191  }
192  gettimeofday(&stop, 0);
193  timeval_subtract(&diff, &stop, &start);
194 
195  double totaltime = diff.tv_sec*1000 + diff.tv_usec/1000.0;
196  envDecodingStats.update(totaltime);
197  }
198  }
199 
200  overheadInPercent = (encodedEnvelope.size() - encodedMsg.size())/(1.0*encodedEnvelope.size());
201  printf("envelope %d %d %10.6f %10.6f %10.6f %10.6f %10.6f %d %s %s\n", BUFFER_MAX, (int) encodedEnvelope.size(), overheadInPercent, envEncodingStats.mean(), envEncodingStats.stdev(), envDecodingStats.mean(), envDecodingStats.stdev(), epochs, representation::TypeTxt[envRepresentationType].c_str(), representation::TypeTxt[representationType].c_str());
202 
203  }
204  printf(" ---\n");
205  }
206 
207  delete buffer;
208  return 0;
209 }
std::string getEncoding() const
Definition: acl_message.h:258
static std::string create(const ACLEnvelope &msg, const representation::Type &acl_representation)
void setReplyWith(const std::string &str)
Definition: acl_message.h:212
void setConversationID(const std::string &str)
Definition: acl_message.h:222
ACLMessage envelope (also typed as fipa::acl::Letter) includes the base envelope and updated fields...
Definition: acl_envelope.h:349
std::string getContent() const
Definition: acl_message.h:294
void setLanguage(const std::string &str)
Definition: acl_message.h:263
static std::string create(const ACLMessage &msg, const representation::Type &acl_representation)
static bool parseData(const std::string &storage, ACLEnvelope &envelope, representation::Type type=fipa::acl::representation::BITEFFICIENT)
const std::string & getName() const
setter and getter methods for all fields; they do not result in deep-copies assignments/retreivals, but this can be easily changed if needed through the overloaded operator which do
Definition: agent_id.h:90
Allows to parse a bytestream that conforms to the bitefficient representation of FIPA messages to a A...
std::string getReplyWith() const
Definition: acl_message.h:217
std::string getOntology() const
Definition: acl_message.h:248
void addReplyTo(const AgentID &aid)
void setContent(const std::string &content)
Definition: acl_message.h:274
void setSender(const AgentID &sender)
Definition: acl_message.h:305
const std::string TypeTxt[]
Definition: types.h:28
std::string getLanguage() const
Definition: acl_message.h:268
This class provides a representation of a message conforming to the FIPA specification SC00061...
Definition: acl_message.h:55
void setReplyBy(const base::Time &time)
Definition: acl_message.h:340
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
Definition: benchmark.cpp:12
void addReceiver(const AgentID &aid)
void info(char **argv)
Definition: main.cpp:18
void setPerformative(Performative perf)
Set performative.
Implements the general AgentID functionality, which is present throughout the fipa specifications(FIP...
Definition: agent_id.h:37
void setEncoding(const std::string &str)
Definition: acl_message.h:253
int main(int argc, char **argv)
Definition: benchmark.cpp:39
void setProtocol(const std::string &str)
the method checks whether the passed protocol string is a word or not(according to the fipa spec) ...
std::string getConversationID() const
Definition: acl_message.h:227
void setOntology(const std::string &str)
Definition: acl_message.h:243