digestanalyst.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. unit digestanalyst;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, teststr, testu, tresults, dbtests;
  6. Type
  7. // Program configuration
  8. TDigestConfig = record
  9. databasename: string;
  10. host: string;
  11. username: string;
  12. password: string;
  13. port: integer;
  14. testsrcdir: string;
  15. relsrcdir: string;
  16. verbose: string;
  17. sql: string;
  18. end;
  19. { TDBDigestAnalyzer }
  20. TDBDigestAnalyzer = Class(TObject)
  21. private
  22. FDB : TTestSQL;
  23. LongLogFile : TStrings;
  24. UnknownLines : integer;
  25. UseLongLog : Boolean;
  26. FCurLongLogLine : Integer;
  27. FPrefix : String;
  28. // Call global verbose with prefix to message.
  29. procedure Verbose(aLevel : TVerboseLevel; const aMsg : string);
  30. // Get the execute log for a given test
  31. function GetExecuteLog(Line, FN: String): String;
  32. // Get the IDs from all config parameters: OS, Log,
  33. function GetIDs(const aConfig: TDigestConfig; var aData: TTestRunData): Boolean;
  34. // Check that all IDS needed for a test run are <>-1
  35. function CheckIDs(var aData: TTestRunData): Boolean;
  36. // process a log file.
  37. procedure Processfile(const aFileName: String; var aData: TTestRunData);
  38. // Update the test run statistics.
  39. procedure UpdateTestRun(const aData: TTestRunData);
  40. // Get contents from longlog
  41. function GetContentsFromLongLog(Line: String): String;
  42. // Get Log from file line
  43. function GetLog(Line, FN: String): String;
  44. public
  45. constructor Create(aDB : TTestSQL; const aPrefix : String);
  46. // Extract the status from a log line. Will change the log line.
  47. class function AnalyseLine(var Line: string; var Status: TTestStatus): Boolean;
  48. // Extract test filename from a log line
  49. class procedure ExtractTestFileName(var Line: string);
  50. // Analyse the file.
  51. procedure Analyse(aConfig : TDigestConfig; aData : TTestRunData);
  52. // Save test result. Return true if a NEW test result record was created (and the result must be counted)
  53. function SaveTestResult(var aResult: TTestResultData): Boolean;
  54. // DB connection to use
  55. property DB : TTestSQL read FDB;
  56. end;
  57. implementation
  58. constructor TDBDigestAnalyzer.Create(aDB: TTestSQL; const aPrefix: String);
  59. begin
  60. FDB:=aDB;
  61. FPrefix:=aPrefix;
  62. end;
  63. procedure TDBDigestAnalyzer.Verbose(aLevel: TVerboseLevel; const aMsg: string);
  64. begin
  65. testu.Verbose(aLevel,FPrefix+aMsg);
  66. end;
  67. function TDBDigestAnalyzer.CheckIDs(var aData : TTestRunData): Boolean;
  68. begin
  69. If aData.CategoryID=-1 then
  70. aData.CategoryID:=1;
  71. Result:=(aData.CPUID<>-1) and (aData.OSID<>-1) and (aData.VersionID<>-1);
  72. if Result then
  73. exit;
  74. If aData.CPUID=-1 then
  75. Verbose(V_WARNING,'NO ID for CPU "'+aData.CPU+'" found.');
  76. If aData.OSID=-1 then
  77. Verbose(V_WARNING,'NO ID for OS "'+aData.OS+'" found.');
  78. If aData.VersionID=-1 then
  79. Verbose(V_WARNING,'NO ID for version "'+aData.Version+'" found.');
  80. end;
  81. procedure TDBDigestAnalyzer.Analyse(aConfig: TDigestConfig; aData : TTestRunData);
  82. begin
  83. FDB.RelSrcDir:=aConfig.relsrcdir;
  84. FDB.TestSrcDir:=aConfig.testsrcdir;
  85. if (aData.longlogfile<>'') and FileExists(aData.longlogfile) then
  86. begin
  87. LongLogFile:=TStringList.Create;
  88. LongLogFile.LoadFromFile(aData.longlogfile);
  89. end;
  90. if not GetIDS(aConfig,aData) then
  91. exit;
  92. ProcessFile(aData.logfile,aData);
  93. UpdateTestRun(aData);
  94. end;
  95. function TDBDigestAnalyzer.GetIDs(const aConfig : TDigestConfig; var aData : TTestRunData): Boolean;
  96. begin
  97. Result := False;
  98. aData.CPUID := FDB.GetCPUID(aData.CPU);
  99. aData.OSID := FDB.GetOSID(aData.OS);
  100. aData.VersionID := FDB.GetVersionID(aData.Version);
  101. if aData.Category='' then
  102. aData.Category:='Compiler/RTL';
  103. aData.CategoryID := FDB.GetCategoryID(aData.Category);
  104. aData.PlatformID := FDB.GetPlatformID(aData,True);
  105. If (Round(aData.Date)=0) then
  106. aData.Date:=Date;
  107. Result:=CheckIDS(aData);
  108. if not Result then
  109. Exit;
  110. aData.RunID:=FDB.GetRunID(aData);
  111. If (aData.RunID<>-1) then
  112. FDB.CleanTestRun(aData.RunID)
  113. else
  114. aData.RunID:=FDB.AddRun(aData);
  115. Result:=aData.RunID<>-1;
  116. if not Result then
  117. begin
  118. Verbose(V_Error,'Could not insert new testrun record!');
  119. exit;
  120. end;
  121. end;
  122. class procedure TDBDigestAnalyzer.ExtractTestFileName(var Line: string);
  123. Var I : integer;
  124. begin
  125. I:=Pos(' ',Line);
  126. If (I<>0) then
  127. Line:=Copy(Line,1,I-1);
  128. end;
  129. class function TDBDigestAnalyzer.AnalyseLine(var Line: string; var Status: TTestStatus): Boolean;
  130. Var
  131. TS : TTestStatus;
  132. begin
  133. Result:=False;
  134. For TS:=FirstStatus to LastStatus do
  135. begin
  136. Result:=Pos(StatusText[TS],Line)=1;
  137. If Result then
  138. begin
  139. Status:=TS;
  140. Delete(Line,1,Length(StatusText[TS]));
  141. ExtractTestFileName(Line);
  142. Break;
  143. end;
  144. end;
  145. end;
  146. (*
  147. ConfigAddStrings : Array [TConfigAddOpt] of string = (
  148. 'compilerdate',
  149. 'compilerfullversion',
  150. 'svncompilerrevision',
  151. 'svntestsrevision',
  152. 'svnrtlrevision',
  153. 'svnpackagesrevision'
  154. );
  155. ConfigAddCols : Array [TConfigAddOpt] of string = (
  156. 'TU_COMPILERDATE',
  157. 'TU_COMPILERFULLVERSION',
  158. 'TU_SVNCOMPILERREVISION',
  159. 'TU_SVNTESTSREVISION',
  160. 'TU_SVNRTLREVISION',
  161. 'TU_SVNPACKAGESREVISION'
  162. );
  163. *)
  164. const
  165. SeparationLine = '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>';
  166. function TDBDigestAnalyzer.GetContentsFromLongLog(Line: String): String;
  167. Function GetLongLogLine : String;
  168. begin
  169. Result:=LongLogFile[FCurLongLogLine];
  170. Inc(FCurLongLogLine);
  171. end;
  172. Function HaveLongLogLine : Boolean; inline;
  173. begin
  174. Result:=FCurLongLogLine<LongLogFile.Count;
  175. end;
  176. var
  177. S : String;
  178. IsFirst, IsFound : boolean;
  179. begin
  180. Result:='';
  181. IsFirst:=true;
  182. IsFound:=false;
  183. While HaveLongLogLine do
  184. begin
  185. S:=GetLongLogLine;
  186. if FCurLongLogLine=1 then
  187. begin
  188. { At start of file there is a separation line }
  189. if (pos(Line,S)=0) and (pos(SeparationLine,S)>=1) then
  190. S:=GetLongLogLine
  191. end;
  192. if pos(Line,S)=1 then
  193. begin
  194. IsFound:=true;
  195. while HaveLongLogLine do
  196. begin
  197. S:=GetLongLogLine;
  198. { End of file marker }
  199. if (Not HaveLongLogLine) or (pos(SeparationLine,S)=1) then
  200. exit;
  201. if length(Result)<MaxLogSize then
  202. Result:=Result+S+LineEnding;
  203. if pos(SeparationLine,S)>1 then
  204. exit;
  205. end;
  206. end
  207. else if IsFirst then
  208. begin
  209. Verbose(V_Warning,'Line "'+Line+'" not found as next "'+S+'"');
  210. IsFirst:=false;
  211. end;
  212. end;
  213. if not IsFound then
  214. begin
  215. Verbose(V_Warning,'Line "'+Line+'" not found');
  216. FCurlongLogLine:=0; // Reset
  217. end;
  218. end;
  219. function TDBDigestAnalyzer.GetLog(Line, FN: String): String;
  220. begin
  221. if UseLongLog then
  222. begin
  223. Result:=GetContentsFromLongLog(Line);
  224. exit;
  225. end;
  226. FN:=ChangeFileExt(FN,'.log');
  227. { packages tests have ../ replaced by root/ }
  228. if not FileExists(FN) and (Copy(FN,1,3)='../') then
  229. FN:='root/'+Copy(FN,4,length(FN));
  230. If FileExists(FN) then
  231. Result:=GetFileContents(FN)
  232. else
  233. begin
  234. Verbose(V_Warning,'File "'+FN+'" not found');
  235. Result:='';
  236. end;
  237. end;
  238. function TDBDigestAnalyzer.GetExecuteLog(Line, FN: String): String;
  239. begin
  240. if UseLongLog then
  241. begin
  242. Result:=GetContentsFromLongLog(Line);
  243. exit;
  244. end;
  245. FN:=ChangeFileExt(FN,'.elg');
  246. { packages tests have ../ replaced by root/ }
  247. if not FileExists(FN) and (Copy(FN,1,3)='../') then
  248. FN:='root/'+Copy(FN,4,length(FN));
  249. If FileExists(FN) then
  250. Result:=GetFileContents(FN)
  251. else
  252. begin
  253. Verbose(V_Warning,'File "'+FN+'" not found');
  254. Result:='';
  255. end;
  256. end;
  257. function TDBDigestAnalyzer.SaveTestResult(var aResult: TTestResultData): Boolean;
  258. var
  259. lLast : TTestResultData;
  260. lNewID : Int64;
  261. begin
  262. Result:=False;
  263. // Get last result for this test.
  264. lLast:=FDB.GetLastTestResult(aResult.TestID,aResult.PlatformID);
  265. if (aResult.Date<lLast.Date) then
  266. exit; // Do not save earlier results
  267. if not aResult.ResultDiffers(lLast) then
  268. exit; // do not save identical results
  269. // Need to save.
  270. lNewID:=FDB.AddTestResult(aResult);
  271. aResult.ID:=lNewId;
  272. // Save current in lastresult
  273. Result:=(LLast.ID<>lNewID);
  274. if Result then
  275. begin
  276. // When new, save previous.
  277. FDB.AddLastResult(aResult.TestID,aResult.PlatformID,lNewID);
  278. FDB.AddPreviousResult(aResult.TestID,aResult.PlatformID,LLast.ID);
  279. end;
  280. end;
  281. procedure TDBDigestAnalyzer.Processfile(const aFileName: String; var aData: TTestRunData);
  282. var
  283. logfile : TStrings;
  284. fullline,line,prevLine : string;
  285. TS : TTestStatus;
  286. lPrev,lResult : TTestResultData;
  287. begin
  288. lPrev:=Default(TTestResultData);
  289. lResult:=Default(TTestResultData);
  290. // init data common to the whole testrun
  291. lResult.RunID:=aData.RunID;
  292. lResult.PlatFormID:=aData.PlatFormID;
  293. lResult.Date:=aData.Date;
  294. lPrev.RunID:=aData.RunID;
  295. lPrev.PlatformID:=aData.PlatformID;
  296. lPrev.TestID:=-1; // Init no test
  297. lPrev.Date:=aData.Date;
  298. for TS in TTestStatus do
  299. aData.StatusCount[TS]:=0;
  300. PrevLine:='';
  301. logfile:=TStringList.Create;
  302. try
  303. LogFile.Capacity:=20000;
  304. LogFile.LoadFromFile(aFileName);
  305. For FullLine in LogFile do
  306. begin
  307. line:=fullline;
  308. TS:=stFailedToCompile;
  309. lResult.TestResult:=TS;
  310. If not AnalyseLine(line,TS) then
  311. begin
  312. Inc(UnknownLines);
  313. Verbose(V_Warning,'Unknown line: "'+line+'"');
  314. end
  315. else
  316. begin
  317. Verbose(V_NORMAL,'Analysing result for test '+Line);
  318. lResult.TestID:=FDB.RequireTestID(line);
  319. if lResult.TestID=-1 then
  320. begin
  321. Verbose(V_Warning,'No test ID: "'+line+'", skipping');
  322. Continue;
  323. end;
  324. If ExpectRun[TS] then
  325. begin
  326. // We expect a log line with log result, save
  327. Inc(aData.StatusCount[TS]);
  328. lPrev.TestResult:=TS;
  329. lPrev.TestID:=lResult.TestID;
  330. PrevLine:=line;
  331. end
  332. else
  333. begin
  334. // New test, insert previous result
  335. if (lPrev.TestID<>-1) and (lPrev.TestID<>lResult.TestID) then
  336. begin
  337. { This can only happen if a Successfully compiled message
  338. is not followed by any other line about the same test }
  339. SaveTestResult(lPrev);
  340. Verbose(V_Warning,'Orphaned test: "'+prevline+'"');
  341. end;
  342. // same test, so now we have run result
  343. lPrev.TestID:=-1;
  344. lResult.TestResult:=TS;
  345. If (lResult.TestID<>-1) then
  346. begin
  347. If Not (TestOK[TS] or TestSkipped[TS]) then
  348. begin
  349. lResult.Log:=GetExecuteLog(Fullline,Line);
  350. if pos(failed_to_compile,lResult.Log)=1 then
  351. lResult.Log:=GetLog(Fullline,Line);
  352. end
  353. else
  354. lResult.Log:='';
  355. if SaveTestResult(lResult) then
  356. Inc(aData.StatusCount[TS]);
  357. end;
  358. end
  359. end
  360. end;
  361. finally
  362. Logfile.Free;
  363. end;
  364. end;
  365. procedure TDBDigestAnalyzer.UpdateTestRun(const aData : TTestRunData);
  366. begin
  367. FDB.UpdateTestRun(aData);
  368. end;
  369. end.