Debian Codegraph @

c/benchmarks/message-encoding_list.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 const bool VERBOSE = false;
41 const bool ERRORS = true;
42
43 static void decode_message_buffer(pn_rwbytes_t data) {
44 pn_message_t *m = pn_message();
45 int err = pn_message_decode(m, data.start, data.size);
46 if (!err) {
47 /* Print the decoded message */
48 pn_string_t *s = pn_string(NULL);
49 pn_inspect(pn_message_body(m), s);
50 if (VERBOSE) {
51 printf("%s\n", pn_string_get(s));
52 fflush(stdout);
53 }
54 pn_free(s);
55 pn_message_free(m);
56 free(data.start);
57 } else {
58 fprintf(stderr, "decode error: %s\n", pn_error_text(pn_message_error(m)));
59 exit(EXIT_FAILURE);
60 }
61 }
62
63 static void BM_EncodeListMessage(benchmark::State &state) {
64 if (VERBOSE)
65 printf("BEGIN BM_EncodeListMessage\n");
66
67 uint64_t entries = 0;
68
69 pn_message_t *message = pn_message();
70 for (auto _ : state) {
71 pn_message_clear(message);
72 pn_data_t *body;
73 body = pn_message_body(message);
74 pn_data_put_list(body);
75 pn_data_enter(body);
76 for (size_t i = 0; i < state.range(0); i += 2) {
77 pn_data_put_string(
78 body, pn_bytes(sizeof("some list value") - 1, "some list value"));
79 pn_data_put_int(body, 42);
80 entries += 2;
81 }
82 pn_data_exit(body);
83 pn_rwbytes_t buf{};
84 if (pn_message_encode2(message, &buf) == 0) {
85 state.SkipWithError(pn_error_text(pn_message_error(message)));
86 }
87 if (buf.start)
88 free(buf.start);
89 }
90 pn_message_free(message);
91
92 state.SetLabel("entries");
93 state.SetItemsProcessed(entries);
94
95 if (VERBOSE)
96 printf("END BM_EncodeListMessage\n");
97 }
98
99 BENCHMARK(BM_EncodeListMessage)
100 ->Arg(10)
101 ->Arg(100)
102 ->Arg(1000)
103 ->Arg(10000)
104 ->ArgName("entries")
105 ->Unit(benchmark::kMillisecond);
106
107 static void BM_CreateListMessage(benchmark::State &state) {
108 if (VERBOSE)
109 printf("BEGIN BM_CreateListMessage\n");
110
111 uint64_t entries = 0;
112
113 pn_message_t *message = pn_message();
114 pn_data_t *body;
115 body = pn_message_body(message);
116 pn_data_put_list(body);
117 pn_data_enter(body);
118 for (auto _ : state) {
119 pn_data_put_string(
120 body, pn_bytes(sizeof("some list value") - 1, "some list value"));
121 pn_data_put_int(body, 42);
122 entries += 2;
123 }
124 pn_data_exit(body);
125 pn_message_free(message);
126
127 state.SetLabel("entries");
128 state.SetItemsProcessed(entries);
129
130 if (VERBOSE)
131 printf("END BM_CreateListMessage\n");
132 }
133
134 BENCHMARK(BM_CreateListMessage);