5
5
use Symfony \Component \Console \Input \InputInterface ;
6
6
use Symfony \Component \Console \Output \OutputInterface ;
7
7
use Symfony \Component \Console \Input \InputOption ;
8
- use Db3v4l \Util \Process ;
9
8
use Symfony \Component \Yaml \Yaml ;
9
+ use Db3v4l \API \Interfaces \TimedExecutor ;
10
10
use Db3v4l \Service \DatabaseConfigurationManager ;
11
11
use Db3v4l \Service \SqlExecutorFactory ;
12
12
use Db3v4l \Service \ProcessManager ;
13
+ use Db3v4l \Util \Process ;
13
14
14
15
class SqlExecute extends BaseCommand
15
16
{
@@ -83,21 +84,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
83
84
84
85
/** @var Process[] $processes */
85
86
$ processes = [];
86
- $ timingFiles = [];
87
+ $ executors = [];
87
88
foreach ($ dbList as $ dbName ) {
88
89
$ dbConnectionSpec = $ this ->dbManager ->getDatabaseConnectionSpecification ($ dbName );
89
- $ process = $ this ->executorFactory ->createForkedExecutor ($ dbConnectionSpec )->getProcess ($ sql );
90
90
91
- // wrap in a `time` call
92
- $ timingFile = tempnam (sys_get_temp_dir (), 'db3val_ ' );
93
- $ process ->setCommandLine (
94
- 'time ' . escapeshellarg ('--output= ' . $ timingFile ) . ' ' . escapeshellarg ('--format=%M %e ' ) . ' '
95
- . $ process ->getCommandLine ());
91
+ $ executor = $ this ->executorFactory ->createForkedExecutor ($ dbConnectionSpec );
92
+ $ process = $ executor ->getProcess ($ sql );
96
93
97
94
$ process ->setTimeout ($ timeout );
98
95
96
+ $ executors [$ dbName ] = $ executor ;
99
97
$ processes [$ dbName ] = $ process ;
100
- $ timingFiles [$ dbName ] = $ timingFile ;
101
98
}
102
99
103
100
if ($ format === 'text ' ) {
@@ -116,13 +113,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
116
113
'exitcode ' => $ process ->getExitCode ()
117
114
);
118
115
119
- $ timingData = file_get_contents ($ timingFiles [$ dbName ]);
120
- if ($ timingData != '' ) {
121
- $ timingData = explode (' ' , $ timingData , 2 );
122
- $ results [$ dbName ]['time ' ] = $ timingData [1 ];
123
- $ results [$ dbName ]['memory ' ] = $ timingData [0 ];
116
+ if ($ executors [$ dbName ] instanceof TimedExecutor) {
117
+ $ timingData = $ executors [$ dbName ]->getTimingData ();
118
+ $ results [$ dbName ] = array_merge ($ results [$ dbName ], $ timingData );
124
119
}
125
- unlink ($ timingFiles [$ dbName ]);
126
120
127
121
if ($ process ->isSuccessful ()) {
128
122
$ succeeded ++;
@@ -135,7 +129,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
135
129
$ time = microtime (true ) - $ start ;
136
130
137
131
$ this ->writeResults ($ results , $ succeeded , $ failed , $ time , $ format );
138
-
139
132
}
140
133
141
134
/**
@@ -147,13 +140,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
147
140
*/
148
141
protected function writeResults (array $ results , $ succeeded , $ failed , $ time , $ format = 'text ' )
149
142
{
150
- if ($ format === 'text ' ) {
151
- $ this ->writeln ($ succeeded . ' succeeded, ' . $ failed . ' failed ' );
152
-
153
- // since we use subprocesses, we can not measure max memory used
154
- $ this ->writeln ("<info>Time taken: " .sprintf ('%.2f ' , $ time )." secs</info> " );
155
- }
156
-
157
143
switch ($ format ) {
158
144
case 'json ' :
159
145
$ results = json_encode ($ results , JSON_PRETTY_PRINT );
@@ -170,8 +156,14 @@ protected function writeResults(array $results, $succeeded, $failed, $time, $for
170
156
throw new \Exception ("Unsupported output format: ' $ format' " );
171
157
break ;
172
158
}
173
-
174
159
$ this ->writeln ($ results , OutputInterface::VERBOSITY_QUIET , OutputInterface::OUTPUT_RAW );
160
+
161
+ if ($ format === 'text ' ) {
162
+ $ this ->writeln ($ succeeded . ' succeeded, ' . $ failed . ' failed ' );
163
+
164
+ // since we use subprocesses, we can not measure max memory used
165
+ $ this ->writeln ("<info>Time taken: " .sprintf ('%.2f ' , $ time )." secs</info> " );
166
+ }
175
167
}
176
168
177
169
/**
0 commit comments