github.com/imannamdari/v2ray-core/v5@v5.0.5/infra/conf/geodata/geodata_test.go (about)

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