Browse Source

[display] wrap json-rpc result, add timers

Simon Krajewski 7 years ago
parent
commit
b80004a7dd
4 changed files with 53 additions and 11 deletions
  1. 4 4
      src/compiler/main.ml
  2. 39 2
      src/context/displayJson.ml
  3. 1 0
      src/core/json/genjson.ml
  4. 9 5
      src/core/timer.ml

+ 4 - 4
src/compiler/main.ml

@@ -687,7 +687,7 @@ try
 		("Services",["--display"],[], Arg.String (fun input ->
 			let input = String.trim input in
 			if String.length input > 0 && (input.[0] = '[' || input.[0] = '{') then begin
-				DisplayJson.parse_input com input
+				DisplayJson.parse_input com input measure_times
 			end else
 				DisplayOutput.handle_display_argument com input pre_compilation did_something;
 		),"","display code tips");
@@ -930,13 +930,13 @@ with
 	| DisplayException(DisplayPackage pack) ->
 		raise (DisplayOutput.Completion (DisplayOutput.print_package ctx.com pack))
 	| DisplayException(DisplayFields(fields,is_toplevel)) ->
-		let fields =
-			if !measure_times then begin
+		let fields = match ctx.com.json_out with
+			| None when !measure_times ->
 				Timer.close_times();
 				(List.map (fun (name,value) ->
 					DisplayTypes.CompletionKind.ITTimer("@TIME " ^ name,value)
 				) (DisplayOutput.get_timer_fields !start_time)) @ fields
-			end else
+			| _ ->
 				fields
 		in
 		raise (DisplayOutput.Completion (DisplayOutput.print_fields ctx.com fields is_toplevel))

+ 39 - 2
src/context/displayJson.ml

@@ -4,6 +4,8 @@ open JsonRpc
 open Json
 open Common
 open DisplayTypes.DisplayMode
+open Timer
+open Genjson
 
 type haxe_json_error =
 	| MissingParam of string
@@ -21,7 +23,31 @@ let get_capabilities () =
 		"packageProvider",JBool true;
 	]
 
-let parse_input com input =
+(* Generate the JSON of our times. *)
+let json_of_times root =
+	let rec loop node =
+		if node.time > 0.0009 then begin
+			let children = ExtList.List.filter_map loop node.children in
+			let fl = [
+				"name",jstring node.name;
+				"path",jstring node.path;
+				"info",jstring node.info;
+				"time",jfloat node.time;
+				"calls",jint node.num_calls;
+				"percentTotal",jfloat (node.time *. 100. /. root.time);
+				"percentParent",jfloat (if node == root then 0. else node.time *. 100. /. node.parent.time);
+			] in
+			let fl = match children with
+				| [] -> fl
+				| _ -> ("children",jarray children) :: fl
+			in
+			Some (jobject fl)
+		end else
+			None
+	in
+	loop root
+
+let parse_input com input report_times =
 	let fail json =
 		prerr_endline (string_of_json json);
 		exit 1;
@@ -29,7 +55,18 @@ let parse_input com input =
 	let process () =
 		let id,name,params = JsonRpc.parse_request input in
 		let f_result json =
-			(string_of_json (JsonRpc.result id json));
+			let fl = [
+				"result",json;
+			] in
+			let fl = if !report_times then begin
+				let _,_,root = Timer.build_times_tree () in
+				begin match json_of_times root with
+				| None -> fl
+				| Some jo -> ("timers",jo) :: fl
+				end
+			end else fl in
+			let jo = jobject fl in
+			(string_of_json (JsonRpc.result id jo));
 		in
 		let f_error jl =
 			fail (JsonRpc.error id 0 ~data:(Some (JArray jl)) "Compiler error")

+ 1 - 0
src/core/json/genjson.ml

@@ -10,6 +10,7 @@ type context = {
 let jnull = Json.JNull
 let jstring s = Json.JString s
 let jint i = Json.JInt i
+let jfloat f = Json.JFloat f
 let jbool b = Json.JBool b
 let jarray l = Json.JArray l
 let jobject l = Json.JObject l

+ 9 - 5
src/core/timer.ml

@@ -80,7 +80,7 @@ type timer_node = {
 	mutable children : timer_node list;
 }
 
-let report_times print =
+let build_times_tree () =
 	let nodes = Hashtbl.create 0 in
 	let rec root = {
 		name = "";
@@ -149,13 +149,17 @@ let report_times print =
 		if node.time > 0.0009 && l > !max_name then max_name := l;
 	in
 	loop 0 root;
-	let max_calls = String.length (string_of_int !max_calls) in
-	print (Printf.sprintf "%-*s | %7s |   %% |  p%% | %*s | info" !max_name "name" "time(s)" max_calls "#");
-	let sep = String.make (!max_name + max_calls + 27) '-' in
+	!max_name,!max_calls,root
+
+let report_times print =
+	let max_name,max_calls,root = build_times_tree () in
+	let max_calls = String.length (string_of_int max_calls) in
+	print (Printf.sprintf "%-*s | %7s |   %% |  p%% | %*s | info" max_name "name" "time(s)" max_calls "#");
+	let sep = String.make (max_name + max_calls + 27) '-' in
 	print sep;
 	let print_time name node =
 		if node.time > 0.0009 then
-			print (Printf.sprintf "%-*s | %7.3f | %3.0f | %3.0f | %*i | %s" !max_name name node.time (node.time *. 100. /. root.time) (node.time *. 100. /. node.parent.time) max_calls node.num_calls node.info)
+			print (Printf.sprintf "%-*s | %7.3f | %3.0f | %3.0f | %*i | %s" max_name name node.time (node.time *. 100. /. root.time) (node.time *. 100. /. node.parent.time) max_calls node.num_calls node.info)
 	in
 	let rec loop depth node =
 		let name = (String.make (depth * 2) ' ') ^ node.name in