From 1fdb2cc397aae33a30551a6d44d5b484281c6f12 Mon Sep 17 00:00:00 2001 From: Elliott Lawrence Date: Tue, 29 Mar 2022 15:47:16 -0700 Subject: [PATCH 1/2] scheduler benchmark 1 Differential Revision: D35132668 fbshipit-source-id: 0e57940e21b00899687e951ed7f498611b27a88e --- .../test/benchmarks/SchedulerBenchmark.cpp | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/fbpcf/scheduler/test/benchmarks/SchedulerBenchmark.cpp b/fbpcf/scheduler/test/benchmarks/SchedulerBenchmark.cpp index a2c87c30..1e355028 100644 --- a/fbpcf/scheduler/test/benchmarks/SchedulerBenchmark.cpp +++ b/fbpcf/scheduler/test/benchmarks/SchedulerBenchmark.cpp @@ -9,6 +9,10 @@ #include "common/init/Init.h" +#include "fbpcf/engine/communication/IPartyCommunicationAgentFactory.h" +#include "fbpcf/engine/util/test/benchmarks/BenchmarkHelper.h" +#include "fbpcf/engine/util/test/benchmarks/NetworkedBenchmark.h" +#include "fbpcf/scheduler/SchedulerHelper.h" #include "fbpcf/scheduler/test/benchmarks/AllocatorBenchmark.h" #include "fbpcf/scheduler/test/benchmarks/WireKeeperBenchmark.h" @@ -87,6 +91,95 @@ BENCHMARK(WireKeeperBenchmark_decreaseReferenceCount, n) { wireKeeper->decreaseReferenceCount(wireIds.at(n)); } } + +// Scheduler benchmarks + +class SchedulerBenchmark : public engine::util::NetworkedBenchmark { + public: + void setup() override { + auto [agentFactory0, agentFactory1] = + engine::util::getSocketAgentFactories(); + agentFactory0_ = std::move(agentFactory0); + agentFactory1_ = std::move(agentFactory1); + + // Set up randomized inputs + batchInput0_ = engine::util::getRandomBoolVector(batchSize_); + batchInput1_ = engine::util::getRandomBoolVector(batchSize_); + + input0_ = batchInput0_.at(0); + input1_ = batchInput1_.at(0); + + randomParty_ = batchInput0_.at(1); + } + + protected: + void initSender() override { + sender_ = getScheduler(0, *agentFactory0_); + } + + void runSender() override { + runMethod(sender_); + } + + void initReceiver() override { + receiver_ = getScheduler(1, *agentFactory1_); + } + + void runReceiver() override { + runMethod(receiver_); + } + + std::pair getTrafficStatistics() override { + return sender_->getTrafficStatistics(); + } + + virtual std::unique_ptr getScheduler( + int myId, + engine::communication::IPartyCommunicationAgentFactory& + communicationAgentFactory) = 0; + + virtual void runMethod(std::unique_ptr& scheduler) = 0; + + size_t batchSize_ = 1000; + std::vector batchInput0_; + std::vector batchInput1_; + + bool input0_; + bool input1_; + + int randomParty_; + + private: + std::unique_ptr + agentFactory0_; + std::unique_ptr + agentFactory1_; + + std::unique_ptr sender_; + std::unique_ptr receiver_; +}; + +class LazySchedulerBenchmark : virtual public SchedulerBenchmark { + protected: + std::unique_ptr getScheduler( + int myId, + engine::communication::IPartyCommunicationAgentFactory& + communicationAgentFactory) override { + return createLazySchedulerWithInsecureEngine( + myId, communicationAgentFactory); + } +}; + +class EagerSchedulerBenchmark : virtual public SchedulerBenchmark { + protected: + std::unique_ptr getScheduler( + int myId, + engine::communication::IPartyCommunicationAgentFactory& + communicationAgentFactory) override { + return createEagerSchedulerWithInsecureEngine( + myId, communicationAgentFactory); + } +}; } // namespace fbpcf::scheduler int main(int argc, char* argv[]) { From 2e0eb7c0c5351bfa475c87286800f4ae65855147 Mon Sep 17 00:00:00 2001 From: Elliott Lawrence Date: Tue, 29 Mar 2022 15:47:59 -0700 Subject: [PATCH 2/2] Add benchmark for schedulers Summary: Looking for some feedback on this approach. The scheduler API has 40 methods, and trying to test each one individually would be overkill I think. Plus, the implementation for a lot of the gates is very similar. I figured the best thing to do would be to just create a bunch of dependent gates, that way we can test that the lazy scheduler properly batches things together more efficiently than the eager scheduler. If anyone has suggestions for more thorough things to test, LMK. Reviewed By: RuiyuZhu Differential Revision: D35162133 fbshipit-source-id: b29fb9fbd9b6da09e5cf34b23d4f359cbe3188c1 --- .../test/benchmarks/SchedulerBenchmark.cpp | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/fbpcf/scheduler/test/benchmarks/SchedulerBenchmark.cpp b/fbpcf/scheduler/test/benchmarks/SchedulerBenchmark.cpp index 1e355028..2b772e92 100644 --- a/fbpcf/scheduler/test/benchmarks/SchedulerBenchmark.cpp +++ b/fbpcf/scheduler/test/benchmarks/SchedulerBenchmark.cpp @@ -180,6 +180,76 @@ class EagerSchedulerBenchmark : virtual public SchedulerBenchmark { myId, communicationAgentFactory); } }; + +class NonFreeGatesBenchmark : virtual public SchedulerBenchmark { + protected: + void runMethod(std::unique_ptr& scheduler) override { + auto wire1 = scheduler->privateBooleanInput(input0_, randomParty_); + auto wire2 = scheduler->privateBooleanInput(input1_, 1 - randomParty_); + IScheduler::WireId wire3; + for (auto level = 0; level < 10; level++) { + for (auto i = 0; i < 200; i++) { + wire3 = scheduler->privateAndPrivate(wire1, wire2); + } + wire2 = wire3; + } + scheduler->getBooleanValue( + scheduler->openBooleanValueToParty(wire3, randomParty_)); + } +}; + +class NonFreeGatesBatchBenchmark : virtual public SchedulerBenchmark { + protected: + void runMethod(std::unique_ptr& scheduler) override { + auto wire1 = + scheduler->privateBooleanInputBatch(batchInput0_, randomParty_); + auto wire2 = + scheduler->privateBooleanInputBatch(batchInput1_, 1 - randomParty_); + IScheduler::WireId wire3; + for (auto level = 0; level < 10; level++) { + for (auto i = 0; i < 200; i++) { + wire3 = scheduler->privateAndPrivateBatch(wire1, wire2); + } + wire2 = wire3; + } + scheduler->getBooleanValueBatch( + scheduler->openBooleanValueToPartyBatch(wire3, randomParty_)); + } +}; + +class LazyScheduler_NonFreeGates_Benchmark : public LazySchedulerBenchmark, + public NonFreeGatesBenchmark {}; + +BENCHMARK_COUNTERS(LazyScheduler_NonFreeGates, counters) { + LazyScheduler_NonFreeGates_Benchmark benchmark; + benchmark.runBenchmark(counters); +} + +class EagerScheduler_NonFreeGates_Benchmark : public EagerSchedulerBenchmark, + public NonFreeGatesBenchmark {}; + +BENCHMARK_COUNTERS(EagerScheduler_NonFreeGates, counters) { + EagerScheduler_NonFreeGates_Benchmark benchmark; + benchmark.runBenchmark(counters); +} + +class LazyScheduler_NonFreeGatesBatch_Benchmark + : public LazySchedulerBenchmark, + public NonFreeGatesBatchBenchmark {}; + +BENCHMARK_COUNTERS(LazyScheduler_NonFreeGatesBatch, counters) { + LazyScheduler_NonFreeGatesBatch_Benchmark benchmark; + benchmark.runBenchmark(counters); +} + +class EagerScheduler_NonFreeGatesBatch_Benchmark + : public EagerSchedulerBenchmark, + public NonFreeGatesBatchBenchmark {}; + +BENCHMARK_COUNTERS(EagerScheduler_NonFreeGatesBatch, counters) { + EagerScheduler_NonFreeGatesBatch_Benchmark benchmark; + benchmark.runBenchmark(counters); +} } // namespace fbpcf::scheduler int main(int argc, char* argv[]) {