|
D Paste by downs
Description: geobench.d optimized
|
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 55 56 57 58 59 60 61 | 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() { string[string] countries; // lookup is by string equality "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]; if (auto p = e.country in countries) e.country = *p; else countries[e.country] = e.country; list ~= e; // yes they're ordered }; logln(list.length, " ranges added, using ", countries.values.length, " countries. "); } } import tools.mersenne; void main() { long count; auto last = sec(); while (true) { string country; for (int i = 0; i < 16; ++i) { auto rand_ip = rand(); 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)