Skip to content

Commit 2874d75

Browse files
mlh758xuri
authored andcommitted
Add benchmark for adding images to sheet (qax-os#367)
* Add benchmark for adding images to sheet This should help track performance regressions in future changes. * Only transform sheet name if necessary
1 parent 677a22d commit 2874d75

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
~$*.xlsx
22
test/Test*.xlsx
3+
*.out
4+
test/image3.png
5+
*.test

picture_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package excelize
2+
3+
import (
4+
"fmt"
5+
_ "image/png"
6+
"io/ioutil"
7+
"testing"
8+
)
9+
10+
func BenchmarkAddPictureFromBytes(b *testing.B) {
11+
f := NewFile()
12+
imgFile, err := ioutil.ReadFile("logo.png")
13+
if err != nil {
14+
panic("unable to load image for benchmark")
15+
}
16+
b.ResetTimer()
17+
for i := 1; i <= b.N; i++ {
18+
f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", i), "", "logo", ".png", imgFile)
19+
}
20+
}

sheet.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -778,18 +778,20 @@ func (f *File) UnprotectSheet(sheet string) {
778778
// trimSheetName provides a function to trim invaild characters by given worksheet
779779
// name.
780780
func trimSheetName(name string) string {
781-
var r []rune
782-
for _, v := range name {
783-
switch v {
784-
case 58, 92, 47, 63, 42, 91, 93: // replace :\/?*[]
785-
continue
786-
default:
787-
r = append(r, v)
781+
if strings.ContainsAny(name, ":\\/?*[]") || utf8.RuneCountInString(name) > 31 {
782+
r := make([]rune, 0, 31)
783+
for _, v := range name {
784+
switch v {
785+
case 58, 92, 47, 63, 42, 91, 93: // replace :\/?*[]
786+
continue
787+
default:
788+
r = append(r, v)
789+
}
790+
if len(r) == 31 {
791+
break
792+
}
788793
}
789-
}
790-
name = string(r)
791-
if utf8.RuneCountInString(name) > 31 {
792-
name = string([]rune(name)[0:31])
794+
name = string(r)
793795
}
794796
return name
795797
}

0 commit comments

Comments
 (0)