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

Preload method with benchmarks #14

Merged
merged 2 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions licensedb/internal/investigation.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,8 @@ func InvestigateReadmeText(text []byte, fs filer.Filer) map[string]float32 {
func IsLicenseDirectory(fileName string) bool {
return licenseDirectoryRe.MatchString(strings.ToLower(fileName))
}

// Preload license database
func Preload() {
_ = globalLicenseDatabase()
}
5 changes: 5 additions & 0 deletions licensedb/licensedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ func Detect(fs filer.Filer) (map[string]api.Match, error) {
}
return licenses, nil
}

// Preload database with licenses
Copy link
Member

Choose a reason for hiding this comment

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

As it extends the API surface, IMO it would be nice to add a few words here to clarify the contract - that it is an optimization and the client does need to call this first, as the rest of the APIs are doing that lazily anyway, or some words to that effect.

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will fix that

func Preload() {
internal.Preload()
}
45 changes: 45 additions & 0 deletions licensedb/licensedb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package licensedb

import (
"os"
"path/filepath"
"testing"

"github.com/go-enry/go-license-detector/v4/licensedb/filer"
)

func BenchmarkDetect(b *testing.B) {
f := pwdFiler()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Detect(f)
if err != nil {
panic(err)
}
}
}

func BenchmarkDetectWithPreload(b *testing.B) {
f := pwdFiler()
Preload()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Detect(f)
if err != nil {
panic(err)
}
}
}

func pwdFiler() filer.Filer {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
root := filepath.Dir(pwd)
f, err := filer.FromDirectory(root)
if err != nil {
panic(err)
}
return f
}