geodata_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package geodata_test
  2. import (
  3. "errors"
  4. "io/fs"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "testing"
  9. "github.com/v2fly/v2ray-core/v5/common"
  10. "github.com/v2fly/v2ray-core/v5/common/platform/filesystem"
  11. "github.com/v2fly/v2ray-core/v5/infra/conf/geodata"
  12. _ "github.com/v2fly/v2ray-core/v5/infra/conf/geodata/memconservative"
  13. _ "github.com/v2fly/v2ray-core/v5/infra/conf/geodata/standard"
  14. )
  15. func init() {
  16. const (
  17. geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
  18. geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
  19. )
  20. wd, err := os.Getwd()
  21. common.Must(err)
  22. tempPath := filepath.Join(wd, "..", "..", "..", "testing", "temp")
  23. geoipPath := filepath.Join(tempPath, "geoip.dat")
  24. geositePath := filepath.Join(tempPath, "geosite.dat")
  25. os.Setenv("v2ray.location.asset", tempPath)
  26. if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
  27. common.Must(os.MkdirAll(tempPath, 0o755))
  28. geoipBytes, err := common.FetchHTTPContent(geoipURL)
  29. common.Must(err)
  30. common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
  31. }
  32. if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) {
  33. common.Must(os.MkdirAll(tempPath, 0o755))
  34. geositeBytes, err := common.FetchHTTPContent(geositeURL)
  35. common.Must(err)
  36. common.Must(filesystem.WriteFile(geositePath, geositeBytes))
  37. }
  38. }
  39. func BenchmarkStandardLoaderGeoIP(b *testing.B) {
  40. standardLoader, err := geodata.GetGeoDataLoader("standard")
  41. common.Must(err)
  42. m1 := runtime.MemStats{}
  43. m2 := runtime.MemStats{}
  44. runtime.ReadMemStats(&m1)
  45. standardLoader.LoadGeoIP("cn")
  46. standardLoader.LoadGeoIP("us")
  47. standardLoader.LoadGeoIP("private")
  48. runtime.ReadMemStats(&m2)
  49. b.ReportMetric(float64(m2.Alloc-m1.Alloc)/1024/1024, "MiB(GeoIP-Alloc)")
  50. b.ReportMetric(float64(m2.TotalAlloc-m1.TotalAlloc)/1024/1024, "MiB(GeoIP-TotalAlloc)")
  51. }
  52. func BenchmarkStandardLoaderGeoSite(b *testing.B) {
  53. standardLoader, err := geodata.GetGeoDataLoader("standard")
  54. common.Must(err)
  55. m3 := runtime.MemStats{}
  56. m4 := runtime.MemStats{}
  57. runtime.ReadMemStats(&m3)
  58. standardLoader.LoadGeoSite("cn")
  59. standardLoader.LoadGeoSite("geolocation-!cn")
  60. standardLoader.LoadGeoSite("private")
  61. runtime.ReadMemStats(&m4)
  62. b.ReportMetric(float64(m4.Alloc-m3.Alloc)/1024/1024, "MiB(GeoSite-Alloc)")
  63. b.ReportMetric(float64(m4.TotalAlloc-m3.TotalAlloc)/1024/1024, "MiB(GeoSite-TotalAlloc)")
  64. }
  65. func BenchmarkMemconservativeLoaderGeoIP(b *testing.B) {
  66. standardLoader, err := geodata.GetGeoDataLoader("memconservative")
  67. common.Must(err)
  68. m1 := runtime.MemStats{}
  69. m2 := runtime.MemStats{}
  70. runtime.ReadMemStats(&m1)
  71. standardLoader.LoadGeoIP("cn")
  72. standardLoader.LoadGeoIP("us")
  73. standardLoader.LoadGeoIP("private")
  74. runtime.ReadMemStats(&m2)
  75. b.ReportMetric(float64(m2.Alloc-m1.Alloc)/1024, "KiB(GeoIP-Alloc)")
  76. b.ReportMetric(float64(m2.TotalAlloc-m1.TotalAlloc)/1024/1024, "MiB(GeoIP-TotalAlloc)")
  77. }
  78. func BenchmarkMemconservativeLoaderGeoSite(b *testing.B) {
  79. standardLoader, err := geodata.GetGeoDataLoader("memconservative")
  80. common.Must(err)
  81. m3 := runtime.MemStats{}
  82. m4 := runtime.MemStats{}
  83. runtime.ReadMemStats(&m3)
  84. standardLoader.LoadGeoSite("cn")
  85. standardLoader.LoadGeoSite("geolocation-!cn")
  86. standardLoader.LoadGeoSite("private")
  87. runtime.ReadMemStats(&m4)
  88. b.ReportMetric(float64(m4.Alloc-m3.Alloc)/1024, "KiB(GeoSite-Alloc)")
  89. b.ReportMetric(float64(m4.TotalAlloc-m3.TotalAlloc)/1024/1024, "MiB(GeoSite-TotalAlloc)")
  90. }
  91. func BenchmarkAllLoader(b *testing.B) {
  92. type testingProfileForLoader struct {
  93. name string
  94. }
  95. testCase := []testingProfileForLoader{
  96. {"standard"},
  97. {"memconservative"},
  98. }
  99. for _, v := range testCase {
  100. b.Run(v.name, func(b *testing.B) {
  101. b.Run("Geosite", func(b *testing.B) {
  102. loader, err := geodata.GetGeoDataLoader(v.name)
  103. if err != nil {
  104. b.Fatal(err)
  105. }
  106. m3 := runtime.MemStats{}
  107. m4 := runtime.MemStats{}
  108. runtime.ReadMemStats(&m3)
  109. loader.LoadGeoSite("cn")
  110. loader.LoadGeoSite("geolocation-!cn")
  111. loader.LoadGeoSite("private")
  112. runtime.ReadMemStats(&m4)
  113. b.ReportMetric(float64(m4.Alloc-m3.Alloc)/1024, "KiB(GeoSite-Alloc)")
  114. b.ReportMetric(float64(m4.TotalAlloc-m3.TotalAlloc)/1024/1024, "MiB(GeoSite-TotalAlloc)")
  115. })
  116. b.Run("GeoIP", func(b *testing.B) {
  117. loader, err := geodata.GetGeoDataLoader(v.name)
  118. if err != nil {
  119. b.Fatal(err)
  120. }
  121. m1 := runtime.MemStats{}
  122. m2 := runtime.MemStats{}
  123. runtime.ReadMemStats(&m1)
  124. loader.LoadGeoIP("cn")
  125. loader.LoadGeoIP("us")
  126. loader.LoadGeoIP("private")
  127. runtime.ReadMemStats(&m2)
  128. b.ReportMetric(float64(m2.Alloc-m1.Alloc)/1024/1024, "MiB(GeoIP-Alloc)")
  129. b.ReportMetric(float64(m2.TotalAlloc-m1.TotalAlloc)/1024/1024, "MiB(GeoIP-TotalAlloc)")
  130. })
  131. })
  132. }
  133. }