123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325letnow=Unix.gettimeofday(** add missing [float] function to Atomic *)moduleAtomic=structincludeAtomicletadd_floatax=letfn()=letv=Atomic.getainAtomic.compare_and_setav(v+.x)inwhilenot(fn())do()doneletmax_floatax=letfn()=letv=Atomic.getainAtomic.compare_and_setav(maxvx)inwhilenot(fn())do()doneendtypestats={(* We must use atomic for this to work with domains! *)nbreq:intAtomic.t;total:floatAtomic.t;maxim :floatAtomic.t;parse :floatAtomic.t;build :floatAtomic.t;send :floatAtomic.t}letinit ()={nbreq=Atomic.make0;total=Atomic.make0.;maxim=Atomic.make0.;parse=Atomic.make0.;build=Atomic.make0.;send=Atomic.make0.}letglobal=ref(init())let per_path=ref(Hashtbl.create128)letgraph_size =ref(24*60)let graph_interval=ref60typegraph={tbl:intAtomic.tarray;cur:intAtomic.t;tim:floatAtomic.t}letgraph=ref {tbl=Array.init!graph_size(fun_->Atomic.make0);cur=Atomic.make0;tim=Atomic.make(Unix.gettimeofday())}letsave_name ="STATS"letsavech=output_valuech(save_name,1);output_valuech!global;output_valuech!per_path;output_valuech!graph_size;output_valuech!graph_interval;output_valuech!graphletrestore(name,version)ch=assert(name=save_name);match versionwith|1->global:=input_valuech;per_path:=input_valuech;graph_size :=input_valuech;graph_interval :=input_valuech;graph:=input_valuech|_->assertfalseletadd_reqt1=letint=float!graph_intervalinletadd_time()=letold=Atomic.get!graph.timinletnew_=old+.intint1>new_ &&Atomic.compare_and_set!graph.timoldnew_inwhileadd_time()doletold=Atomic.get!graph.curinletact=(old+1)modArray.length !graph.tblinifAtomic.compare_and_set!graph.curoldactthenAtomic.set!graph.tbl.(act)0done;Atomic.incr!graph.tbl.(Atomic.get!graph.cur)letdraw_graphsize(moduleOutput:Html.Output)=leti0=Atomic.get!graph.curinletlen=Array.length!graph.tblinOutput.echo"<script>var data = [";fori=i0-size+1toi0doleti=ifi<0theni+lenelseiinletn=Atomic.get!graph.tbl.(i)inOutput.printf"%d,"ndone;Output.echo"];</script>"(** [Simple_httpd] provides filter for request, that can be used to collecting
statistics. Currently, we can not count the time to output the response. *)letfilter()=letmeasurereq=lethost=matchRequest.get_headerreqHeaders.Hostwith|None->"?"|Someh->hinletpath=Util.fast_concat'/'(host::Request.path_componentsreq)inletpp=tryHashtbl.find!per_pathpathwithNot_found->letr=init()inHashtbl.add!per_pathpathr;rinAtomic.incr!global.nbreq;Atomic.incrpp.nbreq;lett1=Request.start_timereqinadd_reqt1;lett2=now()in(req,funresponse->lett3=now()inAtomic.add_float!global.parse(t2-.t1);Atomic.add_float!global.build(t3-.t2);Atomic.add_floatpp.parse(t2-.t1);Atomic.add_floatpp.build(t3-.t2);let post()=let t4=now()inAtomic.max_float!global.maxim(t4-.t1);Atomic.add_float!global.total(t4-.t1);Atomic.add_float!global.send(t4-.t3);Atomic.max_floatpp.maxim(t4-.t1);Atomic.add_floatpp.total(t4-.t1);Atomic.add_floatpp.send(t4-.t3)inResponse.set_post(fun()->Response.get_postresponse();post())response)inletstatoutputpathr=letnb=Atomic.getr.nbreqinletn=floatnbinletfx=Atomic.getx*.1e3/.nin{funml| <tr>
<td>{`path`}
<td class="scol">{`string_of_int nb`}
<td class="scol">{`Printf.sprintf "%.3f" (f r.total)`}
<td class="scol">{`Printf.sprintf "%.3f" (1e3 *. Atomic.get r.maxim) `}
<td class="scol">{`Printf.sprintf "%.3f" (f r.parse)`}
<td class="scol">{`Printf.sprintf "%.3f" (f r.build)`}
<td class="scol">{`Printf.sprintf "%.3f" (f r.send)`}
</tr>
|funml}outputinletget_stat?in_head?css?start_header?end_header?start_contents?end_contentsreqheaders =letcss=matchcsswith|None->""|Somes->{html|<link rel="stylesheet" href={`s`}>|html}in{chaml|
<!DOCTYPE html>
<head>
<meta charset="UTF-8"/>
<title>server status</title>
<style>
table, th, td{ border: 1px solid black;
border-collapse: collapse; }
table { margin-left: auto; margin-right: auto; } .scol { text-align: right;
vertical-align: top;
padding: 3px;
white-space: nowrap; }
.info { text-align: left;
vertical-align: top;
padding: 3px; }
.info div {
max-width: 75vw;
overflow: scroll; }
</style>
<ml>draw_graph !graph_size output</ml>
{`css`}
<script>
function sort(tableId,index,num,desc) {
var tbody = document.getElementById(tableId);
var rows = Array.from(tbody.rows);
rows.sort(function(left, right) {
var l = left.children[index].innerHTML;
var r = right.children[index].innerHTML;
if (desc) {
if (num) return (Number(r) - Number(l));
else return(r < l ? -1 : l < r ? 1 : 0);
} else {
if (num) return (Number(l) - Number(r));
else return(l < r ? -1 : r < l ? 1 : 0);
}
});
// Put them back in the tbody
tbody.innerHTML='';
for(var i = 0; i < rows.length; i++) {
tbody.appendChild(rows[i]);
}
};
function filter(tableId,index,inputId) {
var str = document.getElementById(inputId).value;
var tbody = document.getElementById(tableId);
var rows = tbody.rows;
for(var i = 0; i < rows.length; i++) {
let inner = rows[i].cells[index].textContent;
if (inner.indexOf(str) >= 0) {
rows[i].style.display = 'table-row';
} else {
rows[i].style.display = 'none';
}
}
};
var zoom_graph = 1;
function draw_graph(canvasId, data) {
const canvas = document.getElementById(canvasId);
canvas.width = window.innerWidth * 0.8;
canvas.height = window.innerHeight * 0.25;
canvas.style.marginLeft = window.innerWidth * 0.1 + "px";
const w = canvas.width;
const h = canvas.height;
let maxi = 0;
let len = Math.floor(data.length * zoom_graph);
let start = data.length - len;
for (i = start; i < data.length; i++) {
maxi = Math.max(maxi,data[i]);
}
function X(i) { return (((i - start) * w) / (len - 1) ); };
function Xinv(t) { return (t * (len - 1) / w + start); };
function Y(i) { return ((maxi - i) * (h - 1) / maxi); };
function Yinv(t) { return (maxi - t * maxi / (h - 1)); };
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.moveTo(X(0),Y(data[0]))
for (var i = start; i < data.length; i++) {
ctx.lineTo(X(i),Y(data[i]));
}
ctx.stroke();
ctx.font = "12px Arial";
ctx.setLineDash([5, 15]);
for (var i = 1; i <= 5; i++) {
ctx.fillText(Yinv(i*h/6).toFixed(2),0,i*h/6 + 5);
ctx.beginPath();
ctx.moveTo(40,i * h / 6);
ctx.lineTo(w,i * h / 6);
ctx.stroke();
}
}
function do_zoom(event) {
event.preventDefault();
let dY = event.deltaY;
zoom_graph = Math.min(1,zoom_graph * Math.pow(2, event.deltaY / 250));
draw_graph('canvas',data);
}
function onLoad() {
sort('table',1,true,true);
draw_graph('canvas',data);
setTimeout ( "location.reload ();", 60000);
document.getElementById('canvas').addEventListener("wheel", do_zoom);
}
</script>
<ml>match in_head with None -> () | Some f -> f output</ml>
</head>
<body onload="onLoad();">
<header>
<ml>match start_header with None -> () | Some f -> f output</ml>
<h1>Statistics of the server</h1>
<ml>match end_header with None -> () | Some f -> f output</ml>
</header>
<div class="contents">
<ml>match start_contents with None -> () | Some f -> f output</ml>
<canvas style="margin: 0 auto;" id='canvas'>
</canvas>
<details><summary>Note about timings</summary>
All timings are average time in milliseconds. Parsing time does not take
in account parsing the body of the request if it is read as a stream, in
which case parsing time is included in building time. Similarly,
building time of the body of the response is included in send time
if it is a stream. Actually, if your request is build as a stream
processer, parsing will only include the parsing of the request
headers, build will only include the building of the response headers,
and all the stream processing will be included in the send time.
</details>
<table>
<thead><tr>
<th>path
<button onclick="sort('table',0,false,false);">â–¼</button>
<button onclick="sort('table',0,false,true);">â–²</button>
<input id="path_filter" type="text"
onchange="filter('table',0,'path_filter')">
<th>nb req.
<button onclick="sort('table',1,true,false);">â–¼</button>
<button onclick="sort('table',1,true,true);">â–²</button>
<th>total
<button onclick="sort('table',2,true,false);">â–¼</button>
<button onclick="sort('table',2,true,true);">â–²</button>
<th>max
<button onclick="sort('table',3,true,false);">â–¼</button>
<button onclick="sort('table',3,true,true);">â–²</button>
<th>parsing
<button onclick="sort('table',4,true,false);">â–¼</button>
<button onclick="sort('table',4,true,true);">â–²</button>
<th>build
<button onclick="sort('table',5,true,false);">â–¼</button>
<button onclick="sort('table',5,true,true);">â–²</button>
<th>send
<button onclick="sort('table',6,true,false);">â–¼</button>
<button onclick="sort('table',6,true,true);">â–²</button>
</tr>
<ml>stat output "global" !global</ml>
</thead>
<tbody id="table">
<ml>Hashtbl.iter (stat output) !per_path</ml>
</tbody>
</table>
<ml>match end_contents with None -> () | Some f -> f output</ml>
</div>
</body>|chaml}reqheadersin(measure,get_stat)