Skip to content

Simple benchmark to compare OwnedImpl::add to WatermarkBuffer::add - #19169

Merged
ggreenway merged 2 commits into
envoyproxy:mainfrom
antoniovicente:buffer_add_benchmark
Dec 3, 2021
Merged

ggreenway merged 2 commits into
envoyproxy:mainfrom
antoniovicente:buffer_add_benchmark

Conversation

@antoniovicente

Copy link
Copy Markdown
Contributor

Commit Message:
Simple benchmark to compare OwnedImpl::add to WatermarkBuffer::add

Also, benchmark to cover buffer account use.

Risk Level: n/a (benchmark, test-only changes)
Testing: test-only changes
Docs Changes: n/a
Release Notes: n/a

Also, benchmark to cover buffer account use.

Signed-off-by: Antonio Vicente <[email protected]>
@antoniovicente

Copy link
Copy Markdown
Contributor Author

Benchmark results included below. The summary is that the high watermark checks can have a measurable performance impact when doing a lot of small writes as seen during HTTP1 header serialization to the output buffer. The bulk of the impact seems to come from use of WatermarkBuffer instead of OwnedImpl even if the high-watermark is not set. WatermarkBuffer is most expensive in cases where the high-watermark is already triggered due to additional checks needed to determine if the overflow watermark callback should be invoked.

Accounts may have some performance impact when doing a large number of mid-sized writes but the performance impact is low.

I'm wondering if there's some room for improvement in how high/low watermark checks are done. Strawperson: Have a RAII scoped object that wraps code regions where a specific buffer may increase/decrease in size. Record the size of the buffer when entering the block. Check if it has increased/decreased when exiting the block and invoke the appropriate high/low callback. There should be an ASSERT at the top of each buffer mutation method to verify that the mutation call happened in the context of a mutation enabler RAII scoped object in order to ensure in debug mode that all mutations happen in contexts which will trigger watermark checks afterwards. In addition to performance, benefits of this approach may include limiting the number of different call stacks in which high/low watermark callbacks are triggered.

------------------------------------------------------------------------------------
Benchmark                                          Time             CPU   Iterations
------------------------------------------------------------------------------------
bufferVsWatermarkBuffer/1024/0/0               14627 ns        14495 ns        48421
bufferVsWatermarkBuffer/1024/0/1               16334 ns        16200 ns        43364
bufferVsWatermarkBuffer/1024/1/1               18178 ns        17931 ns        39302
bufferVsWatermarkBuffer/1024/1024/1            17775 ns        17504 ns        40084
bufferVsWatermarkBuffer/65536/0/0             928109 ns       920868 ns          762
bufferVsWatermarkBuffer/65536/0/1            1032415 ns      1016223 ns          689
bufferVsWatermarkBuffer/65536/1/1            1119637 ns      1113670 ns          630
bufferVsWatermarkBuffer/65536/65536/1        1107559 ns      1093671 ns          628
bufferVsWatermarkBuffer/1048576/0/0         13779528 ns     13711828 ns           51
bufferVsWatermarkBuffer/1048576/0/1         16559163 ns     16478600 ns           43
bufferVsWatermarkBuffer/1048576/1/1         18023705 ns     17927382 ns           39
bufferVsWatermarkBuffer/1048576/1048576/1   17611295 ns     17543661 ns           40
bufferAccountUse/1/1048576/0                14139123 ns     14102097 ns           50
bufferAccountUse/1/1048576/1                14111626 ns     14086420 ns           49
bufferAccountUse/1024/1024/0                   93149 ns        92729 ns         7751
bufferAccountUse/1024/1024/1                  100165 ns        99974 ns         7027
bufferAccountUse/4096/1024/0                  431956 ns       431091 ns         1597
bufferAccountUse/4096/1024/1                  471759 ns       470740 ns         1504
bufferAccountUse/16384/1024/0                1400297 ns      1396084 ns          497
bufferAccountUse/16384/1024/1                1405353 ns      1400896 ns          490

@antoniovicente

Copy link
Copy Markdown
Contributor Author

cc @wbpcode

@wbpcode

wbpcode commented Dec 2, 2021 •

Copy link
Copy Markdown
Member

@antoniovicente 👍 Looks great.

May be we also can add some more test to observe the impact of write unit length on buffer performance
For example, write fixed-length data (such as 100MB) to the buffer, but the length of the written unit is different (1byte, 16bytes, 32bytes, 64bytes, 256bytes, 512bytes, 1kb, 4kb, 8kb, 16kb, 32kb).

alyssawilk
alyssawilk previously approved these changes Dec 2, 2021

@alyssawilk alyssawilk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome - love having more data here!

@alyssawilk

Copy link
Copy Markdown
Contributor

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #19169 (comment) was created by @alyssawilk.

see: more, trace.

@KBaichoo KBaichoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these benchmarks, looks good otherwise.

Comment thread test/common/buffer/buffer_speed_test.cc Outdated
UNREFERENCED_PARAMETER(_);
Buffer::OwnedImpl buffer;
if (enable_accounts) {
auto account = buffer_factory.createAccount(reset_handler);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll want to call clearDownstream() on this account when we're finished with it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reset_handler outlives the account so it shouldn't be needed. I changed the benchmark so clearDownstream() is called.

Comment thread test/common/buffer/buffer_speed_test.cc Outdated
buffer->add("a", 1);
}
}
benchmark::DoNotOptimize(length);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think this is needed for this benchmark (and probably not in a lot of the other ones where it has been cargo culted)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I removed this one, the remaining ones seem correct.

Signed-off-by: Antonio Vicente <[email protected]>
@antoniovicente

Copy link
Copy Markdown
Contributor Author

@antoniovicente +1 Looks great.

May be we also can add some more test to observe the impact of write unit length on buffer performance For example, write fixed-length data (such as 100MB) to the buffer, but the length of the written unit is different (1byte, 16bytes, 32bytes, 64bytes, 256bytes, 512bytes, 1kb, 4kb, 8kb, 16kb, 32kb).

Added a 32 byte write case to contrast with the 1 byte write case. Full results below. As expected, the reduction in number of buffer operations results in a significantly faster benchmark result. I think that the benchmark you're reaching for is add(size) vs reserve(N*size) plus memcpy's which is a different situation. Still I hope that we can use these simple benchmarks as a starting point towards understanding where time is spent and if we can do anything to shave off some cycles.

---------------------------------------------------------------------------------------
Benchmark                                             Time             CPU   Iterations
---------------------------------------------------------------------------------------
bufferVsWatermarkBuffer/1024/0/1/0                18233 ns        18081 ns        38858
bufferVsWatermarkBuffer/1024/0/1/1                20584 ns        20471 ns        34271
bufferVsWatermarkBuffer/1024/1/1/1                23219 ns        23112 ns        29933
bufferVsWatermarkBuffer/1024/1024/1/1             22505 ns        22353 ns        31517
bufferVsWatermarkBuffer/65536/0/1/0             1151082 ns      1145282 ns          614
bufferVsWatermarkBuffer/65536/0/1/1             1417067 ns      1299632 ns          532
bufferVsWatermarkBuffer/65536/1/1/1             1481346 ns      1466985 ns          478
bufferVsWatermarkBuffer/65536/65536/1/1         1422413 ns      1415887 ns          497
bufferVsWatermarkBuffer/65536/0/32/0              37502 ns        37301 ns        18709
bufferVsWatermarkBuffer/65536/65536/32/1          45605 ns        45335 ns        15410
bufferVsWatermarkBuffer/1048576/0/1/0          18442054 ns     18376078 ns           38
bufferVsWatermarkBuffer/1048576/0/1/1          20929929 ns     20872865 ns           33
bufferVsWatermarkBuffer/1048576/1/1/1          23665547 ns     23572933 ns           30
bufferVsWatermarkBuffer/1048576/1048576/1/1    22824872 ns     22776911 ns           31
bufferVsWatermarkBuffer/1048576/0/32/0           605743 ns       604556 ns         1164
bufferVsWatermarkBuffer/1048576/1048576/32/1     733903 ns       732069 ns          961

@ggreenway
ggreenway merged commit 3c27f5f into envoyproxy:main Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants