-
Notifications
You must be signed in to change notification settings - Fork 39
/
performance_test.go
114 lines (92 loc) · 2.36 KB
/
performance_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package gubrak
import (
"github.com/DefinitelyMod/gocsv"
// "github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
"time"
)
var (
dataCSV = make([]Data, 0)
tldSearch = "com"
)
type Data struct {
GlobalRank string `csv:"GlobalRank"`
TldRank string `csv:"TldRank"`
Domain string `csv:"Domain"`
TLD string `csv:"TLD"`
RefSubNets string `csv:"RefSubNets"`
RefIPs string `csv:"RefIPs"`
IDNDomain string `csv:"IDN_Domain"`
IDNTLD string `csv:"IDN_TLD"`
PrevGlobalRank string `csv:"PrevGlobalRank"`
PrevTldRank string `csv:"PrevTldRank"`
PrevRefSubNets string `csv:"PrevRefSubNets"`
PrevRefIPs string `csv:"PrevRefIPs"`
}
func TimeBenchmarker(t *testing.T, start time.Time) {
duration := time.Since(start)
t.Log("required time to completed:", duration.Seconds())
}
func TestLoadData(t *testing.T) {
defer TimeBenchmarker(t, time.Now())
basePath, _ := os.Getwd()
fileLocation := filepath.Join(basePath, "majestic_million.csv")
if _, err := os.Stat(fileLocation); err != nil {
if os.IsNotExist(err) {
t.Fatal("To perform performance test, you have to download sample csv data from https://blog.majestic.com/development/majestic-million-csv-daily/")
return
}
}
f, err := os.OpenFile(fileLocation, os.O_RDWR|os.O_CREATE, os.ModePerm)
if f != nil {
defer f.Close()
}
if err != nil {
t.Fatal("error", err.Error())
return
}
rows := make([]Data, 0)
err = gocsv.UnmarshalFile(f, &rows)
if err != nil {
t.Fatal("error", err.Error())
return
}
dataCSV = rows
t.Log("Loading csv file done. Total", len(dataCSV), "data found")
}
func TestPerformanceFilterUsingForRange(t *testing.T) {
if len(dataCSV) == 0 {
t.Skip()
return
}
time.Sleep(time.Second * 2)
defer TimeBenchmarker(t, time.Now())
result := make([]Data, 0)
for _, row := range dataCSV {
if row.TLD == tldSearch {
result = append(result, row)
}
}
t.Log("found", len(result))
}
func TestPerformanceFilterUsingOurLibrary(t *testing.T) {
if len(dataCSV) == 0 {
t.Skip()
return
}
time.Sleep(time.Second * 2)
defer TimeBenchmarker(t, time.Now())
result, err := From(dataCSV).
Filter(func(row Data) bool {
return row.TLD == tldSearch
}).
ResultAndError()
if err != nil {
t.Fatal("error", err.Error())
return
}
resultParsed := result.([]Data)
t.Log("found", len(resultParsed))
}