浏览代码

[macro] expose compilation server stats (#12290)

* Expose compilation stats counters

Also add counter for hxb restore and module typing

* Add 'FromHxb' to modules restored counter to be more explicit

* Minimal doc; actual counters should be self explanatory
Rudy Ges 2 月之前
父节点
当前提交
c4225555ee
共有 5 个文件被更改,包括 38 次插入0 次删除
  1. 2 0
      src/compiler/server.ml
  2. 4 0
      src/context/common.ml
  3. 10 0
      src/macro/macroApi.ml
  4. 1 0
      src/typing/typeloadModule.ml
  5. 21 0
      std/haxe/macro/CompilationServer.hx

+ 2 - 0
src/compiler/server.ml

@@ -449,6 +449,7 @@ class hxb_reader_api_server
 				 our current display file if we're in display mode. *)
 			if full_restore then ignore(f_next chunks EOM)
 			else delay PConnectField (fun () -> ignore(f_next chunks EOF));
+			incr stats.s_modules_restored;
 			m
 		| BadModule reason ->
 			die (Printf.sprintf "Unexpected BadModule %s (%s)" (s_type_path path) (Printer.s_module_skip_reason reason)) __LOC__
@@ -593,6 +594,7 @@ and type_module sctx com delay mpath p =
 					   our current display file if we're in display mode. *)
 					if full_restore then ignore(f_next chunks EOM)
 					else delay PConnectField (fun () -> ignore(f_next chunks EOF));
+					incr stats.s_modules_restored;
 					add_modules true m;
 				| Some reason ->
 					skip mpath reason

+ 4 - 0
src/context/common.ml

@@ -40,6 +40,8 @@ let const_type basic const default =
 
 type stats = {
 	s_files_parsed : int ref;
+	s_modules_typed : int ref;
+	s_modules_restored : int ref;
 	s_classes_built : int ref;
 	s_methods_typed : int ref;
 	s_macros_called : int ref;
@@ -477,6 +479,8 @@ let short_platform_name = function
 let stats =
 	{
 		s_files_parsed = ref 0;
+		s_modules_typed = ref 0;
+		s_modules_restored = ref 0;
 		s_classes_built = ref 0;
 		s_methods_typed = ref 0;
 		s_macros_called = ref 0;

+ 10 - 0
src/macro/macroApi.ml

@@ -2359,6 +2359,16 @@ let macro_api ccom get_api =
 			) (decode_array a);
 			vnull
 		);
+		"server_stats", vfun0 (fun () ->
+			encode_obj [
+				"filesParsed", vint !(stats.s_files_parsed);
+				"modulesTyped", vint !(stats.s_modules_typed);
+				"modulesRestoredFromHxb", vint !(stats.s_modules_restored);
+				"classesBuilt", vint !(stats.s_classes_built);
+				"methodsTyped", vint !(stats.s_methods_typed);
+				"macrosCalled", vint !(stats.s_macros_called);
+			]
+		);
 		"position_to_range", vfun1 (fun p ->
 			let p = decode_pos p in
 			let l1,c1,l2,c2 = Lexer.get_pos_coords p in

+ 1 - 0
src/typing/typeloadModule.ml

@@ -718,6 +718,7 @@ let type_module com g mpath file ?(dont_check_path=false) ?(is_extern=false) tde
 	let tdecls = ModuleLevel.handle_import_hx com g m tdecls p in
 	let ctx_m = type_types_into_module com g m tdecls p in
 	if is_extern then m.m_extra.m_kind <- MExtern else if not dont_check_path then Naming.check_module_path ctx_m.com m.m_path p;
+	incr stats.s_modules_typed;
 	m
 
 class hxb_reader_api_typeload

+ 21 - 0
std/haxe/macro/CompilationServer.hx

@@ -42,6 +42,15 @@ enum abstract ModuleCheckPolicy(Int) {
 	var CheckFileContentModification = 2;
 }
 
+typedef CompilationStats = {
+	var filesParsed:Int;
+	var modulesTyped:Int;
+	var modulesRestoredFromHxb:Int;
+	var classesBuilt:Int;
+	var methodsTyped:Int;
+	var macrosCalled:Int;
+}
+
 /**
 	This class provides some methods which can be invoked from command line using
 	`--macro server.field(args)`.
@@ -86,5 +95,17 @@ class CompilationServer {
 	static public function invalidateFiles(filePaths:Array<String>) {
 		@:privateAccess Compiler.load("server_invalidate_files", 1)(filePaths);
 	}
+
+	/**
+		Get current compilation server stats counters.
+
+		Can be called at different compilation stages, or even in the middle of
+		a macro execution.
+
+		Counters are reset at the beginning of each request.
+	**/
+	static public function getStats():CompilationStats {
+		return @:privateAccess Compiler.load("server_stats", 0)();
+	}
 	#end
 }