public void InnerDataSourceSearch()
        {
            Helper.PrintMem();

            //预热
            IpLocationSearch.Find("14.123.238.167");

            uint ip = IpLocationHelper.IPv4ToInteger("116.19.114.15");

            Helper.Time("单条查询", () =>
            {
                IpLocationSearch.Find(ip);
            });

            int count = 20000000;

            Helper.Time("批量查询", () =>
            {
                IpLocationSearch.Find(ip);
            }, count);

            Helper.TimeWithParallel("并发查询", () =>
            {
                IpLocationSearch.Find(ip);
            }, count);

            Helper.TimeWithThread("多线程查询", () =>
            {
                IpLocationSearch.Find(ip);
            }, 4, count);
        }
        public void StreamDataSourceSearch()
        {
            Helper.PrintMem();

            var dest = Path.Combine(AppContext.BaseDirectory, "ip.dat");

            if (!File.Exists(dest))
            {
                var filename = Path.Combine(AppContext.BaseDirectory, "ip.txt");

                using (var fs = File.Create(dest))
                {
                    IpSearcherFactory.Generate(Helper.GetIPList(filename).ToArray(), fs);
                }
            }

            var dataSource = new StreamDataSource(dest);

            var searcher = IpSearcherFactory.GetSearcher(dataSource);

            //预热
            searcher.Search(IpLocationHelper.IPv4ToInteger("14.123.238.167"));

            uint ip = IpLocationHelper.IPv4ToInteger("116.19.114.15");

            Helper.Time("单条查询", () =>
            {
                searcher.Search(ip);
            });

            int count = 20000;

            Helper.Time("批量查询", () =>
            {
                searcher.Search(ip);
            }, count);

            Helper.TimeWithParallel("并发查询", () =>
            {
                searcher.Search(ip);
            }, count);

            Helper.TimeWithThread("多线程查询", () =>
            {
                searcher.Search(ip);
            }, 4, count);
        }
Example #3
0
        public void Writer_And_Reader()
        {
            var filename = Path.Combine(AppContext.BaseDirectory, "ip.txt");
            var dest     = Path.Combine(AppContext.BaseDirectory, "ip.dat");

            using (var fs = File.Create(dest))
            {
                IpSearcherFactory.Generate(Helper.GetIPList(filename).ToArray(), fs);
            }

            var dataSource = new StreamDataSource(dest);

            var searcher = IpSearcherFactory.GetSearcher(dataSource);

            var result = searcher.Search(IpLocationHelper.IPv4ToInteger("14.123.238.167"));

            Assert.NotNull(result);
            Assert.Equal("广东省", result.Province);

            dataSource.Dispose();
        }
Example #4
0
        public static IList <IpEntity> GetIPList(string filename)
        {
            var list = new List <IpEntity>();

            foreach (var line in File.ReadLines(filename))
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var seg = line.Split('|');

                var start = seg[0].Split('.');
                var end   = seg[1].Split('.');

                var startB = start.Select(s => byte.Parse(s)).ToArray();
                var endB   = end.Select(s => byte.Parse(s)).ToArray();

                IpEntity info = new IpEntity();

                info.Prefix   = startB[0];
                info.StartIP  = seg[0];
                info.EndIP    = seg[1];
                info.Country  = seg[2] == "0" ? string.Empty : seg[2];
                info.Province = seg[3] == "0" ? string.Empty : seg[3];
                info.City     = seg[4] == "0" ? string.Empty : seg[4];
                info.Isp      = seg[5] == "0" ? string.Empty : seg[5];

                info.MinIP = IpLocationHelper.IPv4ToInteger(startB);
                info.MaxIP = IpLocationHelper.IPv4ToInteger(endB);

                list.Add(info);
            }

            return(list);
        }