Created
October 4, 2011 18:32
-
-
Save dgfitch/1262415 to your computer and use it in GitHub Desktop.
Simple HTTP statistics timer in F#
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
open System | |
open System.IO | |
open System.Net | |
open System.Diagnostics | |
let track (url:string) (log:string) = | |
while true do | |
let stopwatch = new Stopwatch() | |
stopwatch.Start() | |
let error = ref (None : string option) | |
let status = ref 0 | |
try | |
let request = WebRequest.Create(url) :?> HttpWebRequest | |
use response = request.GetResponse() :?> HttpWebResponse | |
status := int response.StatusCode | |
with ex -> | |
error := Some ex.Message | |
stopwatch.Stop() | |
try | |
printfn "%A - elapsed %A" System.DateTime.Now stopwatch.Elapsed | |
use file = File.AppendText log | |
file.WriteLine( sprintf "\"%A\",\"%A\",\"%s\"" System.DateTime.Now stopwatch.Elapsed.Milliseconds (defaultArg error.Value "") ) | |
file.Close() | |
with ex -> | |
printfn "ERROR: %s" ex.Message | |
System.Threading.Thread.Sleep INTERVAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment