Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve substitution formatter parsing performance #35498

Merged
merged 12 commits into from
Aug 2, 2024

Conversation

wbpcode
Copy link
Member

@wbpcode wbpcode commented Jul 30, 2024

Commit Message: improve substitution formatter parsing performance
Additional Description:

See #35466

Previous parsing performance result:

---------------------------------------------------------------------------
Benchmark                                 Time             CPU   Iterations
---------------------------------------------------------------------------
BM_AccessLogFormatterSetup            75744 ns        75605 ns         9401

Latest parsing performance result:

---------------------------------------------------------------------------
Benchmark                                 Time             CPU   Iterations
---------------------------------------------------------------------------
BM_AccessLogFormatterSetup             8932 ns         8917 ns        78153

Risk Level: mid. Core code change.
Testing: unit.
Docs Changes: n/a.
Release Notes: no behavior change.
Platform Specific Features: n/a.

@wbpcode
Copy link
Member Author

wbpcode commented Jul 30, 2024

By the way, from the performance results, the json formatter is too slow...

---------------------------------------------------------------------------
Benchmark                                 Time             CPU   Iterations
---------------------------------------------------------------------------
BM_AccessLogFormatterSetup             8932 ns         8917 ns        78153
BM_AccessLogFormatter                   510 ns          505 ns      1396258
BM_StructAccessLogFormatter            7153 ns         7140 ns       103466
BM_TypedStructAccessLogFormatter       5390 ns         5384 ns       113175
BM_JsonAccessLogFormatter             12472 ns        12457 ns        52666
BM_TypedJsonAccessLogFormatter        10999 ns        10985 ns        61242
BM_FormatterCommandParsing             40.1 ns         40.0 ns     17475447

ravenblackx
ravenblackx previously approved these changes Jul 30, 2024
Copy link
Contributor

@ravenblackx ravenblackx left a comment

Choose a reason for hiding this comment

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

Big fan of const std::string& to string_view migration.

Arguably the date formatting change could be a separate PR to the string-parameter change, but I'm okay with approving both together.

Comment on lines 78 to 83
testing::NiceMock<MockTimeSystem> time_system;
ON_CALL(time_system, systemTime())
.WillByDefault(testing::Return(SystemTime(std::chrono::seconds(1234567890))));
ON_CALL(time_system, monotonicTime())
.WillByDefault(testing::Return(MonotonicTime(std::chrono::seconds(1234567890))));

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this block is repeated a lot, probably worth a helper function so you can just do MockTimeSystem time_system = makeMockTime() (the function can still wrap it in a NiceMock but you won't have to say it's so every time, or set up the ON_CALLs)

Copy link
Member Author

@wbpcode wbpcode Jul 30, 2024

Choose a reason for hiding this comment

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

Good idea, let's have a try.

@ravenblackx ravenblackx self-assigned this Jul 30, 2024
@wbpcode
Copy link
Member Author

wbpcode commented Jul 30, 2024

cc @ravenblackx Thanks so much for the approval. And yeah, actually DateFormatter refactoring is a separated PR. See #35450

I developed this patch based on #35450. And I will do a rebase after #35450 is landed.

