Created
April 2, 2013 08:07
-
-
Save nimolix/358c35caf214975a7e39 to your computer and use it in GitHub Desktop.
Parallel DNS lookup comparison between Ruby and Go
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
aol.com | |
anonymous.to | |
comcast.net | |
dispostable.com | |
everymail.net | |
everyone.net | |
flashmail.com | |
gmail.com | |
googlemail.com | |
guerillamail.com | |
hotmail.com | |
hotmail.fr | |
hotmail.it | |
inbox.com | |
live.com | |
mail.com | |
mailinator.com | |
msn.com | |
onebox.com | |
outlook.com | |
qmail.com | |
rediff.com | |
spamgourmet.com | |
trashmail.net | |
yahoo.com | |
ymail.com |
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
$ go build parallel_dns_lookup.go | |
$ sudo killall -HUP mDNSResponder # On mountain lion to flush DNS cache | |
$ time ./parallel_dns_lookup | |
Setting GOMAXPROCS to 8 | |
googlemail.com => 173.194.45.53 | |
hotmail.fr => 94.245.116.9 | |
flashmail.com => 207.55.16.224 | |
hotmail.com => 65.55.72.183 | |
dispostable.com => 80.94.76.10 | |
everymail.net => 212.69.187.37 | |
guerillamail.com => 72.52.4.90 | |
mail.com => 213.165.66.221 | |
aol.com => 64.12.89.186 | |
gmail.com => 173.194.45.54 | |
comcast.net => 162.150.0.50 | |
inbox.com => 64.135.77.80 | |
mailinator.com => 72.51.33.80 | |
yahoo.com => 206.190.36.45 | |
ymail.com => 98.139.102.145 | |
onebox.com => 204.11.168.221 | |
live.com => 65.55.206.154 | |
anonymous.to => 74.53.27.99 | |
rediff.com => 92.123.65.153 | |
hotmail.it => 94.245.116.11 | |
trashmail.net => 88.198.11.51 | |
msn.com => 65.55.206.228 | |
outlook.com => 157.56.238.43 | |
spamgourmet.com => 216.75.62.102 | |
everyone.net => 131.103.75.164 | |
qmail.com => 113.108.11.220 | |
real 0m0.603s | |
user 0m0.007s | |
sys 0m0.015s |
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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net" | |
"runtime" | |
"strings" | |
) | |
type DomainType struct { | |
Domain string | |
IpAddress string | |
ErrorMsg string | |
} | |
func getDomains() []string { | |
content, err := ioutil.ReadFile("domains.txt") | |
if err != nil { | |
log.Fatal(err) | |
} | |
domains := string(content) | |
return strings.Split(strings.TrimSpace(domains), "\n") | |
} | |
func lookupDomain(communicateChannel chan DomainType, domain string) { | |
rawIpAddresses, err := net.LookupIP(domain) | |
errorMsg := "" | |
if err != nil { | |
errorMsg = err.Error() | |
} | |
// If there are multiple A records, grab only the first one. | |
ipAddress := "" | |
if len(rawIpAddresses) > 0 { | |
ipAddress = rawIpAddresses[0].String() | |
} | |
communicateChannel <- DomainType{domain, ipAddress, errorMsg} | |
} | |
func checkAllDomains(communicateChannel chan DomainType, numberOfDomains int) { | |
returnedCount := 0 | |
for { | |
domainTypeInstance := <-communicateChannel | |
result := "" | |
if domainTypeInstance.ErrorMsg != "" { | |
result = domainTypeInstance.ErrorMsg | |
} else { | |
result = domainTypeInstance.IpAddress | |
} | |
fmt.Printf("%s => %s\n", domainTypeInstance.Domain, result) | |
returnedCount++ | |
if returnedCount >= numberOfDomains { | |
break | |
} | |
} | |
return | |
} | |
func main() { | |
fmt.Printf("Setting GOMAXPROCS to %d\n", runtime.NumCPU()) | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
domains := getDomains() | |
communicateChannel := make(chan DomainType) | |
for _, domain := range domains { | |
go lookupDomain(communicateChannel, domain) | |
} | |
// Waiting for all goroutines to complete | |
checkAllDomains(communicateChannel, len(domains)) | |
} |
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
require 'resolv' | |
resolver = Resolv::DNS.new | |
domains = [] | |
File.open("domains.txt").each_line { |domain| | |
domains << domain.strip | |
} | |
domains.map do |name| | |
Thread.new do | |
print "#{name} -> #{resolver.getaddress(name.strip) rescue "No address found"}\n" | |
end | |
end.each(&:join) | |
__END__ | |
$ sudo killall -HUP mDNSResponder # On mountain lion to flush DNS cache | |
$ time ruby parallel_dns_lookup.rb | |
inbox.com -> 64.135.77.80 | |
outlook.com -> 157.56.238.59 | |
hotmail.com -> 65.55.72.167 | |
googlemail.com -> 173.194.70.83 | |
trashmail.net -> 88.198.11.51 | |
live.com -> 65.55.206.154 | |
spamgourmet.com -> 216.75.62.102 | |
flashmail.com -> 207.55.16.224 | |
ymail.com -> 68.180.206.184 | |
everyone.net -> 131.103.75.164 | |
hotmail.it -> 94.245.116.7 | |
onebox.com -> 204.11.168.221 | |
mailinator.com -> 72.51.33.80 | |
comcast.net -> 162.150.0.50 | |
dispostable.com -> 80.94.76.10 | |
guerillamail.com -> 208.73.210.202 | |
yahoo.com -> 206.190.36.45 | |
rediff.com -> 23.62.99.123 | |
qmail.com -> 113.108.11.220 | |
everymail.net -> 212.69.187.37 | |
mail.com -> 213.165.66.221 | |
aol.com -> 64.12.79.57 | |
hotmail.fr -> 94.245.116.13 | |
msn.com -> 65.55.206.228 | |
anonymous.to -> 74.53.27.99 | |
gmail.com -> 173.194.45.53 | |
real 0m15.273s | |
user 0m0.066s | |
sys 0m0.013s | |
$ ruby -v | |
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0] | |
$ sysctl -n machdep.cpu.brand_string | |
Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment