EventBaseBenchmark.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/Benchmark.h>
  17. #include <folly/io/async/EventBase.h>
  18. #include <folly/portability/GFlags.h>
  19. using namespace folly;
  20. class CountedLoopCallback : public EventBase::LoopCallback {
  21. public:
  22. CountedLoopCallback(EventBase* eventBase, unsigned int count)
  23. : eventBase_(eventBase), count_(count) {}
  24. void runLoopCallback() noexcept override {
  25. --count_;
  26. if (count_ > 0) {
  27. eventBase_->runInLoop(this);
  28. }
  29. }
  30. private:
  31. EventBase* eventBase_;
  32. unsigned int count_;
  33. };
  34. BENCHMARK(timeMeasurementsOn, n) {
  35. EventBase eventBase;
  36. while (n--) {
  37. CountedLoopCallback c(&eventBase, 10);
  38. eventBase.runInLoop(&c);
  39. eventBase.loop();
  40. }
  41. }
  42. BENCHMARK_RELATIVE(timeMeasurementsOff, n) {
  43. EventBase eventBase(/* enableTimeMeasurement */ false);
  44. while (n--) {
  45. CountedLoopCallback c(&eventBase, 10);
  46. eventBase.runInLoop(&c);
  47. eventBase.loop();
  48. }
  49. }
  50. /**
  51. * --bm_min_iters=1000000
  52. *
  53. * ============================================================================
  54. * folly/io/async/test/EventBaseBenchmark.cpp relative time/iter iters/s
  55. * ============================================================================
  56. * timeMeasurementsOn 1.25us 798.33K
  57. * timeMeasurementsOff 214.47% 584.04ns 1.71M
  58. * ============================================================================
  59. */
  60. int main(int argc, char** argv) {
  61. gflags::ParseCommandLineFlags(&argc, &argv, true);
  62. runBenchmarks();
  63. }