forked from ua-parser/uap-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
97 lines (93 loc) · 2.35 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"bufio"
"fmt"
"github.com/ua-parser/uap-go/uaparser"
"os"
"strconv"
"sync"
"time"
)
func main() {
if len(os.Args) < 3 {
fmt.Printf("Usage: %s [old|new|both] [concurrency level]\n", os.Args[0])
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)
for i := 0; i < cLevel; i++ {
wg.Add(1)
go runTest(uaParser, i, &wg)
}
wg.Wait()
return
case "old":
fmt.Println("Running old version of uap...")
uaParser, _ := uaparser.New("./uap-core/regexes.yaml")
for i := 0; i < cLevel; i++ {
wg.Add(1)
go runTest(uaParser, i, &wg)
}
wg.Wait()
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)
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")
for i := 0; i < cLevel; i++ {
wg.Add(1)
runTest(uaParser, i, &wg)
}
wg.Wait()
return
default:
fmt.Printf("Usage: %s [old|new|both]\n", os.Args[0])
return
}
}
func runTest(uaParser *uaparser.Parser, id int, wg *sync.WaitGroup) {
file, err := os.Open("./uas")
if err != nil {
fmt.Printf("Failed to open ./wrappers file. Error: %s\n", err.Error())
return
}
defer file.Close()
line := 0
delim := ""
for i := 0; i < id*2; i++ {
delim += "\t"
}
totalLines := countLines()
var totalTime time.Duration
scanner := bufio.NewScanner(file)
for scanner.Scan() {
str := scanner.Text()
start := time.Now()
_ = uaParser.Parse(str)
elapsed := time.Since(start)
totalTime += elapsed
line++
fmt.Printf("\r\t\t\t\t%s%.2f%% completed|", delim, float64(line*100)/float64(totalLines))
}
fmt.Printf("\nProcessed lines: %d. Test took %s, average took %.3f ms\n", line, totalTime, float64(totalTime.Nanoseconds()/1e6)/(float64(line)))
wg.Done()
}
func countLines() int {
file, _ := os.Open("./uas")
fileScanner := bufio.NewScanner(file)
defer file.Close()
lineCount := 0
for fileScanner.Scan() {
lineCount++
}
return lineCount
}