Skip to content

Commit 1b01959

Browse files
committed
parallelized execution update
1 parent fbaf318 commit 1b01959

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

covToDB-Directory.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require_once __DIR__ . '/vendor/autoload.php';
8+
9+
/**
10+
* The main entrypoint. Called at the bottom of this file.
11+
*/
12+
function main($baseCoverageDir, $runID, $testType) {
13+
readCoverageFromFolder($baseCoverageDir, $runID, $testType);
14+
}
15+
16+
/**
17+
* Reads each .cov file in a directory and inserts records into the db define din db.class.php
18+
*
19+
* @param string $coveragePath
20+
* @param string $runID
21+
* @return void
22+
*/
23+
function readCoverageFromFolder($coveragePath, $runID, $testType) {
24+
if (!realpath($coveragePath)) {
25+
printf("No coverage files found in $coveragePath\n");
26+
return;
27+
}
28+
$fileCount = count(scandir($coveragePath));
29+
$currentFile = 0;
30+
31+
foreach (scandir($coveragePath) as $file) {
32+
printf("Reading ($currentFile/$fileCount)\r");
33+
$currentFile += 1;
34+
if (pathinfo($file)['extension'] !== 'cov') {
35+
continue;
36+
}
37+
$output = shell_exec('php '.__DIR__.DIRECTORY_SEPARATOR."covToDB-Single.php {$coveragePath}/{$file} {$runID} {$testType}");
38+
printf($output."\n");
39+
}
40+
$start_time = microtime(TRUE);
41+
$endtime = microtime(TRUE);
42+
$runtime = $endtime-$start_time;
43+
printf("Inserted from all files in {$coveragePath}\n");
44+
printf("Execution took {$runtime} seconds.\n");
45+
}
46+
47+
/**
48+
* Reads a single .cov file into memory.
49+
*/
50+
function readCoverage($coveragePath) {
51+
if (!is_file($coveragePath)) {
52+
return null;
53+
}
54+
$file = include($coveragePath);
55+
return $file;
56+
}
57+
58+
if (!array_key_exists(3, $argv)) {
59+
printf("This script requires 3 parameters to run:\nInputDirectory RunID TestType");
60+
} else {
61+
main(filter_var($argv[1], FILTER_SANITIZE_STRING),filter_var($argv[2], FILTER_SANITIZE_STRING),filter_var($argv[3], FILTER_SANITIZE_STRING));
62+
}

