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

[pkg/ottl] Improve time performance #35129

Merged

Conversation

TylerHelmuth
Copy link
Member

Description:
Improves Time performance by move the conversion from our format to Go's format to happen during startup.

Benchmarks before:

goos: darwin
goarch: arm64
pkg: github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs
Benchmark_Time/simple_short_form-10         	1000000000	         0.0000079 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/simple_short_form_with_short_year_and_slashes-10         	1000000000	         0.0000115 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/month_day_year-10                                        	1000000000	         0.0000057 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/simple_long_form-10                                      	1000000000	         0.0000075 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/date_with_timestamp-10                                   	1000000000	         0.0000063 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/day_of_the_week_long_form-10                             	1000000000	         0.0000085 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_weekday,_short_month,_long_format-10               	1000000000	         0.0000089 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_months-10                                          	1000000000	         0.0000070 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/timestamp_with_time_zone_offset-10                       	1000000000	         0.0000665 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_date_with_timestamp_without_time_zone_offset-10    	1000000000	         0.0000428 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format-10                             	1000000000	         0.0000345 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format_before_2000-10                 	1000000000	         0.0000349 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/no_location-10                                           	1000000000	         0.0000035 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/with_location_-_America-10                               	1000000000	         0.0000104 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/with_location_-_Asia-10                                  	1000000000	         0.0000084 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format_before_2000,_ignore_default_location-10         	1000000000	         0.0000379 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs	0.458s

Benchmark's after:

goos: darwin
goarch: arm64
pkg: github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs
Benchmark_Time/simple_short_form-10         	1000000000	         0.0000054 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/simple_short_form_with_short_year_and_slashes-10         	1000000000	         0.0000037 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/month_day_year-10                                        	1000000000	         0.0000053 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/simple_long_form-10                                      	1000000000	         0.0000042 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/date_with_timestamp-10                                   	1000000000	         0.0000087 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/day_of_the_week_long_form-10                             	1000000000	         0.0000035 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_weekday,_short_month,_long_format-10               	1000000000	         0.0000036 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_months-10                                          	1000000000	         0.0000031 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/timestamp_with_time_zone_offset-10                       	1000000000	         0.0000491 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_date_with_timestamp_without_time_zone_offset-10    	1000000000	         0.0000381 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format-10                             	1000000000	         0.0000365 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format_before_2000-10                 	1000000000	         0.0000364 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/no_location-10                                           	1000000000	         0.0000028 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/with_location_-_America-10                               	1000000000	         0.0000017 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/with_location_-_Asia-10                                  	1000000000	         0.0000028 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format_before_2000,_ignore_default_location-10         	1000000000	         0.0000393 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs	0.441s

Link to tracking Issue:
Closes #35078

Testing:
Added benchmark test

@TylerHelmuth TylerHelmuth added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Sep 10, 2024
@TylerHelmuth TylerHelmuth requested review from a team and djaglowski September 10, 2024 20:38
@atoulme
Copy link
Contributor

atoulme commented Sep 10, 2024

I fed your benchmark results into benchstat:

goos: darwin
goarch: arm64
pkg: github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs
                                                                        │  /tmp/before.txt   │               /tmp/after.txt                │
                                                                        │       sec/op       │       sec/op        vs base                 │
_Time/simple_short_form-10                                                0.000007900n ± ∞ ¹   0.000005400n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/simple_short_form_with_short_year_and_slashes-10                    0.000011500n ± ∞ ¹   0.000003700n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/month_day_year-10                                                   0.000005700n ± ∞ ¹   0.000005300n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/simple_long_form-10                                                 0.000007500n ± ∞ ¹   0.000004200n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/date_with_timestamp-10                                              0.000006300n ± ∞ ¹   0.000008700n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/day_of_the_week_long_form-10                                        0.000008500n ± ∞ ¹   0.000003500n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/short_weekday,_short_month,_long_format-10                          0.000008900n ± ∞ ¹   0.000003600n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/short_months-10                                                     0.000007000n ± ∞ ¹   0.000003100n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/timestamp_with_time_zone_offset-10                                   0.00006650n ± ∞ ¹    0.00004910n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/short_date_with_timestamp_without_time_zone_offset-10                0.00004280n ± ∞ ¹    0.00003810n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/RFC_3339_in_custom_format-10                                         0.00003450n ± ∞ ¹    0.00003650n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/RFC_3339_in_custom_format_before_2000-10                             0.00003490n ± ∞ ¹    0.00003640n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/no_location-10                                                      0.000003500n ± ∞ ¹   0.000002800n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/with_location_-_America-10                                          0.000010400n ± ∞ ¹   0.000001700n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/with_location_-_Asia-10                                             0.000008400n ± ∞ ¹   0.000002800n ± ∞ ¹        ~ (p=1.000 n=1) ²
_Time/RFC_3339_in_custom_format_before_2000,_ignore_default_location-10    0.00003790n ± ∞ ¹    0.00003930n ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                                                                    0.00001279n         0.000007823n        -38.85%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05

