|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <common/init/Init.h> |
| 9 | +#include <folly/Benchmark.h> |
| 10 | +#include <quic/priority/HTTPPriorityQueue.h> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +using namespace std; |
| 14 | +using namespace folly; |
| 15 | + |
| 16 | +static inline void insert( |
| 17 | + quic::HTTPPriorityQueue& pq, |
| 18 | + size_t numConcurrentStreams, |
| 19 | + bool incremental) { |
| 20 | + // insert streams at various priorities |
| 21 | + for (size_t i = 0; i < numConcurrentStreams; i++) { |
| 22 | + pq.insertOrUpdate( |
| 23 | + quic::PriorityQueue::Identifier::fromStreamID(i), |
| 24 | + quic::HTTPPriorityQueue::Priority(i % 8, incremental)); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +static inline void processQueueIncremental( |
| 29 | + quic::HTTPPriorityQueue& pq, |
| 30 | + size_t numConcurrentStreams, |
| 31 | + size_t packetsPerStream, |
| 32 | + uint8_t shift) { |
| 33 | + CHECK_GT(packetsPerStream, 0); |
| 34 | + CHECK_EQ(numConcurrentStreams % 8, 0) << "requires equal streams per urgency"; |
| 35 | + |
| 36 | + for (uint8_t urgency = 0; urgency < 8; urgency++) { |
| 37 | + for (size_t i = 0; |
| 38 | + i < (numConcurrentStreams / 8 + shift) * (packetsPerStream - 1); |
| 39 | + i++) { |
| 40 | + (void)pq.getNextScheduledID(quic::none); |
| 41 | + } |
| 42 | + for (size_t i = 0; i < (numConcurrentStreams / 8); i++) { |
| 43 | + auto id = pq.getNextScheduledID(quic::none); |
| 44 | + // LOG(INFO) << id.asStreamID(); |
| 45 | + pq.erase(id); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +static inline void processQueueSequential( |
| 51 | + quic::HTTPPriorityQueue& pq, |
| 52 | + size_t numConcurrentStreams, |
| 53 | + size_t packetsPerStream) { |
| 54 | + CHECK_GT(packetsPerStream, 0); |
| 55 | + for (size_t i = 0; i < numConcurrentStreams; i++) { |
| 56 | + quic::PriorityQueue::Identifier id; |
| 57 | + for (size_t p = 0; p < packetsPerStream; p++) { |
| 58 | + id = pq.getNextScheduledID(quic::none); |
| 59 | + // LOG(INFO) << id.asStreamID(); |
| 60 | + } |
| 61 | + pq.erase(id); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +static inline void benchmarkPriority( |
| 66 | + size_t numConcurrentStreams, |
| 67 | + bool incremental) { |
| 68 | + quic::HTTPPriorityQueue pq; |
| 69 | + insert(pq, numConcurrentStreams, incremental); |
| 70 | + |
| 71 | + size_t packetsPerStream = 4; |
| 72 | + if (incremental) { |
| 73 | + processQueueIncremental(pq, numConcurrentStreams, packetsPerStream, 1); |
| 74 | + } else { |
| 75 | + processQueueSequential(pq, numConcurrentStreams, packetsPerStream); |
| 76 | + } |
| 77 | + CHECK(pq.empty()); |
| 78 | +} |
| 79 | + |
| 80 | +BENCHMARK(sequential, n) { |
| 81 | + for (size_t i = 0; i < n; i++) { |
| 82 | + benchmarkPriority(96, false); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +BENCHMARK(sequentialCrossover, n) { |
| 87 | + for (size_t i = 0; i < n; i++) { |
| 88 | + benchmarkPriority(40, false); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +BENCHMARK(incremental, n) { |
| 93 | + for (size_t i = 0; i < n; i++) { |
| 94 | + benchmarkPriority(96, true); |
| 95 | + } |
| 96 | +} |
| 97 | +BENCHMARK(sequential8, n) { |
| 98 | + for (size_t i = 0; i < n; i++) { |
| 99 | + benchmarkPriority(8, false); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +BENCHMARK(incremental8, n) { |
| 104 | + for (size_t i = 0; i < n; i++) { |
| 105 | + benchmarkPriority(8, true); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +BENCHMARK(insertSequential, n) { |
| 110 | + // insert streams at various priorities |
| 111 | + for (size_t j = 0; j < n; j++) { |
| 112 | + quic::HTTPPriorityQueue pq; |
| 113 | + insert(pq, 100, false); |
| 114 | + pq.clear(); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +BENCHMARK(insertIncremental, n) { |
| 119 | + // insert streams at various priorities |
| 120 | + for (size_t j = 0; j < n; j++) { |
| 121 | + quic::HTTPPriorityQueue pq; |
| 122 | + insert(pq, 100, true); |
| 123 | + pq.clear(); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +BENCHMARK(processSequential, n) { |
| 128 | + // insert streams at various priorities |
| 129 | + size_t nStreams = 96; |
| 130 | + for (size_t j = 0; j < n; j++) { |
| 131 | + quic::HTTPPriorityQueue pq; |
| 132 | + BENCHMARK_SUSPEND { |
| 133 | + insert(pq, nStreams, false); |
| 134 | + } |
| 135 | + processQueueSequential(pq, nStreams, 4); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +BENCHMARK(processIncremental, n) { |
| 140 | + // insert streams at various priorities |
| 141 | + size_t nStreams = 96; |
| 142 | + for (size_t j = 0; j < n; j++) { |
| 143 | + quic::HTTPPriorityQueue pq; |
| 144 | + BENCHMARK_SUSPEND { |
| 145 | + insert(pq, nStreams, true); |
| 146 | + } |
| 147 | + processQueueIncremental(pq, nStreams, 4, 0); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +BENCHMARK(eraseSequential, n) { |
| 152 | + // insert streams at various priorities |
| 153 | + size_t nStreams = 96; |
| 154 | + for (size_t j = 0; j < n; j++) { |
| 155 | + quic::HTTPPriorityQueue pq; |
| 156 | + BENCHMARK_SUSPEND { |
| 157 | + insert(pq, nStreams, false); |
| 158 | + } |
| 159 | + while (!pq.empty()) { |
| 160 | + pq.erase(pq.getNextScheduledID(quic::none)); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +BENCHMARK(eraseIncremental, n) { |
| 166 | + // insert streams at various priorities |
| 167 | + size_t nStreams = 96; |
| 168 | + for (size_t j = 0; j < n; j++) { |
| 169 | + quic::HTTPPriorityQueue pq; |
| 170 | + BENCHMARK_SUSPEND { |
| 171 | + insert(pq, nStreams, true); |
| 172 | + } |
| 173 | + while (!pq.empty()) { |
| 174 | + pq.erase(pq.getNextScheduledID(quic::none)); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +int main(int argc, char** argv) { |
| 180 | + facebook::initFacebook(&argc, &argv); |
| 181 | + runBenchmarks(); |
| 182 | + return 0; |
| 183 | +} |
0 commit comments