Signed-off-by: wbpcode <[email protected]>
@@ -16,17 +16,9 @@ namespace Formatter {
class MetadataFormatterCommandParser : public ::Envoy::Formatter::CommandParser {
public:
MetadataFormatterCommandParser();
Copy link
Contributor

Choose a reason for hiding this comment

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

Constructor should be =default or not declared at all now that the impl was removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmmmm, so weird. I need to re run the tests in my local env.

I remember I had fixed all these. Seems some code changes are lost.

Signed-off-by: wbpcode <[email protected]>
@kyessenov
Copy link
Contributor

By the way, from the performance results, the json formatter is too slow...

---------------------------------------------------------------------------
Benchmark                                 Time             CPU   Iterations
---------------------------------------------------------------------------
BM_AccessLogFormatterSetup             8932 ns         8917 ns        78153
BM_AccessLogFormatter                   510 ns          505 ns      1396258
BM_StructAccessLogFormatter            7153 ns         7140 ns       103466
BM_TypedStructAccessLogFormatter       5390 ns         5384 ns       113175
BM_JsonAccessLogFormatter             12472 ns        12457 ns        52666
BM_TypedJsonAccessLogFormatter        10999 ns        10985 ns        61242
BM_FormatterCommandParsing             40.1 ns         40.0 ns     17475447

Any idea why JSON is 20x slower? Is it because it's using some library?

@wbpcode
Copy link
Member Author

wbpcode commented Jul 31, 2024

Any idea why JSON is 20x slower? Is it because it's using some library?

Not sure. But format the output proto struct has taken 10x time than the text formatter.

@ravenblackx
Copy link
Contributor

/retest

ravenblackx
ravenblackx previously approved these changes Jul 31, 2024
@wbpcode
Copy link
Member Author

wbpcode commented Aug 1, 2024

#35450 was landed. Now, it's this PR's turn.

@wbpcode
Copy link
Member Author

wbpcode commented Aug 1, 2024

/assign-from @envoyproxy/senior-maintainers

Copy link

@envoyproxy/senior-maintainers assignee is @mattklein123

🐱

Caused by: a #35498 (comment) was created by @wbpcode.

see: more, trace.

ravenblackx
ravenblackx previously approved these changes Aug 1, 2024
mattklein123
mattklein123 previously approved these changes Aug 2, 2024
@wbpcode wbpcode dismissed stale reviews from mattklein123 and ravenblackx via a5a1e6c August 2, 2024 14:40
@wbpcode
Copy link
Member Author

wbpcode commented Aug 2, 2024

/retest

@wbpcode wbpcode enabled auto-merge (squash) August 2, 2024 16:40
@wbpcode wbpcode merged commit 6c645a1 into envoyproxy:main Aug 2, 2024
51 checks passed
@wbpcode wbpcode deleted the dev-refactor-formatter-with-re2 branch August 2, 2024 20:45
martinduke pushed a commit to martinduke/envoy that referenced this pull request Aug 8, 2024
Commit Message: improve substitution formatter parsing performance
Additional Description:

See envoyproxy#35466

Previous parsing performance result:
```
---------------------------------------------------------------------------
Benchmark                                 Time             CPU   Iterations
---------------------------------------------------------------------------
BM_AccessLogFormatterSetup            75744 ns        75605 ns         9401
```

Latest parsing performance result:
```
---------------------------------------------------------------------------
Benchmark                                 Time             CPU   Iterations
---------------------------------------------------------------------------
BM_AccessLogFormatterSetup             8932 ns         8917 ns        78153
```

Risk Level: mid. Core code change.
Testing: unit.
Docs Changes: n/a.
Release Notes: no behavior change.
Platform Specific Features: n/a.

---------

Signed-off-by: wbpcode <[email protected]>
Signed-off-by: Martin Duke <[email protected]>
asingh-g pushed a commit to asingh-g/envoy that referenced this pull request Aug 20, 2024
Commit Message: improve substitution formatter parsing performance
Additional Description:

See envoyproxy#35466

Previous parsing performance result:
```
---------------------------------------------------------------------------
Benchmark                                 Time             CPU   Iterations
---------------------------------------------------------------------------
BM_AccessLogFormatterSetup            75744 ns        75605 ns         9401
```

Latest parsing performance result:
```
---------------------------------------------------------------------------
Benchmark                                 Time             CPU   Iterations
---------------------------------------------------------------------------
BM_AccessLogFormatterSetup             8932 ns         8917 ns        78153
```

Risk Level: mid. Core code change.
Testing: unit.
Docs Changes: n/a.
Release Notes: no behavior change.
Platform Specific Features: n/a.

---------

Signed-off-by: wbpcode <[email protected]>
Signed-off-by: asingh-g <[email protected]>
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.

use re2 for the substitution formatter parsing and prefer the string view
4 participants