Skip to content

perf(config/file): optimize file format func #3610

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

Merged
merged 2 commits into from
Apr 23, 2025

Conversation

1911860538
Copy link
Contributor

@1911860538 1911860538 commented Apr 3, 2025

Below is my local test code.
format_test.go

package kratos

import (
	"fmt"
	"strings"
	"testing"
)

func format(name string) string {
	if p := strings.Split(name, "."); len(p) > 1 {
		return p[len(p)-1]
	}
	return ""
}

func formatOptimized(name string) string {
	if idx := strings.LastIndex(name, "."); idx >= 0 {
		return name[idx+1:]
	}
	return ""
}

func TestFormatFunctions(t *testing.T) {
	testCases := []struct {
		name     string
		input    string
		expected string
	}{
		{"Simple case", "file.txt", "txt"},
		{"Double extension", "archive.tar.gz", "gz"},
		{"No extension", "file", ""},
		{"Dot at the end", "file.", ""},
		{"Multiple dots", "a.b.c.d.ext", "ext"},
		{"Leading and trailing dots", ".hidden.", ""},
		{"Only a dot", ".", ""},
		{"Empty string", "", ""},
		{"Multiple dots with empty segment", "a..b..c.ext", "ext"},
		{"Dot in folder name", "folder.with.dot/file.txt", "txt"},
		{"Spaces in name", " my file .txt ", "txt "},
		{"Uppercase extension", "IMAGE.JPG", "JPG"},
		{"File with special chars", "file-name_123.final.backup.tar.gz", "gz"},
		{"Chinese filename", "文件.数据.备份.db", "db"},
		{"Empty string", "", ""},
		{"A single space", " ", ""},
		{"Hidden file", ".gitignore", "gitignore"},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			original := format(tc.input)
			optimized := formatOptimized(tc.input)

			if original != optimized {
				t.Errorf("Mismatch for input %q: original=%q, optimized=%q", tc.input, original, optimized)
			}

			if original != tc.expected || optimized != tc.expected {
				t.Errorf("Unexpected result, wanted: %s, original got: %s, optimized got: %s", tc.expected, original, optimized)
			}
		})
	}
}

func Benchmark_simpleCase(b *testing.B) {
	f := "abc.txt"

	b.Run(fmt.Sprintf("original_%s", f), func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			_ = format(f)
		}
	})

	b.Run(fmt.Sprintf("optimized_%s", f), func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			_ = formatOptimized(f)
		}
	})
}

func Benchmark_doubleExtension(b *testing.B) {
	f := "abc.txt.gz"

	b.Run(fmt.Sprintf("original_%s", f), func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			_ = format(f)
		}
	})

	b.Run(fmt.Sprintf("optimized_%s", f), func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			_ = formatOptimized(f)
		}
	})
}

func Benchmark_hiddenFile(b *testing.B) {
	f := ".gitignore"

	b.Run(fmt.Sprintf("original_%s", f), func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			_ = format(f)
		}
	})

	b.Run(fmt.Sprintf("optimized_%s", f), func(b *testing.B) {
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			_ = formatOptimized(f)
		}
	})
}

test output

=== RUN   TestFormatFunctions
=== RUN   TestFormatFunctions/Simple_case
--- PASS: TestFormatFunctions/Simple_case (0.00s)
=== RUN   TestFormatFunctions/Double_extension
--- PASS: TestFormatFunctions/Double_extension (0.00s)
=== RUN   TestFormatFunctions/No_extension
--- PASS: TestFormatFunctions/No_extension (0.00s)
=== RUN   TestFormatFunctions/Dot_at_the_end
--- PASS: TestFormatFunctions/Dot_at_the_end (0.00s)
=== RUN   TestFormatFunctions/Multiple_dots
--- PASS: TestFormatFunctions/Multiple_dots (0.00s)
=== RUN   TestFormatFunctions/Leading_and_trailing_dots
--- PASS: TestFormatFunctions/Leading_and_trailing_dots (0.00s)
=== RUN   TestFormatFunctions/Only_a_dot
--- PASS: TestFormatFunctions/Only_a_dot (0.00s)
=== RUN   TestFormatFunctions/Empty_string
--- PASS: TestFormatFunctions/Empty_string (0.00s)
=== RUN   TestFormatFunctions/Multiple_dots_with_empty_segment
--- PASS: TestFormatFunctions/Multiple_dots_with_empty_segment (0.00s)
=== RUN   TestFormatFunctions/Dot_in_folder_name
--- PASS: TestFormatFunctions/Dot_in_folder_name (0.00s)
=== RUN   TestFormatFunctions/Spaces_in_name
--- PASS: TestFormatFunctions/Spaces_in_name (0.00s)
=== RUN   TestFormatFunctions/Uppercase_extension
--- PASS: TestFormatFunctions/Uppercase_extension (0.00s)
=== RUN   TestFormatFunctions/File_with_special_chars
--- PASS: TestFormatFunctions/File_with_special_chars (0.00s)
=== RUN   TestFormatFunctions/Chinese_filename
--- PASS: TestFormatFunctions/Chinese_filename (0.00s)
=== RUN   TestFormatFunctions/Empty_string#01
--- PASS: TestFormatFunctions/Empty_string#01 (0.00s)
=== RUN   TestFormatFunctions/A_single_space
--- PASS: TestFormatFunctions/A_single_space (0.00s)
=== RUN   TestFormatFunctions/Hidden_file
--- PASS: TestFormatFunctions/Hidden_file (0.00s)
--- PASS: TestFormatFunctions (0.00s)
PASS

Process finished with the exit code 0

benchmark output

goos: darwin
goarch: amd64
pkg: testgolang/test_strings/kratos
cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
Benchmark_simpleCase
Benchmark_simpleCase/original_abc.txt
Benchmark_simpleCase/original_abc.txt-8         	26287210	        42.79 ns/op	      32 B/op	       1 allocs/op
Benchmark_simpleCase/optimized_abc.txt
Benchmark_simpleCase/optimized_abc.txt-8        	271469425	         4.251 ns/op	       0 B/op	       0 allocs/op
Benchmark_doubleExtension
Benchmark_doubleExtension/original_abc.txt.gz
Benchmark_doubleExtension/original_abc.txt.gz-8 	19794057	        57.31 ns/op	      48 B/op	       1 allocs/op
Benchmark_doubleExtension/optimized_abc.txt.gz
Benchmark_doubleExtension/optimized_abc.txt.gz-8         	327488960	         3.280 ns/op	       0 B/op	       0 allocs/op
Benchmark_hiddenFile
Benchmark_hiddenFile/original_.gitignore
Benchmark_hiddenFile/original_.gitignore-8               	21159012	        47.92 ns/op	      32 B/op	       1 allocs/op
Benchmark_hiddenFile/optimized_.gitignore
Benchmark_hiddenFile/optimized_.gitignore-8              	229040276	         5.033 ns/op	       0 B/op	       0 allocs/op
PASS

Process finished with the exit code 0

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 3, 2025
@dosubot dosubot bot added the LGTM label Apr 23, 2025
@shenqidebaozi shenqidebaozi merged commit a0a243d into go-kratos:main Apr 23, 2025
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LGTM size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants