xref: /aosp_15_r20/external/pigweed/pw_rpc/benchmark.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2021 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker 
15*61c4878aSAndroid Build Coastguard Worker #include "pw_rpc/benchmark.h"
16*61c4878aSAndroid Build Coastguard Worker 
17*61c4878aSAndroid Build Coastguard Worker #include <algorithm>
18*61c4878aSAndroid Build Coastguard Worker 
19*61c4878aSAndroid Build Coastguard Worker #include "pw_rpc/internal/config.h"
20*61c4878aSAndroid Build Coastguard Worker 
21*61c4878aSAndroid Build Coastguard Worker namespace pw::rpc {
22*61c4878aSAndroid Build Coastguard Worker namespace {
23*61c4878aSAndroid Build Coastguard Worker 
CopyBuffer(ConstByteSpan input,ByteSpan output)24*61c4878aSAndroid Build Coastguard Worker StatusWithSize CopyBuffer(ConstByteSpan input, ByteSpan output) {
25*61c4878aSAndroid Build Coastguard Worker   if (input.size() > output.size()) {
26*61c4878aSAndroid Build Coastguard Worker     return pw::StatusWithSize::ResourceExhausted();
27*61c4878aSAndroid Build Coastguard Worker   }
28*61c4878aSAndroid Build Coastguard Worker   std::copy(input.begin(), input.end(), output.begin());
29*61c4878aSAndroid Build Coastguard Worker   return pw::StatusWithSize(input.size());
30*61c4878aSAndroid Build Coastguard Worker }
31*61c4878aSAndroid Build Coastguard Worker 
32*61c4878aSAndroid Build Coastguard Worker }  // namespace
33*61c4878aSAndroid Build Coastguard Worker 
UnaryEcho(ConstByteSpan request,RawUnaryResponder & responder)34*61c4878aSAndroid Build Coastguard Worker void BenchmarkService::UnaryEcho(ConstByteSpan request,
35*61c4878aSAndroid Build Coastguard Worker                                  RawUnaryResponder& responder) {
36*61c4878aSAndroid Build Coastguard Worker   std::byte response[32];
37*61c4878aSAndroid Build Coastguard Worker   StatusWithSize result = CopyBuffer(request, response);
38*61c4878aSAndroid Build Coastguard Worker   responder.Finish(span(response).first(result.size()), result.status())
39*61c4878aSAndroid Build Coastguard Worker       .IgnoreError();
40*61c4878aSAndroid Build Coastguard Worker }
41*61c4878aSAndroid Build Coastguard Worker 
AllocateReaderWriterId()42*61c4878aSAndroid Build Coastguard Worker BenchmarkService::ReaderWriterId BenchmarkService::AllocateReaderWriterId() {
43*61c4878aSAndroid Build Coastguard Worker   return next_reader_writer_id_++;
44*61c4878aSAndroid Build Coastguard Worker }
45*61c4878aSAndroid Build Coastguard Worker 
BidirectionalEcho(RawServerReaderWriter & new_reader_writer)46*61c4878aSAndroid Build Coastguard Worker void BenchmarkService::BidirectionalEcho(
47*61c4878aSAndroid Build Coastguard Worker     RawServerReaderWriter& new_reader_writer) {
48*61c4878aSAndroid Build Coastguard Worker   auto id = AllocateReaderWriterId();
49*61c4878aSAndroid Build Coastguard Worker 
50*61c4878aSAndroid Build Coastguard Worker   struct Captures {
51*61c4878aSAndroid Build Coastguard Worker     BenchmarkService* self;
52*61c4878aSAndroid Build Coastguard Worker     ReaderWriterId id;
53*61c4878aSAndroid Build Coastguard Worker   };
54*61c4878aSAndroid Build Coastguard Worker 
55*61c4878aSAndroid Build Coastguard Worker   auto captures = std::make_unique<Captures>(Captures{.self = this, .id = id});
56*61c4878aSAndroid Build Coastguard Worker   new_reader_writer.set_on_next(
57*61c4878aSAndroid Build Coastguard Worker       [context = std::move(captures)](ConstByteSpan request) {
58*61c4878aSAndroid Build Coastguard Worker         auto& reader_writers = context->self->reader_writers_;
59*61c4878aSAndroid Build Coastguard Worker         auto rw_id = context->id;
60*61c4878aSAndroid Build Coastguard Worker         auto reader_writer = reader_writers.find(rw_id);
61*61c4878aSAndroid Build Coastguard Worker         if (reader_writer == reader_writers.end()) {
62*61c4878aSAndroid Build Coastguard Worker           return;
63*61c4878aSAndroid Build Coastguard Worker         }
64*61c4878aSAndroid Build Coastguard Worker         Status status = reader_writer->second.Write(request);
65*61c4878aSAndroid Build Coastguard Worker         if (!status.ok()) {
66*61c4878aSAndroid Build Coastguard Worker           reader_writer->second.Finish(status)
67*61c4878aSAndroid Build Coastguard Worker               .IgnoreError();  // TODO: b/242598609 - Handle Status properly
68*61c4878aSAndroid Build Coastguard Worker           reader_writers.erase(rw_id);
69*61c4878aSAndroid Build Coastguard Worker         }
70*61c4878aSAndroid Build Coastguard Worker       });
71*61c4878aSAndroid Build Coastguard Worker   reader_writers_.insert({id, std::move(new_reader_writer)});
72*61c4878aSAndroid Build Coastguard Worker }
73*61c4878aSAndroid Build Coastguard Worker 
74*61c4878aSAndroid Build Coastguard Worker }  // namespace pw::rpc
75