Reintroduce lambdas

pull/13576/head
Vijay Pai 7 years ago
parent f230ffd476
commit c6587ca11b
  1. 60
      test/cpp/qps/client_sync.cc

@ -154,7 +154,12 @@ class SynchronousStreamingClient : public SynchronousClient {
messages_issued_(num_threads_) { messages_issued_(num_threads_) {
StartThreads(num_threads_); StartThreads(num_threads_);
} }
virtual ~SynchronousStreamingClient() {} virtual ~SynchronousStreamingClient() {
CleanupAllStreams([this](size_t thread_idx) {
// Don't log any kind of error since we may have canceled this
stream_[thread_idx]->Finish().IgnoreError();
});
}
protected: protected:
std::vector<grpc::ClientContext> context_; std::vector<grpc::ClientContext> context_;
@ -187,18 +192,14 @@ class SynchronousStreamingClient : public SynchronousClient {
new (&context_[thread_idx]) ClientContext(); new (&context_[thread_idx]) ClientContext();
} }
virtual void CleanStream(size_t thread_idx) { void CleanupAllStreams(std::function<void(size_t)> cleaner) {
context_[thread_idx].TryCancel();
}
void CleanupAllStreams() {
std::vector<std::thread> cleanup_threads; std::vector<std::thread> cleanup_threads;
for (size_t i = 0; i < num_threads_; i++) { for (size_t i = 0; i < num_threads_; i++) {
cleanup_threads.emplace_back([this, i] { cleanup_threads.emplace_back([this, i, cleaner] {
std::lock_guard<std::mutex> l(stream_mu_[i]); std::lock_guard<std::mutex> l(stream_mu_[i]);
shutdown_[i].val = true; shutdown_[i].val = true;
if (stream_[i]) { if (stream_[i]) {
CleanStream(i); cleaner(i);
} }
}); });
} }
@ -209,7 +210,8 @@ class SynchronousStreamingClient : public SynchronousClient {
private: private:
void DestroyMultithreading() override final { void DestroyMultithreading() override final {
CleanupAllStreams(); CleanupAllStreams(
[this](size_t thread_idx) { context_[thread_idx].TryCancel(); });
EndThreads(); EndThreads();
} }
}; };
@ -220,7 +222,10 @@ class SynchronousStreamingPingPongClient final
public: public:
SynchronousStreamingPingPongClient(const ClientConfig& config) SynchronousStreamingPingPongClient(const ClientConfig& config)
: SynchronousStreamingClient(config) {} : SynchronousStreamingClient(config) {}
~SynchronousStreamingPingPongClient() { CleanupAllStreams(); } ~SynchronousStreamingPingPongClient() {
CleanupAllStreams(
[this](size_t thread_idx) { stream_[thread_idx]->WritesDone(); });
}
private: private:
bool InitThreadFuncImpl(size_t thread_idx) override { bool InitThreadFuncImpl(size_t thread_idx) override {
@ -267,12 +272,6 @@ class SynchronousStreamingPingPongClient final
messages_issued_[thread_idx] = 0; messages_issued_[thread_idx] = 0;
return true; return true;
} }
void CleanStream(size_t thread_idx) override {
stream_[thread_idx]->WritesDone();
// Don't log any kind of error since we may have canceled this
stream_[thread_idx]->Finish().IgnoreError();
}
}; };
class SynchronousStreamingFromClientClient final class SynchronousStreamingFromClientClient final
@ -280,7 +279,10 @@ class SynchronousStreamingFromClientClient final
public: public:
SynchronousStreamingFromClientClient(const ClientConfig& config) SynchronousStreamingFromClientClient(const ClientConfig& config)
: SynchronousStreamingClient(config), last_issue_(num_threads_) {} : SynchronousStreamingClient(config), last_issue_(num_threads_) {}
~SynchronousStreamingFromClientClient() { CleanupAllStreams(); } ~SynchronousStreamingFromClientClient() {
CleanupAllStreams(
[this](size_t thread_idx) { stream_[thread_idx]->WritesDone(); });
}
private: private:
std::vector<double> last_issue_; std::vector<double> last_issue_;
@ -323,12 +325,6 @@ class SynchronousStreamingFromClientClient final
} }
return true; return true;
} }
void CleanStream(size_t thread_idx) override {
stream_[thread_idx]->WritesDone();
// Don't log any kind of error since we may have canceled this
stream_[thread_idx]->Finish().IgnoreError();
}
}; };
class SynchronousStreamingFromServerClient final class SynchronousStreamingFromServerClient final
@ -336,7 +332,7 @@ class SynchronousStreamingFromServerClient final
public: public:
SynchronousStreamingFromServerClient(const ClientConfig& config) SynchronousStreamingFromServerClient(const ClientConfig& config)
: SynchronousStreamingClient(config), last_recv_(num_threads_) {} : SynchronousStreamingClient(config), last_recv_(num_threads_) {}
~SynchronousStreamingFromServerClient() { CleanupAllStreams(); } ~SynchronousStreamingFromServerClient() {}
private: private:
std::vector<double> last_recv_; std::vector<double> last_recv_;
@ -374,11 +370,6 @@ class SynchronousStreamingFromServerClient final
} }
return true; return true;
} }
void CleanStream(size_t thread_idx) override {
// Don't log any kind of error since we may have canceled this
stream_[thread_idx]->Finish().IgnoreError();
}
}; };
class SynchronousStreamingBothWaysClient final class SynchronousStreamingBothWaysClient final
@ -387,7 +378,10 @@ class SynchronousStreamingBothWaysClient final
public: public:
SynchronousStreamingBothWaysClient(const ClientConfig& config) SynchronousStreamingBothWaysClient(const ClientConfig& config)
: SynchronousStreamingClient(config) {} : SynchronousStreamingClient(config) {}
~SynchronousStreamingBothWaysClient() { CleanupAllStreams(); } ~SynchronousStreamingBothWaysClient() {
CleanupAllStreams(
[this](size_t thread_idx) { stream_[thread_idx]->WritesDone(); });
}
private: private:
bool InitThreadFuncImpl(size_t thread_idx) override { bool InitThreadFuncImpl(size_t thread_idx) override {
@ -405,12 +399,6 @@ class SynchronousStreamingBothWaysClient final
// TODO (vjpai): Do this // TODO (vjpai): Do this
return true; return true;
} }
void CleanStream(size_t thread_idx) override {
stream_[thread_idx]->WritesDone();
// Don't log any kind of error since we may have canceled this
stream_[thread_idx]->Finish().IgnoreError();
}
}; };
std::unique_ptr<Client> CreateSynchronousClient(const ClientConfig& config) { std::unique_ptr<Client> CreateSynchronousClient(const ClientConfig& config) {

Loading…
Cancel
Save