Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,29 @@ package main
import (
"fmt"
"log"
"os"

"github.com/ua-parser/uap-go/uaparser"
"gopkg.in/yaml.v3"
)

func main() {
uagent := "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true"

parser, err := uaparser.New("./regexes.yaml")

regexes, err := os.ReadFile("./regexes.yaml")
if err != nil {
log.Fatal(err)
}

var def *uaparser.RegexesDefinitions

if err := yaml.Unmarshal(regexes, def); err != nil {
fmt.Printf("error parsing regexes definitions. Error: %s\n", err.Error())
return
}

parser, err := uaparser.New(uaparser.WithRegexesDefinitions(def))
if err != nil {
log.Fatal(err)
}
Expand Down
39 changes: 35 additions & 4 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,42 @@ import (
"time"

"github.com/ua-parser/uap-go/uaparser"
"gopkg.in/yaml.v3"
)

func main() {
if len(os.Args) < 3 {
fmt.Printf("Usage: %s [old|new|both] [concurrency level]\n", os.Args[0])
return
}

regexes, err := os.ReadFile("./uap-core/regexes.yaml")
if err != nil {
fmt.Printf("Failed to read regexes file. Error: %s\n", err.Error())
return
}

var def *uaparser.RegexesDefinitions

if err := yaml.Unmarshal(regexes, def); err != nil {
fmt.Printf("error parsing regexes definitions. Error: %s\n", err.Error())
return
}

var wg sync.WaitGroup
cLevel, _ := strconv.Atoi(os.Args[2])
switch os.Args[1] {
case "new":
fmt.Println("Running new version of uap...")
uaParser, _ := uaparser.NewWithOptions("./uap-core/regexes.yaml", (uaparser.EOsLookUpMode | uaparser.EUserAgentLookUpMode), 100, 20, true, true, 1024)
uaParser, _ := uaparser.New(
uaparser.WithRegexesDefinitions(def),
uaparser.WithMode(uaparser.EOsLookUpMode|uaparser.EUserAgentLookUpMode),
uaparser.WithMissesThreshold(100),
uaparser.WithMatchIdxNotOk(20),
uaparser.WithSort(true),
uaparser.WithDebug(true),
uaparser.WithCacheSize(1024),
)
for i := 0; i < cLevel; i++ {
wg.Add(1)
go runTest(uaParser, i, &wg)
Expand All @@ -30,7 +53,7 @@ func main() {
return
case "old":
fmt.Println("Running old version of uap...")
uaParser, _ := uaparser.New("./uap-core/regexes.yaml")
uaParser, _ := uaparser.New(uaparser.WithRegexesDefinitions(def))
for i := 0; i < cLevel; i++ {
wg.Add(1)
go runTest(uaParser, i, &wg)
Expand All @@ -39,13 +62,21 @@ func main() {
return
case "both":
fmt.Println("Running new version of uap...")
uaParser, _ := uaparser.NewWithOptions("./uap-core/regexes.yaml", (uaparser.EOsLookUpMode | uaparser.EUserAgentLookUpMode), 100, 20, true, true, 1024)
uaParser, _ := uaparser.New(
uaparser.WithRegexesDefinitions(def),
uaparser.WithMode(uaparser.EOsLookUpMode|uaparser.EUserAgentLookUpMode),
uaparser.WithMissesThreshold(100),
uaparser.WithMatchIdxNotOk(20),
uaparser.WithSort(true),
uaparser.WithDebug(true),
uaparser.WithCacheSize(1024),
)
for i := 0; i < cLevel; i++ {
wg.Add(1)
runTest(uaParser, i, &wg)
}
fmt.Println("Running old version of uap...")
uaParser, _ = uaparser.New("./uap-core/regexes.yaml")
uaParser, _ = uaparser.New(uaparser.WithRegexesDefinitions(def))
for i := 0; i < cLevel; i++ {
wg.Add(1)
runTest(uaParser, i, &wg)
Expand Down
41 changes: 30 additions & 11 deletions uaparser/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,25 @@ var largeUasSample []string

func init() {
var err error
benchedParser, err = New("../uap-core/regexes.yaml")

regexes, err := os.ReadFile("../uap-core/regexes.yaml")
if err != nil {
log.Fatal(err)
}

benchedParser, err = New(regexes)
if err != nil {
log.Fatal(err)
}
benchedParserWithOptions, err = NewWithOptions("../uap-core/regexes.yaml", (EOsLookUpMode | EUserAgentLookUpMode), 100, 20, true, true, cDefaultCacheSize)
benchedParserWithOptions, err = New(regexes,
WithMode(EOsLookUpMode|EUserAgentLookUpMode),
WithMissesThreshold(100),
WithMatchIdxNotOk(20),
WithSort(true),
WithDebug(true),
WithCacheSize(cDefaultCacheSize),
)

if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -48,17 +62,22 @@ func BenchmarkParserWithOptions(b *testing.B) {
}

func BenchmarkParserWithDifferentCacheSize(b *testing.B) {
sizes := []int{cDefaultCacheSize, cDefaultCacheSize*2, cDefaultCacheSize*3, cDefaultCacheSize*4}
sizes := []int{cDefaultCacheSize, cDefaultCacheSize * 2, cDefaultCacheSize * 3, cDefaultCacheSize * 4}

regexes, err := os.ReadFile("../uap-core/regexes.yaml")
if err != nil {
log.Fatal(err)
}

for _, size := range sizes {
parser, err := NewWithOptions(
"../uap-core/regexes.yaml",
EOsLookUpMode | EUserAgentLookUpMode | EDeviceLookUpMode,
cDefaultMissesTreshold,
cDefaultMatchIdxNotOk,
false,
false,
size)
parser, err := New(regexes,
WithMode(EOsLookUpMode|EUserAgentLookUpMode|EDeviceLookUpMode),
WithMissesThreshold(cDefaultMissesTreshold),
WithMatchIdxNotOk(cDefaultMatchIdxNotOk),
WithSort(false),
WithDebug(false),
WithCacheSize(size),
)
if err != nil {
log.Fatal(err)
}
Expand Down
45 changes: 45 additions & 0 deletions uaparser/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package uaparser

type Option func(*Parser)

func WithMode(mode LookupMode) Option {
return func(s *Parser) {
s.config.Mode = mode
}
}

func WithSort(useSort bool) Option {
return func(s *Parser) {
s.config.UseSort = useSort
}
}

func WithDebug(debug bool) Option {
return func(s *Parser) {
s.config.DebugMode = debug
}
}

func WithCacheSize(size int) Option {
return func(s *Parser) {
s.config.CacheSize = size
}
}

func WithMissesThreshold(threshold uint64) Option {
return func(s *Parser) {
s.config.MissesThreshold = threshold
}
}
Comment thread
jkroepke marked this conversation as resolved.
Outdated

func WithMatchIdxNotOk(idx int) Option {
return func(s *Parser) {
s.config.MatchIdxNotOk = idx
}
}

func WithRegexesDefinitions(def *RegexesDefinitions) Option {
return func(s *Parser) {
s.RegexesDefinitions = def
}
}
Loading
Loading