Created
July 8, 2011 20:44
-
-
Save leifwickland/1072762 to your computer and use it in GitHub Desktop.
Example of slamming a site with lots of HTTP HEAD requests in Scala
This file contains 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
// Note to self: Use https://github.com/scalaj/scalaj-http next time instead. | |
import java.net._ | |
import actors.Future | |
import actors.Futures._ | |
object woobox extends App { | |
val codes = ('a'.to('z').map(_.toString) ++ 0.to(9).map(_.toString)).combinations(6) | |
val blockSize = 100 | |
var i = 0 | |
while (codes.hasNext) { | |
val futures = codes.take(blockSize).map("http://woobox.com/" + _.mkString).map(getCode).toSeq | |
i += futures.size | |
val responses = awaitAll(99999999, futures:_*).flatMap(x=>x).asInstanceOf[Seq[(Int, String)]] | |
val responsesByCode = responses.groupBy(_._1) | |
println(delta + "url=" + responses.last._2 + " i=" + i + " " + responsesByCode.map{ case (code, c) => "%d->%d".format(code, c.size)}.mkString(", ")) | |
responsesByCode.getOrElse(200, Nil).foreach{url => println(" match: " + url)} | |
} | |
def getCode(url: String): Future[(Int, String)] = future { | |
val request = new URL(url).openConnection.asInstanceOf[HttpURLConnection] | |
request.setRequestMethod("HEAD") | |
(request.getResponseCode, url) | |
} | |
def delta = "%09.1f: ".format(0.001 * (System.currentTimeMillis - this.executionStart)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment