Last active
September 3, 2016 16:05
-
-
Save hodzanassredin/cdb0c1ff5b11b81fd8e5bd0fa8d975a8 to your computer and use it in GitHub Desktop.
expvars prototype for suave.io
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
// Step 0. Boilerplate to get the paket.exe tool | |
open System | |
open System.IO | |
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__ | |
if (File.Exists "paket.exe") then | |
File.Delete "paket.exe" | |
let url = "https://github.com/fsprojects/Paket/releases/download/3.18.2/paket.exe" | |
use wc = new Net.WebClient() | |
let tmp = Path.GetTempFileName() | |
wc.DownloadFile(url, tmp) | |
File.Move(tmp,Path.GetFileName url);; | |
// Step 1. Resolve and install the packages | |
#r "paket.exe" | |
Paket.Dependencies.Install """ | |
source https://nuget.org/api/v2 | |
nuget Suave | |
""";; | |
// Step 2. Use the packages | |
#r "packages/Suave/lib/net40/Suave.dll" | |
#r "System.Runtime.Serialization" | |
open Suave // always open suave | |
open Suave.Filters | |
open Suave.Operators | |
open Suave.Successful | |
open Suave.Json | |
open System.Runtime.Serialization | |
open System.Net | |
type RateCounter() = | |
let mutable lastTick = System.Environment.TickCount | |
let mutable lastFrameRate = 0 | |
let mutable frameRate = 0 | |
let lockObj = new obj() | |
let synchronized f v = lock lockObj (fun () -> f v) | |
let inc v = | |
if System.Environment.TickCount - lastTick >= 1000 | |
then | |
lastFrameRate <- frameRate | |
frameRate <- v | |
lastTick <- System.Environment.TickCount | |
else | |
frameRate <- frameRate + v | |
lastFrameRate | |
member x.Inc v = synchronized inc v |> ignore | |
member x.Get () = synchronized inc 0 | |
let helloCounter = RateCounter() | |
let hello (x : HttpContext) = | |
helloCounter.Inc 1 | |
OK "Hello World!" x | |
let _, server = startWebServerAsync defaultConfig hello | |
let client = new WebClient () | |
let rec gen_load sleepMs = async{ | |
let! _ = client.AsyncDownloadString(new Uri("http://127.0.0.1:8083/")) | |
do! Async.Sleep(sleepMs) | |
return! gen_load(sleepMs) | |
} | |
//startWebServer defaultConfig hello | |
let expvarsConfig = {defaultConfig with bindings = [ HttpBinding.mkSimple Protocol.HTTP "0.0.0.0" 8000]} | |
let _, expvarsServer = startWebServerAsync expvarsConfig (path "/debug/vars" >=> fun ctx -> (OK (sprintf "{\"hellorps\":%d}" <| helloCounter.Get() ) ctx)) | |
Async.Parallel [server; expvarsServer; gen_load(0)] |> Async.RunSynchronously |> ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment