QueueAppenderBenchmark.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2017-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/Format.h>
  18. #include <folly/Range.h>
  19. #include <folly/io/Cursor.h>
  20. #include <folly/io/IOBufQueue.h>
  21. DECLARE_bool(benchmark);
  22. using namespace folly::io;
  23. constexpr size_t kBenchmarkSize = 4096;
  24. template <class T>
  25. void runArithmeticBench(int64_t iters) {
  26. while (iters--) {
  27. folly::IOBufQueue queue;
  28. QueueAppender appender(&queue, kBenchmarkSize);
  29. for (size_t i = 0; i < kBenchmarkSize / sizeof(T); ++i) {
  30. appender.write((T)0xFB);
  31. }
  32. folly::doNotOptimizeAway(queue.move());
  33. }
  34. }
  35. BENCHMARK(write_uint8, iters) {
  36. runArithmeticBench<uint8_t>(iters);
  37. }
  38. BENCHMARK(write_uint16, iters) {
  39. runArithmeticBench<uint16_t>(iters);
  40. }
  41. BENCHMARK(write_uint32, iters) {
  42. runArithmeticBench<uint32_t>(iters);
  43. }
  44. void runPushBenchmark(int64_t iters, const std::string& str) {
  45. constexpr size_t kNumPushPerIter = 1024;
  46. while (iters--) {
  47. folly::IOBufQueue queue;
  48. QueueAppender appender(&queue, kBenchmarkSize);
  49. for (size_t i = 0; i < kNumPushPerIter; ++i) {
  50. appender.push(reinterpret_cast<const uint8_t*>(str.data()), str.size());
  51. }
  52. folly::doNotOptimizeAway(queue.move());
  53. }
  54. }
  55. BENCHMARK(push_64b, iters) {
  56. std::string data;
  57. BENCHMARK_SUSPEND {
  58. data = std::string(64, 'f');
  59. }
  60. runPushBenchmark(iters, data);
  61. }
  62. BENCHMARK(push_1024b, iters) {
  63. std::string data;
  64. BENCHMARK_SUSPEND {
  65. data = std::string(1024, 'b');
  66. }
  67. runPushBenchmark(iters, data);
  68. }
  69. BENCHMARK(append, iters) {
  70. constexpr size_t kNumAppendPerIter = 1024;
  71. std::unique_ptr<folly::IOBuf> largeBuffer;
  72. BENCHMARK_SUSPEND {
  73. largeBuffer = folly::IOBuf::create(1024);
  74. largeBuffer->append(1024);
  75. }
  76. while (iters--) {
  77. folly::IOBufQueue queue;
  78. QueueAppender appender(&queue, kBenchmarkSize);
  79. for (size_t i = 0; i < kNumAppendPerIter; ++i) {
  80. appender.insert(largeBuffer->clone());
  81. }
  82. folly::doNotOptimizeAway(queue.move());
  83. }
  84. }
  85. void preallocate_postallocate_bench(int64_t iters, size_t size) {
  86. std::string data;
  87. BENCHMARK_SUSPEND {
  88. data = std::string(size, 'f');
  89. }
  90. while (iters--) {
  91. folly::IOBufQueue queue;
  92. for (size_t i = 0; i < kBenchmarkSize; ++i) {
  93. auto range = queue.preallocate(size, kBenchmarkSize);
  94. memcpy(range.first, data.data(), size);
  95. queue.postallocate(size);
  96. }
  97. folly::doNotOptimizeAway(queue.move());
  98. }
  99. }
  100. BENCHMARK(preallocate_postallocate_1b, iters) {
  101. preallocate_postallocate_bench(iters, 1);
  102. }
  103. BENCHMARK(preallocate_postallocate_4b, iters) {
  104. preallocate_postallocate_bench(iters, 4);
  105. }
  106. BENCHMARK(preallocate_postallocate_32b, iters) {
  107. preallocate_postallocate_bench(iters, 32);
  108. }
  109. BENCHMARK(preallocate_postallocate_256b, iters) {
  110. preallocate_postallocate_bench(iters, 256);
  111. }
  112. int main(int argc, char** argv) {
  113. gflags::ParseCommandLineFlags(&argc, &argv, true);
  114. folly::runBenchmarks();
  115. return 0;
  116. }