Apollo 11.0
自动驾驶开放平台
cyber_benchmark_writer.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include <getopt.h>
18#include <libgen.h>
19#include <string>
20#include <vector>
21#include <memory>
22
23#include "cyber/cyber.h"
24#include "cyber/benchmark/benchmark_msg.pb.h"
25
26#if __has_include("gperftools/profiler.h")
27#include "gperftools/profiler.h"
28#include "gperftools/heap-profiler.h"
29#include "gperftools/malloc_extension.h"
30#endif
31
32std::string BINARY_NAME = "cyber_benchmark_writer"; // NOLINT
33
34int message_size = -1;
36int qos_policy = 0;
37int data_type = 0;
38int running_time = 10;
39bool enable_cpuprofile = false;
40bool enable_heapprofile = false;
41std::string profile_filename = "cyber_benchmark_writer_cpu.prof"; // NOLINT
42std::string heapprofile_filename = "cyber_benchmark_writer_mem.prof"; // NOLINT
43
45 AINFO << "Usage: \n " << BINARY_NAME << " [OPTION]...\n"
46 << "Description: \n"
47 << " -h, --help: help information \n"
48 << " -s, --message_size=message_size: transport message size\n"
49 << " -t, --transport_freq=transmission_frequency: transmission frequency\n" // NOLINT
50 << " -q, --qos_policy=qos_reliable_policy: set qos reliable policy, "
51 "0 is Reliable, 1 is Best effort, default value is 0\n"
52 << " -d, --data_type=data_type: transport data type, "
53 "0 is bytes, 1 is repeated field, default value is 0\n"
54 << " -T, --time=time: running time, default value is 10 seconds\n"
55 << " -c, --cpuprofile: enable gperftools cpu profile\n"
56 << " -o, --profile_filename=filename: the filename to dump the "
57 "profile to, default value is cyber_benchmark_writer_cpu.prof. Only work " // NOLINT
58 "with -c option\n"
59 << " -H, --heapprofile: enable gperftools heap profile\n"
60 << " -O, --heapprofile_filename=filename: the filename to dump the "
61 "profile to, default value is cyber_benchmark_writer_mem.prof. Only work " // NOLINT
62 "with -H option\n"
63 << "Example:\n"
64 << " " << BINARY_NAME << " -h\n"
65 << " " << BINARY_NAME << " -s 64K -t 10\n"
66 << " " << BINARY_NAME << " -s 64K -t 10 -c -H ";
67}
68
69void GetOptions(const int argc, char* const argv[]) {
70 opterr = 0; // extern int opterr
71 int long_index = 0;
72 const std::string short_opts = "hs:t:q:d:T:co:HO:";
73 static const struct option long_opts[] = {
74 {"help", no_argument, nullptr, 'h'},
75 {"message_size", required_argument, nullptr, 's'},
76 {"transport_freq", required_argument, nullptr, 't'},
77 {"qos_policy", required_argument, nullptr, 'q'},
78 {"data_type", required_argument, nullptr, 'd'},
79 {"time", required_argument, nullptr, 'T'},
80 {"cpuprofile", no_argument, nullptr, 'c'},
81 {"profile_filename", required_argument, nullptr, 'o'},
82 {"heapprofile", no_argument, nullptr, 'H'},
83 {"heapprofile_filename", required_argument, nullptr, 'O'},
84 {NULL, no_argument, nullptr, 0}};
85
86 // log command for info
87 std::string cmd("");
88 for (int i = 0; i < argc; ++i) {
89 cmd += argv[i];
90 cmd += " ";
91 }
92 AINFO << "command: " << cmd;
93
94 if (1 == argc) {
96 exit(0);
97 }
98
99 do {
100 int opt =
101 getopt_long(argc, argv, short_opts.c_str(), long_opts, &long_index);
102 if (opt == -1) {
103 break;
104 }
105 int base_size = 1;
106 std::string arg;
107 switch (opt) {
108 case 's':
109 arg = std::string(optarg);
110 switch (arg[arg.length()-1]) {
111 case 'K':
112 base_size = 1024;
113 break;
114 case 'M':
115 base_size = 1024 * 1024;
116 break;
117 default:
118 AERROR << "Invalid identifier. It should be 'K' or 'M' or 'B'";
119 exit(-1);
120 }
121 message_size = std::stoi(arg.substr(0, arg.length()-1)) * base_size;
122 if (message_size < 0 || message_size % 4 != 0) {
123 AERROR << "Invalid message size.";
124 exit(-1);
125 }
126 break;
127 case 't':
128 transport_freq = std::stoi(std::string(optarg));
129 if (transport_freq < 0) {
130 AERROR << "Invalid transport frequency. It should greater than 0";
131 exit(-1);
132 }
133 break;
134 case 'T':
135 running_time = std::stoi(std::string(optarg));
136 if (running_time < 0) {
137 AERROR << "Invalid running time. It should greater than 0";
138 exit(-1);
139 }
140 break;
141 case 'q':
142 qos_policy = std::stoi(std::string(optarg));
143 if (qos_policy != 0 && qos_policy != 1) {
144 AERROR << "Invalid qos_policy. It should be 0 or 1";
145 exit(-1);
146 }
147 break;
148 case 'd':
149 data_type = std::stoi(std::string(optarg));
150 if (data_type != 0 && data_type != 1) {
151 AERROR << "Invalid data_type. It should be 0 or 1";
152 exit(-1);
153 }
154 break;
155 case 'c':
156#ifndef BASE_PROFILER_H_
157 AWARN << "gperftools not installed, ignore perf parameters";
158#endif
159 enable_cpuprofile = true;
160 break;
161 case 'o':
162 profile_filename = std::string(optarg);
163 break;
164 case 'H':
165#ifndef BASE_PROFILER_H_
166 AWARN << "gperftools not installed, ignore perf parameters";
167#endif
168 enable_heapprofile = true;
169 break;
170 case 'O':
171 heapprofile_filename = std::string(optarg);
172 break;
173 case 'h':
174 DisplayUsage();
175 exit(0);
176 default:
177 break;
178 }
179 } while (true);
180
181 if (optind < argc) {
182 AINFO << "Found non-option ARGV-element \"" << argv[optind++] << "\"";
183 DisplayUsage();
184 exit(1);
185 }
186
187 if (message_size == -1 || transport_freq == -1) {
188 AINFO << "-s and -t parameters must be specified";
189 DisplayUsage();
190 exit(1);
191 }
192}
193
194int main(int argc, char** argv) {
195 GetOptions(argc, argv);
196
198
200
202 attrs.set_channel_name("/apollo/cyber/benchmark");
203 auto qos = attrs.mutable_qos_profile();
204
205 if (qos_policy == 1) {
206 qos->set_reliability(
208 } else {
209 qos->set_reliability(
211 }
212 auto writer = node->CreateWriter<
214
215 // sleep a while for initialization, aboout 2 seconds
216 apollo::cyber::Rate rate_init(0.5);
217 apollo::cyber::Rate rate_ctl(static_cast<float>(transport_freq));
218 rate_init.Sleep();
219
220 uint64_t send_msg_total = transport_freq * running_time;
221
222 // std::vector<uint32_t> trans_vec;
223 // int num_of_instance = message_size / 4;
224 // for (int i = 0; i < num_of_instance; i++) {
225 // trans_vec.push_back(rand());
226 // }
227
228 // char* data = (char*)malloc(message_size);
229 // for (int i = 0; i < num_of_instance; i++) {
230 // *(uint32_t*)(data + i * 4) = rand();
231 // }
232
233 // if (data_type == 0) {
234 // trans_unit->set_data_bytes(data, message_size);
235 // } else {
236 // for (int i = 0; i < num_of_instance; i++) {
237 // trans_unit->add_data(trans_vec[i]);
238 // }
239 // }
240 // free(data);
241
242 int send_msg = 0;
243
244#ifndef NO_TCMALLOC
245#ifdef BASE_PROFILER_H_
246 if (enable_cpuprofile) {
247 ProfilerStart(profile_filename.c_str());
248 }
249 if (enable_heapprofile) {
250 HeapProfilerStart(heapprofile_filename.c_str());
251 }
252#endif
253#endif
254
255std::vector<uint32_t> trans_vec;
256int num_of_instance = message_size / 4;
257for (int i = 0; i < num_of_instance; i++) {
258 trans_vec.push_back(rand()); // NOLINT
259}
260
261char* data = (char*)malloc(message_size); // NOLINT
262
263 while (send_msg < send_msg_total) {
264 auto trans_unit = std::make_shared<
266 int base = rand(); // NOLINT
267
268 for (int i = 0; i < num_of_instance; i++) {
269 trans_vec[i] = base * i;
270 }
271
272 for (int i = 0; i < num_of_instance; i++) {
273 *(uint32_t*)(data + i * 4) = base * i; // NOLINT
274 }
275
276 if (data_type == 0) {
277 trans_unit->set_data_bytes(data, message_size);
278 } else {
279 for (int i = 0; i < num_of_instance; i++) {
280 trans_unit->add_data(trans_vec[i]);
281 }
282 }
283
284 writer->Write(trans_unit);
285 ++send_msg;
286 rate_ctl.Sleep();
287 }
288
289free(data);
290
291#ifndef NO_TCMALLOC
292#ifdef BASE_PROFILER_H_
293 if (enable_cpuprofile) {
294 ProfilerStop();
295 }
296 if (enable_heapprofile) {
297 HeapProfilerDump("Befor shutdown");
298 HeapProfilerStop();
299 }
300#endif
301#endif
302
304
305 return 0;
306}
bool enable_heapprofile
void DisplayUsage()
int running_time
int main(int argc, char **argv)
int transport_freq
void GetOptions(const int argc, char *const argv[])
std::string heapprofile_filename
std::string profile_filename
int message_size
std::string BINARY_NAME
bool enable_cpuprofile
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
void Clear()
Definition init.cc:154
bool Init(const char *binary_name, const std::string &dag_info)
Definition init.cc:99
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33