Skip to content

Instantly share code, notes, and snippets.

@lefthandedgoat
Created June 2, 2014 15:26
Show Gist options
  • Save lefthandedgoat/a5934130cad7f293a2c5 to your computer and use it in GitHub Desktop.
Save lefthandedgoat/a5934130cad7f293a2c5 to your computer and use it in GitHub Desktop.
open System.Net
open Microsoft.FSharp.Control.WebExtensions
open System.Diagnostics
open System
let fetch name (url:string) =
printfn "fetching %s" name
let uri = new System.Uri(url)
use webClient = new WebClient()
let stopwatch = Stopwatch()
stopwatch.Start()
let html = webClient.DownloadString(uri)
decimal stopwatch.Elapsed.Milliseconds
let random = Random()
let sleep fromMS toMS accumulator =
let milliseconds = random.Next(fromMS, toMS)
System.Threading.Thread.Sleep(milliseconds)
accumulator
let start = 0.0M
let microsoft accumulator = accumulator + fetch "Microsoft.com" "http://www.microsoft.com/en-us/default.aspx"
let msdn accumulator = accumulator + fetch "MSDN" "http://msdn.microsoft.com/"
let bing accumulator = accumulator + fetch "Bing" "http://www.bing.com"
let duckduckgo accumulator = accumulator + fetch "Duck Duck Go" "https://duckduckgo.com/"
let yahoo accumulator = accumulator + fetch "Yahoo" "http://www.yahoo.com"
let google accumulator = accumulator + fetch "Google" "http://www.google.com"
let generate number func = [ for x in 1 .. number do yield func]
let normalWorkflow () =
start
|> microsoft
|> sleep 1000 2000
|> msdn
|> sleep 1500 2000
|> bing
|> sleep 2000 3000
|> duckduckgo
|> sleep 1000 2000
|> yahoo
|> sleep 4000 6000
|> google
let fastWorkflow () =
start
|> microsoft
|> sleep 100 200
|> msdn
|> sleep 150 200
|> bing
|> sleep 200 300
|> duckduckgo
|> sleep 100 200
|> yahoo
|> sleep 400 600
|> google
let all =
generate 5 normalWorkflow
@ generate 10 fastWorkflow
@ generate 50 normalWorkflow
@ generate 2 fastWorkflow
@ generate 4 normalWorkflow
let results =
all
|> Array.ofList
|> Array.Parallel.map (fun f -> f())
@lefthandedgoat
Copy link
Author

Sorry didn't see the response. I will start editing the gist ;p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment