Created
September 6, 2013 21:57
-
-
Save DmitryOlshansky/6470498 to your computer and use it in GitHub Desktop.
A simple benchmark where std.regex performs quite poorly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std.regex; | |
| import core.stdc.stdio; | |
| import core.stdc.string; | |
| import std.string; | |
| import std.stdio : writefln, writef; | |
| /* | |
| Parses mail logs like this line: | |
| Nov 1 19:37:59 themailhost sendmail[10620]: [ID 801593 mail.info] pA20bx2u010620: | |
| from=<some@one.com>, size=6988, class=0, nrcpts=2, | |
| msgid=<5693296.131.1320194279221.JavaMail.gfish@somemachine>, proto=ESMTP, | |
| daemon=MTA, relay=somehost.domain.com [1.2.3.4] | |
| Looks for the relay lines and stores the number of times a | |
| particular relay appears. | |
| Only relays matching foo.com and bar.corp are stored. | |
| At the end the list of relays is displayed along with | |
| the number of times that relay appeared in the log. | |
| */ | |
| void main (string[] args) | |
| { | |
| int[string] relayHosts; | |
| auto regex = ctRegex!(`relay=([\w-.]+[\w]+)[.,]*\s`); | |
| //auto regex = regex(r"relay=([0-9a-zA-Z\-\.]+[0-9a-zA-Z]+)[\.\,]*\s"); | |
| foreach (arg; args[1 .. args.length]) | |
| { | |
| char[4096] buf = void; | |
| //auto file = new BufferedFile(arg, FileMode.In, 268_435_456); // 250 MB | |
| auto file = fopen(toStringz(arg), "r".ptr); | |
| if(!file) | |
| continue; | |
| scope(exit) fclose(file); | |
| while(fgets(buf.ptr, buf.sizeof, file) != null) | |
| { | |
| auto line = buf[0..strlen(buf.ptr)]; | |
| // Find the relay portion of the string (if any) | |
| auto cap = matchFirst(line, regex); | |
| if(!cap.empty) { | |
| auto hostName = cap[1]; | |
| if(endsWith(hostName, "foo.com") || | |
| endsWith(hostName, "bar.corp")) | |
| { | |
| if(cast(string)hostName in relayHosts) | |
| relayHosts[cast(string)hostName]++; | |
| else | |
| relayHosts[hostName.idup] = 1; | |
| } | |
| } | |
| } | |
| } | |
| /* Display the results */ | |
| writefln("Host, Number of relayed emails"); | |
| foreach(relay ; relayHosts.keys.sort) | |
| { | |
| writef("%s,%d\n", relay, relayHosts[relay]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment