|
D Paste by downs
Description: geobench.d
|
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 | module geobench; import tools.time, tools.log, tools.base, std.string, std.socket; class GeoIP { import std.file; static { struct entry { uint start; string country; } entry[] list; string lookup(uint what) { auto range = list; while (range.length > 1) { auto center = range[$/2]; if (center.start == what) return center.country; if (center.start < what) range = range[$/2 .. $]; else range = range[0 .. $/2]; } return range[0].country; } } static this() { "GeoIPCountryWhois.csv".read().castLike("").split("\n") /map/ (string line) { line = line.strip(); if (!line.length) return; auto parts = line[1 .. $-1].split("\",\""); // remove ""s auto ip = parts[0]; entry e; // why parse IPs? Let the system do it for us. e.start = (new InternetAddress(ip, 0)).addr(); e.country = parts[$-1]; list ~= e; // yes they're ordered }; } } import tools.mersenne; void main() { long count; auto last = sec(); while (true) { auto rand_ip = rand(); auto country = GeoIP.lookup(rand_ip); count ++; auto s = sec(); if (s > last + 1) { last = s; logln(count, " lookups per second, last country was ", country); count = 0; } } } |
(some replies deleted)