covToDB-Single.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require_once __DIR__ . '/vendor/autoload.php';
8+
9+
/**
10+
* The main entrypoint. Called at the bottom of this file.
11+
*/
12+
function main($filepath, $runID, $testType) {
13+
readCoverageFromFolder($filepath, $runID, $testType);
14+
}
15+
16+
/**
17+
* Reads each .cov file in a directory and inserts records into the db define din db.class.php
18+
*
19+
* @param string $filepath
20+
* @param string $runID
21+
* @return void
22+
*/
23+
function readCoverageFromFolder($filepath, $runID, $testType) {
24+
require_once __DIR__ . DIRECTORY_SEPARATOR . 'db.class.php';
25+
$start_time = microtime(TRUE);
26+
//INPUT RUN IF NOT PRESENT
27+
$run = DB::query("SELECT * FROM CC_RUN WHERE runid=%s", $runID);
28+
if (empty($run)) {
29+
DB::insert("CC_RUN", [
30+
"runid" => $runID
31+
]);
32+
$cachedCCRUNID = DB::insertId();
33+
} else {
34+
$cachedCCRUNID = $run[0]['ccrunid'];
35+
}
36+
unset($run);
37+
38+
//CACHE LOCAL IDS FOR EASY USE
39+
$cachedTests = [];
40+
$tests = DB::query("SELECT * FROM CC_TESTS WHERE ccrunid=%s AND testtype=%s", $cachedCCRUNID, $testType);
41+
foreach ($tests as $test) {
42+
$cachedTests[$test['testname']] = $test['testid'];
43+
}
44+
unset($tests);
45+
$cachedFiles = [];
46+
$files = DB::query("SELECT * FROM CC_FILES WHERE ccrunid=%s", $cachedCCRUNID);
47+
foreach ($files as $file) {
48+
$cachedFiles[$file['filepath']] = $file['fileid'];
49+
}
50+
unset($files);
51+
52+
//ITERATE THROUGH FILES
53+
printf("Reading {$filepath}\r");
54+
$fileCoverage = readCoverage($filepath);
55+
56+
//TEST NAME INSERT
57+
foreach ($fileCoverage->getTests() as $testname => $content) {
58+
//found in initial cache
59+
if (isset($cachedTests[$testname])) {
60+
continue;
61+
}
62+
$existingTest = DB::query(
63+
"SELECT * FROM CC_TESTS WHERE ccrunid=%s AND testtype=%s AND testname=%s",
64+
$cachedCCRUNID, $testType, $testname
65+
);
66+
//not found in initial cache but found in DB
67+
if (!empty($existingTest)) {
68+
$cachedTests[$existingTest[0]['testname']] = $existingTest[0]['testid'];
69+
continue;
70+
}
71+
//actual new record
72+
DB::insert("CC_TESTS", [
73+
"testtype" => $testType,
74+
"testname" => $testname,
75+
"ccrunid" => $cachedCCRUNID
76+
]);
77+
$cachedTests[$testname] = DB::insertId();
78+
}
79+
80+
//DO FILE + LINE INSERTS
81+
foreach ($fileCoverage->getData(true)->lineCoverage() as $testFile => $content) {
82+
// not found in initial cache
83+
if (!isset($cachedFiles[$testFile])) {
84+
$existingFile = DB::query(
85+
"SELECT * FROM CC_FILES WHERE ccrunid=%s AND filepath=%s",
86+
$cachedCCRUNID, $testFile
87+
);
88+
// found in DB
89+
if (!empty($existingFile)) {
90+
$cachedFiles[$existingFile[0]['filepath']] = $existingFile[0]['fileid'];
91+
} else {
92+
// actual new record
93+
DB::insert("CC_FILES", [
94+
"filepath" => $testFile,
95+
"ccrunid" => $cachedCCRUNID
96+
]);
97+
$cachedFiles[$testFile] = DB::insertId();
98+
}
99+
}
100+
foreach ($content as $lineNumber => $tests) {
101+
foreach ($tests as $test) {
102+
$existingLine = DB::query(
103+
"SELECT * FROM CC_LINES WHERE linenumber=%s AND testid=%s AND fileid=%s AND ccrunid=%s",
104+
$lineNumber, $cachedTests[$test], $cachedFiles[$testFile], $cachedCCRUNID
105+
);
106+
if (empty($existingLine)) {
107+
DB::insert("CC_LINES", [
108+
"linenumber" => $lineNumber,
109+
"testid" => $cachedTests[$test],
110+
"fileid" => $cachedFiles[$testFile],
111+
"ccrunid" => $cachedCCRUNID
112+
]);
113+
}
114+
}
115+
}
116+
}
117+
printf("Inserted from file {$filepath}\n");
118+
119+
$endtime = microtime(TRUE);
120+
$runtime = $endtime-$start_time;
121+
printf("Execution took {$runtime} seconds.\n");
122+
}
123+
124+
/**
125+
* Reads a single .cov file into memory.
126+
*/
127+
function readCoverage($coveragePath) {
128+
if (!is_file($coveragePath)) {
129+
return null;
130+
}
131+
$file = include($coveragePath);
132+
return $file;
133+
}
134+
135+
if (!array_key_exists(3, $argv)) {
136+
printf("This script requires 3 parameters to run:\nFilePath RunID TestType");
137+
} else {
138+
main(filter_var($argv[1], FILTER_SANITIZE_STRING),filter_var($argv[2], FILTER_SANITIZE_STRING),filter_var($argv[3], FILTER_SANITIZE_STRING));
139+
}

0 commit comments

Comments
 (0)