Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mirage/mirage-www
Browse files Browse the repository at this point in the history
  • Loading branch information
avsm committed Apr 28, 2015
2 parents 3d73867 + 08dbf5a commit 925b5a3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/dispatch.ml
Expand Up @@ -64,6 +64,10 @@ module Main
| [] | [""] | ["index.html"] ->
return (`Html (Pages.Index.t ~feeds:updates_feeds read_tmpl))

| ["stats"; "gc"] ->
let gc = return (Cow.Html.to_string (Stats.page ())) in
return (`Html gc)

| ["about"]
| ["community"] ->
return (`Html (Pages.About.t read_tmpl))
Expand Down Expand Up @@ -101,6 +105,8 @@ module Main
let cid = Cohttp.Connection.to_string conn_id in
C.log c (Printf.sprintf "conn %s closed" cid)
in

Stats.start OS.Time.sleep;
http (S.make ~callback ~conn_closed ())

end
51 changes: 51 additions & 0 deletions src/stats.ml
@@ -0,0 +1,51 @@
let (>>=) = Lwt.bind

let delay = 60. *. 2.
let history = 10_000

let html_of_stat t =
let open Gc in
let k f =
let str = Printf.sprintf "%.0fk" (f /. 1000.) in
Cow.Html.of_string str
in
<:html<
<tr>
<td>$k t.minor_words$</td>
<td>$k t.major_words$</td>
<td>$int:t.minor_collections$</td>
<td>$int:t.major_collections$</td>
</tr>
>>

let html_of_stats ts =
<:html<
<table>
<tr>
<th>Minor Words</th>
<th>Major Words</th>
<th>Minor Collections</th>
<th>Major Collections</th>
</tr>
$list:List.map html_of_stat ts$
</table>
>>

let stats = Queue.create ()

let start ~sleep =
let gather () =
let stat = Gc.quick_stat () in
if Queue.length stats >= history then ignore (Queue.pop stats);
Queue.push stat stats
in
let rec loop () =
gather ();
sleep delay >>= fun () ->
loop ()
in
Lwt.async loop

let page () =
let stats = Queue.fold (fun acc s -> s :: acc) [] stats in
html_of_stats stats
2 changes: 2 additions & 0 deletions src/stats.mli
@@ -0,0 +1,2 @@
val start: sleep:(float -> unit Lwt.t) -> unit
val page: unit -> Cow.Html.t

0 comments on commit 925b5a3

Please sign in to comment.