-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.cpp
216 lines (184 loc) · 8.08 KB
/
basic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdlib>
#include <fmt/format.h>
#include <tb_client.hpp>
inline auto get_time_ms() noexcept {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
fmt::print("TigerBeetle C++ Sample\n");
fmt::print("Connecting...\n");
auto address = []() -> std::string_view {
if (const char *env_address = std::getenv("TB_ADDRESS"); env_address) {
return env_address;
}
return "3001";
}();
tigerbeetle::Client client(address);
if (client.initStatus() != tigerbeetle::TB_INIT_SUCCESS) {
fmt::println("Failed to initialize tb_client");
return EXIT_FAILURE;
}
tigerbeetle::CompletionContext ctx{};
tigerbeetle::tb_packet_t packet{};
////////////////////////////////////////////////////////////
// Submitting a batch of accounts
////////////////////////////////////////////////////////////
static constexpr size_t ACCOUNTS_LEN = 2;
auto accounts = tigerbeetle::make_account<ACCOUNTS_LEN>();
// Fully initialize all fields in declaration order
accounts[0] = tigerbeetle::tb_account_t{.id = 1,
.debits_pending = 0,
.debits_posted = 0,
.credits_pending = 0,
.credits_posted = 0,
.user_data_128 = 0,
.user_data_64 = 0,
.user_data_32 = 0,
.reserved = 0,
.ledger = 777,
.code = 2,
.flags = 0,
.timestamp = 0};
accounts[1] = tigerbeetle::tb_account_t{.id = 2,
.debits_pending = 0,
.debits_posted = 0,
.credits_pending = 0,
.credits_posted = 0,
.user_data_128 = 0,
.user_data_64 = 0,
.user_data_32 = 0,
.reserved = 0,
.ledger = 777,
.code = 2,
.flags = 0,
.timestamp = 0};
packet.operation = tigerbeetle::TB_OPERATION_CREATE_ACCOUNTS;
packet.data = accounts.data();
packet.data_size = sizeof(tigerbeetle::tb_account_t) * ACCOUNTS_LEN;
packet.user_data = &ctx;
packet.status = tigerbeetle::TB_PACKET_OK;
fmt::print("Creating accounts...\n");
client.send_request(packet, &ctx);
if (packet.status != tigerbeetle::TB_PACKET_OK) {
fmt::print("Error calling create_accounts (ret={})\n", packet.status);
return EXIT_FAILURE;
}
if (ctx.size != 0) {
std::span<const tigerbeetle::tb_create_accounts_result_t> results(
reinterpret_cast<const tigerbeetle::tb_create_accounts_result_t *>(
ctx.reply.data()),
ctx.size / sizeof(tigerbeetle::tb_create_accounts_result_t));
fmt::print("create_account results:\n");
for (const auto &result : results) {
fmt::print("index={}, ret={}\n", result.index,
static_cast<int>(result.result));
}
return EXIT_FAILURE;
}
fmt::print("Accounts created successfully\n");
////////////////////////////////////////////////////////////
// Submitting multiple batches of transfers
////////////////////////////////////////////////////////////
fmt::print("Creating transfers...\n");
static constexpr auto MAX_BATCHES = 100;
static constexpr auto TRANSFERS_PER_BATCH =
tigerbeetle::MAX_MESSAGE_SIZE / sizeof(tigerbeetle::tb_transfer_t);
long max_latency_ms = 0;
long total_time_ms = 0;
for (int i = 0; i < MAX_BATCHES; ++i) {
auto transfers = tigerbeetle::make_transfer<TRANSFERS_PER_BATCH>();
// Initialize transfers using ranges, fixing lambda to modify in-place
size_t baseID = i * TRANSFERS_PER_BATCH + 1;
size_t j = 0;
std::ranges::for_each(transfers, [&](auto &transfer) {
transfer.id = baseID + j++;
transfer.debit_account_id = accounts[0].id;
transfer.credit_account_id = accounts[1].id;
transfer.amount = 1;
transfer.pending_id = 0;
transfer.user_data_128 = 0;
transfer.user_data_64 = 0;
transfer.user_data_32 = 0;
transfer.timeout = 0;
transfer.ledger = 777;
transfer.code = 2;
transfer.flags = 0;
transfer.timestamp = 0;
});
packet.operation = tigerbeetle::TB_OPERATION_CREATE_TRANSFERS;
packet.data = transfers.data();
packet.data_size = sizeof(tigerbeetle::tb_transfer_t) * TRANSFERS_PER_BATCH;
packet.user_data = &ctx;
packet.status = tigerbeetle::TB_PACKET_OK;
auto start = std::chrono::steady_clock::now();
client.send_request(packet, &ctx);
auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count();
if (elapsed_ms > max_latency_ms)
max_latency_ms = elapsed_ms;
total_time_ms += elapsed_ms;
if (packet.status != tigerbeetle::TB_PACKET_OK) {
fmt::print("Error calling create_transfers (ret={})\n", packet.status);
return EXIT_FAILURE;
}
if (ctx.size != 0) {
std::span<const tigerbeetle::tb_create_transfers_result_t> results(
reinterpret_cast<const tigerbeetle::tb_create_transfers_result_t *>(
ctx.reply.data()),
ctx.size / sizeof(tigerbeetle::tb_create_transfers_result_t));
fmt::print("create_transfers results:\n");
for (const auto &result : results) {
fmt::print("index={}, ret={}\n", result.index,
static_cast<int>(result.result));
}
return EXIT_FAILURE;
}
}
fmt::print("Transfers created successfully\n");
fmt::print("============================================\n");
fmt::print("{} transfers per second\n",
(MAX_BATCHES * TRANSFERS_PER_BATCH * 1000) / total_time_ms);
fmt::print("create_transfers max p100 latency per {} transfers = {}ms\n",
TRANSFERS_PER_BATCH, max_latency_ms);
fmt::print("total {} transfers in {}ms\n", MAX_BATCHES * TRANSFERS_PER_BATCH,
total_time_ms);
fmt::print("\n");
////////////////////////////////////////////////////////////
// Looking up accounts
////////////////////////////////////////////////////////////
fmt::print("Looking up accounts ...\n");
std::array<tigerbeetle::tb_uint128_t, ACCOUNTS_LEN> ids = {accounts[0].id,
accounts[1].id};
packet.operation = tigerbeetle::TB_OPERATION_LOOKUP_ACCOUNTS;
packet.data = ids.data();
packet.data_size = sizeof(tigerbeetle::tb_uint128_t) * ACCOUNTS_LEN;
packet.user_data = &ctx;
packet.status = tigerbeetle::TB_PACKET_OK;
client.send_request(packet, &ctx);
if (packet.status != tigerbeetle::TB_PACKET_OK) {
fmt::print("Error calling lookup_accounts (ret={})\n", packet.status);
return EXIT_FAILURE;
}
if (ctx.size == 0) {
fmt::print("No accounts found\n");
return EXIT_FAILURE;
} else {
std::span<const tigerbeetle::tb_account_t> results(
reinterpret_cast<const tigerbeetle::tb_account_t *>(ctx.reply.data()),
ctx.size / sizeof(tigerbeetle::tb_account_t));
fmt::print("{} Account(s) found\n", results.size());
fmt::print("============================================\n");
std::ranges::for_each(results, [](const auto &result) {
fmt::print("id={}\n", static_cast<std::size_t>(result.id));
fmt::print("debits_posted={}\n", result.debits_posted);
fmt::print("credits_posted={}\n", result.credits_posted);
});
}
return EXIT_SUCCESS;
}