|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe purpose of this article is to show how to create a more specialized The web control uses a database from MaxMind, specifically its GeoIP Free Database. MaxMind also provide a C# class to access its database. It's a very simple control, and the bulk of the work is performed by the ready-prepared As ever, a live demo is available on my website. How it worksBroadly, the After the countries have been loaded a lookup is performed against the IP database to determine the location of the current visitor, this is then selected in the drop down list box - allowing users to correct it if it's wrong. Aside from that it behaves as any other Now the technical details behind MaxMind's GeoIP database (not strictly necessary to understand since the C# class is provided by MaxMind). MaxMind store IP address ranges, each of these records is also noted against a country. For example (in CSV format), Implementation detailsThe control is relatively simple in design, the majority of the work is performed in the OnInitBelow is part of the code for the // Check to see if the application cache should be used
if (useAppCache)
{
// Check to see whether the IP Database is
// already in the Application Cache
if (Context.Cache.Get("GeoIPData") == null)
// No, so store it as well as setting a dependency on the file
Context.Cache.Insert("GeoIPData",
CountryLookup.FileToMemory(
ConfigurationSettings.AppSettings["GeoDatFile"]),
new CacheDependency
(ConfigurationSettings.AppSettings["GeoDatFile"]));
The // Load the countries from the ASCII file into the control
LoadCountries();
// Perform the lookup using the MemoryStream taken from the Cache
CountryLookup cl = new CountryLookup(
((MemoryStream)Context.Cache.Get("GeoIPData"))
);
// What country is the visitor from?
string visitorCountry = cl.lookupCountryName(
this.Page.Request.ServerVariables["REMOTE_ADDR"]
);
// Select the country in the control
this.SelectedIndex = this.Items.IndexOf(
new ListItem(visitorCountry,visitorCountry)
);
}
CountryLookup changesThe Deploying the demoWithin the demo is a test Web Form, the GeoIP database, the Countries text file and the You'll have to do a few basic things to add the control to your page,
PerformanceI performed some limited testing on the control to see how it performed when under load. I use Windows XP Professional as my development platform which has a restricted version of IIS - limited to 10 simultaneous connections. I used Microsoft Application Center Test to perform the testing, which involved loading the demonstration page as many times as possible over 5 minutes. Non-cached version resultsThe graph below shows the results of the test using the standard control (without any caching). Each time a page is requested the GeoIP Database file is loaded and searched, the countries are also loaded from the text file. Below the graph are some basic statistics that were recorded during the test.
Average requests per second: 32.47
Average time to first byte (msecs): 279.39
Average time to last byte (msecs): 279.55
Response Code: 403 - The server understood the request, but is refusing
to fulfill it.
Count: 5,429
Percent (%): 55.73
Response Code: 200 - The request completed successfully.
Count: 4,313
Percent (%): 44.27
Since my local machine is not really designed to be a server (it's an Athlon XP 2000+ based machine, with 512MB of RAM but without any SCSI hard disks) it's fair to assume that a dedicated server would perform better. Despite this, the server could only sustain an average of 32 requests a second. Cached resultsTo improve performance I used ASP.NET's Application Cache to store both the GeoIP Database and the countries. The option to use both of these can be set through the |
||||||||||||||||||||||
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 11 Jan 2003 Editor: Smitha Vijayan |
Copyright 2002 by Paul Ingles Everything else Copyright © CodeProject, 1999-2008 Web13 | Advertise on the Code Project |