RefCountBenchmark.cpp 1.6 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 <thread>
  17. #include <folly/Benchmark.h>
  18. #include <folly/experimental/TLRefCount.h>
  19. namespace folly {
  20. template <typename Counter>
  21. void shutdown(Counter&) {}
  22. void shutdown(TLRefCount& c) {
  23. c.useGlobal();
  24. --c;
  25. }
  26. template <typename Counter, size_t threadCount>
  27. void benchmark(size_t n) {
  28. Counter x;
  29. std::vector<std::thread> ts;
  30. for (size_t t = 0; t < threadCount; ++t) {
  31. ts.emplace_back([&]() {
  32. for (size_t i = 0; i < n; ++i) {
  33. ++x;
  34. }
  35. for (size_t i = 0; i < n; ++i) {
  36. --x;
  37. }
  38. });
  39. }
  40. for (auto& t : ts) {
  41. t.join();
  42. }
  43. shutdown(x);
  44. }
  45. BENCHMARK(TLRefCountOneThread, n) {
  46. benchmark<TLRefCount, 1>(n);
  47. }
  48. BENCHMARK(TLRefCountFourThreads, n) {
  49. benchmark<TLRefCount, 4>(n);
  50. }
  51. } // namespace folly
  52. int main(int argc, char** argv) {
  53. gflags::ParseCommandLineFlags(&argc, &argv, true);
  54. gflags::SetCommandLineOptionWithMode(
  55. "bm_min_usec", "100000", gflags::SET_FLAG_IF_DEFAULT);
  56. folly::runBenchmarks();
  57. return 0;
  58. }