38.85% performance gains!

assert.NoError(t, err)

t.Run(tt.name, func(t *testing.B) {
result, err := exprFunc(nil, nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

you might need to use b.N, and report allocs.

	b.ReportAllocs()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {

@TylerHelmuth TylerHelmuth merged commit 7e8ebd7 into open-telemetry:main Sep 16, 2024
160 of 164 checks passed
@TylerHelmuth TylerHelmuth deleted the ottl-improve-time-performance branch September 16, 2024 22:19
@github-actions github-actions bot added this to the next release milestone Sep 16, 2024
jriguera pushed a commit to springernature/opentelemetry-collector-contrib that referenced this pull request Oct 4, 2024
**Description:**
Improves `Time` performance by move the conversion from our format to
Go's format to happen during startup.

Benchmarks before:

```
goos: darwin
goarch: arm64
pkg: github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs
Benchmark_Time/simple_short_form-10         	1000000000	         0.0000079 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/simple_short_form_with_short_year_and_slashes-10         	1000000000	         0.0000115 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/month_day_year-10                                        	1000000000	         0.0000057 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/simple_long_form-10                                      	1000000000	         0.0000075 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/date_with_timestamp-10                                   	1000000000	         0.0000063 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/day_of_the_week_long_form-10                             	1000000000	         0.0000085 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_weekday,_short_month,_long_format-10               	1000000000	         0.0000089 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_months-10                                          	1000000000	         0.0000070 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/timestamp_with_time_zone_offset-10                       	1000000000	         0.0000665 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_date_with_timestamp_without_time_zone_offset-10    	1000000000	         0.0000428 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format-10                             	1000000000	         0.0000345 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format_before_2000-10                 	1000000000	         0.0000349 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/no_location-10                                           	1000000000	         0.0000035 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/with_location_-_America-10                               	1000000000	         0.0000104 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/with_location_-_Asia-10                                  	1000000000	         0.0000084 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format_before_2000,_ignore_default_location-10         	1000000000	         0.0000379 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs	0.458s
```

Benchmark's after:

```
goos: darwin
goarch: arm64
pkg: github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs
Benchmark_Time/simple_short_form-10         	1000000000	         0.0000054 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/simple_short_form_with_short_year_and_slashes-10         	1000000000	         0.0000037 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/month_day_year-10                                        	1000000000	         0.0000053 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/simple_long_form-10                                      	1000000000	         0.0000042 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/date_with_timestamp-10                                   	1000000000	         0.0000087 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/day_of_the_week_long_form-10                             	1000000000	         0.0000035 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_weekday,_short_month,_long_format-10               	1000000000	         0.0000036 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_months-10                                          	1000000000	         0.0000031 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/timestamp_with_time_zone_offset-10                       	1000000000	         0.0000491 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/short_date_with_timestamp_without_time_zone_offset-10    	1000000000	         0.0000381 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format-10                             	1000000000	         0.0000365 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format_before_2000-10                 	1000000000	         0.0000364 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/no_location-10                                           	1000000000	         0.0000028 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/with_location_-_America-10                               	1000000000	         0.0000017 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/with_location_-_Asia-10                                  	1000000000	         0.0000028 ns/op	       0 B/op	       0 allocs/op
Benchmark_Time/RFC_3339_in_custom_format_before_2000,_ignore_default_location-10         	1000000000	         0.0000393 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs	0.441s
```

**Link to tracking Issue:** <Issue number if applicable>
Closes
open-telemetry#35078

**Testing:** <Describe what testing was performed and which tests were
added.>
Added benchmark test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg/ottl Skip Changelog PRs that do not require a CHANGELOG.md entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ottl] ParseTime should pre-convert from strptime to gotime format
3 participants