From b1270b3ae501c136d1999fa0bacc87617ffe7c40 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 17 May 2021 12:00:10 +0300 Subject: [PATCH 1/2] Preload method with benchmarks 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. --- licensedb/internal/investigation.go | 5 ++++ licensedb/licensedb.go | 5 ++++ licensedb/licensedb_test.go | 45 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 licensedb/licensedb_test.go diff --git a/licensedb/internal/investigation.go b/licensedb/internal/investigation.go index bd3c924..f2c9307 100644 --- a/licensedb/internal/investigation.go +++ b/licensedb/internal/investigation.go @@ -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() +} diff --git a/licensedb/licensedb.go b/licensedb/licensedb.go index 55670ce..520a305 100644 --- a/licensedb/licensedb.go +++ b/licensedb/licensedb.go @@ -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() +} diff --git a/licensedb/licensedb_test.go b/licensedb/licensedb_test.go new file mode 100644 index 0000000..a8a8295 --- /dev/null +++ b/licensedb/licensedb_test.go @@ -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 +} From 6403a23e656acb8434697c4bdcd4720ec2e209a1 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 6 Jul 2021 16:38:09 +0300 Subject: [PATCH 2/2] Described preload API doc --- licensedb/licensedb.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/licensedb/licensedb.go b/licensedb/licensedb.go index 520a305..27fc3f8 100644 --- a/licensedb/licensedb.go +++ b/licensedb/licensedb.go @@ -54,7 +54,12 @@ func Detect(fs filer.Filer) (map[string]api.Match, error) { return licenses, nil } -// Preload database with licenses +// Preload database with licenses - load internal database from assets into memory. +// This method is an optimization for cases when the `Detect` method should return fast, +// e.g. in HTTP web servers where connection timeout can occur during detect +// `Preload` method could be called before server startup. +// This method os optional and it's not required to be called, other APIs loads license database +// lazily on first invocation. func Preload() { internal.Preload() }