Skip to content

Commit c78e5c4

Browse files
Add benchmark for some common files
1 parent 01e1e30 commit c78e5c4

File tree

7 files changed

+39
-12
lines changed

7 files changed

+39
-12
lines changed

CONTRIBUTING.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ Code contributions must respect these rules:
77
- code must be test covered
88
- code must be formatted using gofmt tool
99
- exported names must be documented
10+
11+
**Important**: By submitting a pull request, you agree to allow the project
12+
owner to license your work under the same license as that used by the project.

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ containing the string "foobar" at the start of their first line:
6767
```go
6868
mime, extension := mimetype.Detect([]byte("foobar\nfoo foo bar"))
6969
```
70-
See [TestAppend](https://github.com/gabriel-vasile/mimetype/blob/master/mime_test.go) for a working example.
71-
See [Contribute](https://github.com/gabriel-vasile/mimetype#contributing) if you consider the missing mime type should be included in the library by default.
70+
See [TestAppend](mime_test.go) for a working example.
71+
See [Contributing](CONTRIBUTING.md) if you consider the missing mime type should be included in the library by default.
7272

7373
## Supported mimes
7474
##### Application
@@ -95,5 +95,5 @@ containing specific metadata files.
9595
<img alt="structure" src="mimetype.gif" width="88%">
9696
</div>
9797

98-
## Contribute
99-
See [CONTRIBUTING.md](CONTRIBUTING.md)
98+
## Contributing
99+
See [CONTRIBUTING.md](CONTRIBUTING.md).

mime.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Package mimetype uses magic number signatures to detect the mime type and
2-
// extension of a file.
1+
// Package mimetype uses magic number signatures
2+
// to detect the mime type and extension of a file.
33
package mimetype
44

55
import (
@@ -18,8 +18,8 @@ func Detect(in []byte) (mime, extension string) {
1818
return n.Mime(), n.Extension()
1919
}
2020

21-
// DetectReader returns the mime type and extension of the byte slice read
22-
// from the provided reader.
21+
// DetectReader returns the mime type and extension
22+
// of the byte slice read from the provided reader.
2323
func DetectReader(r io.Reader) (mime, extension string, err error) {
2424
in := make([]byte, matchers.ReadLimit)
2525
n, err := r.Read(in)

mime_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os"
88
"path/filepath"
99
"testing"
10+
11+
"github.com/gabriel-vasile/mimetype/matchers"
1012
)
1113

1214
const testDataDir = "testdata"
@@ -28,6 +30,7 @@ var files = map[string]*Node{
2830

2931
// images
3032
"a.png": Png,
33+
"a.jpg": Jpg,
3134
"a.psd": Psd,
3235
"a.webp": Webp,
3336
"a.tif": Tiff,
@@ -166,3 +169,24 @@ func TestAppend(t *testing.T) {
166169
func TestTreePrint(t *testing.T) {
167170
t.Logf("\n%s", Root.Tree())
168171
}
172+
173+
func BenchmarkMatchDetect(b *testing.B) {
174+
files := []string{"a.png", "a.jpg", "a.pdf", "a.zip", "a.docx", "a.doc"}
175+
data, fLen := [][matchers.ReadLimit]byte{}, len(files)
176+
for _, f := range files {
177+
d := [matchers.ReadLimit]byte{}
178+
179+
file, err := os.Open(filepath.Join(testDataDir, f))
180+
if err != nil {
181+
b.Fatal(err)
182+
}
183+
184+
io.ReadFull(file, d[:])
185+
data = append(data, d)
186+
}
187+
188+
b.ResetTimer()
189+
for n := 0; n < b.N; n++ {
190+
Detect(data[n%fLen][:])
191+
}
192+
}

node.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import "fmt"
44

55
type (
66
// Node represents a node in the matchers tree structure.
7-
// It holds the mime type, the extension and the function to check whether
8-
// a byte slice has the mime type
7+
// It holds the mime type, the extension and the function
8+
// to check whether a byte slice has the mime type
99
Node struct {
1010
mime string
1111
extension string

testdata/a.jpg

9.88 KB
Loading

tree.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package mimetype
33
import "github.com/gabriel-vasile/mimetype/matchers"
44

55
// Root is a matcher which passes for any slice of bytes.
6-
// When a matcher passes the check, the children matchers are tried in order to
7-
// find a more accurate mime type.
6+
// When a matcher passes the check, the children matchers
7+
// are tried in order to find a more accurate mime type.
88
var Root = NewNode("application/octet-stream", "", matchers.True,
99
SevenZ, Zip, Pdf, Doc, Xls, Ppt, Ps, Psd, Ogg,
1010
Png, Jpg, Gif, Webp, Tiff, Bmp, Ico,

0 commit comments

Comments
 (0)