Skip to content

Commit

Permalink
Preload method with benchmarks
Browse files Browse the repository at this point in the history
Fixes #13 - added preload method to initialize database without
performing any license check. Added two benchmarks to compare
the difference between calls with and without preload.
  • Loading branch information
g4s8 committed May 17, 2021
1 parent 29d8148 commit b1270b3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
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
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
}

0 comments on commit b1270b3

Please sign in to comment.