Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package geodata_test
 
import (
    "errors"
    "io/fs"
    "os"
    "path/filepath"
    "runtime"
    "testing"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/platform/filesystem"
    "github.com/v2fly/v2ray-core/v5/infra/conf/geodata"
    _ "github.com/v2fly/v2ray-core/v5/infra/conf/geodata/memconservative"
    _ "github.com/v2fly/v2ray-core/v5/infra/conf/geodata/standard"
)
 
func init() {
    const (
        geoipURL   = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
        geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
    )
 
    wd, err := os.Getwd()
    common.Must(err)
 
    tempPath := filepath.Join(wd, "..", "..", "..", "testing", "temp")
    geoipPath := filepath.Join(tempPath, "geoip.dat")
    geositePath := filepath.Join(tempPath, "geosite.dat")
 
    os.Setenv("v2ray.location.asset", tempPath)
 
    if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
        common.Must(os.MkdirAll(tempPath, 0o755))
        geoipBytes, err := common.FetchHTTPContent(geoipURL)
        common.Must(err)
        common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
    }
    if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) {
        common.Must(os.MkdirAll(tempPath, 0o755))
        geositeBytes, err := common.FetchHTTPContent(geositeURL)
        common.Must(err)
        common.Must(filesystem.WriteFile(geositePath, geositeBytes))
    }
}
 
func BenchmarkStandardLoaderGeoIP(b *testing.B) {
    standardLoader, err := geodata.GetGeoDataLoader("standard")
    common.Must(err)
 
    m1 := runtime.MemStats{}
    m2 := runtime.MemStats{}
    runtime.ReadMemStats(&m1)
    standardLoader.LoadGeoIP("cn")
    standardLoader.LoadGeoIP("us")
    standardLoader.LoadGeoIP("private")
    runtime.ReadMemStats(&m2)
 
    b.ReportMetric(float64(m2.Alloc-m1.Alloc)/1024/1024, "MiB(GeoIP-Alloc)")
    b.ReportMetric(float64(m2.TotalAlloc-m1.TotalAlloc)/1024/1024, "MiB(GeoIP-TotalAlloc)")
}
 
func BenchmarkStandardLoaderGeoSite(b *testing.B) {
    standardLoader, err := geodata.GetGeoDataLoader("standard")
    common.Must(err)
 
    m3 := runtime.MemStats{}
    m4 := runtime.MemStats{}
    runtime.ReadMemStats(&m3)
    standardLoader.LoadGeoSite("cn")
    standardLoader.LoadGeoSite("geolocation-!cn")
    standardLoader.LoadGeoSite("private")
    runtime.ReadMemStats(&m4)
 
    b.ReportMetric(float64(m4.Alloc-m3.Alloc)/1024/1024, "MiB(GeoSite-Alloc)")
    b.ReportMetric(float64(m4.TotalAlloc-m3.TotalAlloc)/1024/1024, "MiB(GeoSite-TotalAlloc)")
}
 
func BenchmarkMemconservativeLoaderGeoIP(b *testing.B) {
    standardLoader, err := geodata.GetGeoDataLoader("memconservative")
    common.Must(err)
 
    m1 := runtime.MemStats{}
    m2 := runtime.MemStats{}
    runtime.ReadMemStats(&m1)
    standardLoader.LoadGeoIP("cn")
    standardLoader.LoadGeoIP("us")
    standardLoader.LoadGeoIP("private")
    runtime.ReadMemStats(&m2)
 
    b.ReportMetric(float64(m2.Alloc-m1.Alloc)/1024, "KiB(GeoIP-Alloc)")
    b.ReportMetric(float64(m2.TotalAlloc-m1.TotalAlloc)/1024/1024, "MiB(GeoIP-TotalAlloc)")
}
 
func BenchmarkMemconservativeLoaderGeoSite(b *testing.B) {
    standardLoader, err := geodata.GetGeoDataLoader("memconservative")
    common.Must(err)
 
    m3 := runtime.MemStats{}
    m4 := runtime.MemStats{}
    runtime.ReadMemStats(&m3)
    standardLoader.LoadGeoSite("cn")
    standardLoader.LoadGeoSite("geolocation-!cn")
    standardLoader.LoadGeoSite("private")
    runtime.ReadMemStats(&m4)
 
    b.ReportMetric(float64(m4.Alloc-m3.Alloc)/1024, "KiB(GeoSite-Alloc)")
    b.ReportMetric(float64(m4.TotalAlloc-m3.TotalAlloc)/1024/1024, "MiB(GeoSite-TotalAlloc)")
}
 
func BenchmarkAllLoader(b *testing.B) {
    type testingProfileForLoader struct {
        name string
    }
    testCase := []testingProfileForLoader{
        {"standard"},
        {"memconservative"},
    }
    for _, v := range testCase {
        b.Run(v.name, func(b *testing.B) {
            b.Run("Geosite", func(b *testing.B) {
                loader, err := geodata.GetGeoDataLoader(v.name)
                if err != nil {
                    b.Fatal(err)
                }
 
                m3 := runtime.MemStats{}
                m4 := runtime.MemStats{}
                runtime.ReadMemStats(&m3)
                loader.LoadGeoSite("cn")
                loader.LoadGeoSite("geolocation-!cn")
                loader.LoadGeoSite("private")
                runtime.ReadMemStats(&m4)
 
                b.ReportMetric(float64(m4.Alloc-m3.Alloc)/1024, "KiB(GeoSite-Alloc)")
                b.ReportMetric(float64(m4.TotalAlloc-m3.TotalAlloc)/1024/1024, "MiB(GeoSite-TotalAlloc)")
            })
 
            b.Run("GeoIP", func(b *testing.B) {
                loader, err := geodata.GetGeoDataLoader(v.name)
                if err != nil {
                    b.Fatal(err)
                }
 
                m1 := runtime.MemStats{}
                m2 := runtime.MemStats{}
                runtime.ReadMemStats(&m1)
                loader.LoadGeoIP("cn")
                loader.LoadGeoIP("us")
                loader.LoadGeoIP("private")
                runtime.ReadMemStats(&m2)
 
                b.ReportMetric(float64(m2.Alloc-m1.Alloc)/1024/1024, "MiB(GeoIP-Alloc)")
                b.ReportMetric(float64(m2.TotalAlloc-m1.TotalAlloc)/1024/1024, "MiB(GeoIP-TotalAlloc)")
            })
        })
    }
}