Debian Codegraph @

c/benchmarks/message-encoding_map.cpp

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21 #include <unistd.h>
22
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "proton/connection_driver.h"
29 #include "proton/engine.h"
30 #include "proton/log.h"
31 #include "proton/message.h"
32
33 #include <benchmark/benchmark.h>
34 #include <proton/listener.h>
35 #include <proton/netaddr.h>
36 #include <proton/proactor.h>
37 #include <proton/sasl.h>
38 #include <wait.h>
39
40 #define MAX_SIZE 1024
41
42 typedef char str[MAX_SIZE];
43
44 const bool VERBOSE = false;
45 const bool ERRORS = true;
46
47 static void decode_message_buffer(pn_rwbytes_t data) {
48 pn_message_t *m = pn_message();
49 int err = pn_message_decode(m, data.start, data.size);
50 if (!err) {
51 /* Print the decoded message */
52 pn_string_t *s = pn_string(NULL);
53 pn_inspect(pn_message_body(m), s);
54 if (VERBOSE) {
55 printf("%s\n", pn_string_get(s));
56 fflush(stdout);
57 }
58 pn_free(s);
59 pn_message_free(m);
60 free(data.start);
61 } else {
62 fprintf(stderr, "decode error: %s\n", pn_error_text(pn_message_error(m)));
63 exit(EXIT_FAILURE);
64 }
65 }
66
67 static void BM_EncodeMapMessage(benchmark::State &state) {
68 if (VERBOSE)
69 printf("BEGIN BM_EncodeMapMessage\n");
70
71 uint64_t entries = 0;
72
73 pn_message_t *message = pn_message();
74
75 for (auto _ : state) {
76 pn_message_clear(message);
77 pn_data_t *body;
78 body = pn_message_body(message);
79 pn_data_put_map(body);
80 pn_data_enter(body);
81 for (size_t i = 0; i < state.range(0); ++i) {
82 pn_data_put_int(body, i);
83 pn_data_put_string(
84 body, pn_bytes(sizeof("some key value") - 1, "some key value"));
85 entries += 2;
86 }
87 pn_data_exit(body);
88 pn_rwbytes_t buf{};
89 if (pn_message_encode2(message, &buf) == 0) {
90 state.SkipWithError(pn_error_text(pn_message_error(message)));
91 }
92 if (buf.start)
93 free(buf.start);
94 }
95 pn_message_free(message);
96
97 state.SetLabel("entries");
98 state.SetItemsProcessed(entries);
99
100 if (VERBOSE)
101 printf("END BM_EncodeMapMessage\n");
102 }
103
104 BENCHMARK(BM_EncodeMapMessage)
105 ->Arg(10)
106 ->Arg(100)
107 ->Arg(1000)
108 ->Arg(10000)
109 ->ArgName("entries")
110 ->Unit(benchmark::kMillisecond);
111
112 static void BM_CreateMapMessage(benchmark::State &state) {
113 if (VERBOSE)
114 printf("BEGIN BM_CreateMapMessage\n");
115
116 uint64_t entries = 0;
117
118 pn_message_t *message = pn_message();
119
120 pn_message_clear(message);
121 pn_data_t *body;
122 body = pn_message_body(message);
123 pn_data_put_map(body);
124 pn_data_enter(body);
125 for (auto _ : state) {
126 pn_data_put_int(body, entries);
127 pn_data_put_string(
128 body, pn_bytes(sizeof("some key value") - 1, "some key value"));
129 entries += 2;
130 }
131 pn_data_exit(body);
132 pn_message_free(message);
133
134 state.SetLabel("entries");
135 state.SetItemsProcessed(entries);
136
137 if (VERBOSE)
138 printf("END BM_CreateMapMessage\n");
139 }
140
141 BENCHMARK(BM_CreateMapMessage);
142
143
144 static void decode_message(pn_delivery_t *dlv) {
145 static char buffer[MAX_SIZE];
146 ssize_t len;
147 // try to decode the message body
148 if (pn_delivery_pending(dlv) < MAX_SIZE) {
149 // read in the raw data
150 len = pn_link_recv(pn_delivery_link(dlv), buffer, MAX_SIZE);
151 if (len > 0) {
152 // decode it into a proton message
153 pn_message_t *m = pn_message();
154 if (PN_OK == pn_message_decode(m, buffer, len)) {
155 pn_string_t *s = pn_string(NULL);
156 pn_inspect(pn_message_body(m), s);
157 if (VERBOSE)
158 printf("%s\n", pn_string_get(s));
159 pn_free(s);
160 }
161 pn_message_free(m);
162 }
163 }
164 }