Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
1ac995e0
Commit
1ac995e0
authored
Sep 04, 2014
by
Scott Vivian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add query logging function to Usage, refactor qa-db.php
parent
d63ef701
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
42 deletions
+44
-42
Usage.php
qa-include/Q2A/Util/Usage.php
+36
-21
qa-db.php
qa-include/qa-db.php
+8
-21
No files found.
qa-include/Q2A/Util/Usage.php
View file @
1ac995e0
...
...
@@ -11,21 +11,22 @@
class
Q2A_Util_Usage
{
public
$database_usage
;
public
$database_queries
;
public
$usage_start
;
public
$usage_last
;
private
$stages
;
private
$startUsage
;
private
$prevUsage
;
private
$databaseUsage
;
private
$databaseQueryLog
;
/**
* Initialize the counts of resource usage
*/
public
function
__construct
()
{
global
$qa_database_usage
,
$qa_database_queries
,
$qa_usage_start
,
$qa_usage_last
;
$this
->
stages
=
array
();
$this
->
databaseUsage
=
array
(
'queries'
=>
0
,
'clock'
=>
0
);
$this
->
databaseQueryLog
=
''
;
$qa_database_usage
=
array
(
'queries'
=>
0
,
'clock'
=>
0
);
$qa_database_queries
=
''
;
$qa_usage_last
=
$qa_usage_start
=
$this
->
getCurrent
();
$this
->
prevUsage
=
$this
->
startUsage
=
$this
->
getCurrent
();
}
/**
...
...
@@ -33,14 +34,12 @@ class Q2A_Util_Usage
*/
public
function
getCurrent
()
{
global
$qa_database_usage
;
$usage
=
array
(
'files'
=>
count
(
get_included_files
()),
'queries'
=>
$
qa_database_u
sage
[
'queries'
],
'queries'
=>
$
this
->
databaseU
sage
[
'queries'
],
'ram'
=>
function_exists
(
'memory_get_usage'
)
?
memory_get_usage
()
:
0
,
'clock'
=>
array_sum
(
explode
(
' '
,
microtime
())),
'mysql'
=>
$
qa_database_u
sage
[
'clock'
],
'mysql'
=>
$
this
->
databaseU
sage
[
'clock'
],
);
if
(
function_exists
(
'getrusage'
))
{
...
...
@@ -61,11 +60,29 @@ class Q2A_Util_Usage
*/
public
function
mark
(
$stage
)
{
global
$qa_usage_last
,
$qa_usage_stages
;
$usage
=
$this
->
getCurrent
();
$qa_usage_stages
[
$stage
]
=
$this
->
delta
(
$qa_usage_last
,
$usage
);
$qa_usage_last
=
$usage
;
$this
->
stages
[
$stage
]
=
$this
->
delta
(
$this
->
prevUsage
,
$usage
);
$this
->
prevUsage
=
$usage
;
}
/**
* Logs query and updates database usage stats
*/
public
function
logDatabaseQuery
(
$query
,
$usedtime
,
$gotrows
,
$gotcolumns
)
{
$this
->
databaseUsage
[
'clock'
]
+=
$usedtime
;
if
(
strlen
(
$this
->
databaseQueryLog
)
<
1048576
)
{
// don't keep track of big tests
$rowcolstring
=
''
;
if
(
is_numeric
(
$gotrows
))
$rowcolstring
.=
' - '
.
$gotrows
.
(
$gotrows
==
1
?
' row'
:
' rows'
);
if
(
is_numeric
(
$gotcolumns
))
$rowcolstring
.=
' - '
.
$gotcolumns
.
(
$gotcolumns
==
1
?
' column'
:
' columns'
);
$this
->
databaseQueryLog
.=
$query
.
"
\n\n
"
.
sprintf
(
'%.2f ms'
,
$usedtime
*
1000
)
.
$rowcolstring
.
"
\n\n
"
;
}
$this
->
databaseUsage
[
'queries'
]
++
;
}
/**
...
...
@@ -73,9 +90,7 @@ class Q2A_Util_Usage
*/
public
function
output
()
{
global
$qa_usage_start
,
$qa_usage_stages
,
$qa_database_queries
;
$totaldelta
=
$this
->
delta
(
$qa_usage_start
,
$this
->
getCurrent
());
$totaldelta
=
$this
->
delta
(
$this
->
startUsage
,
$this
->
getCurrent
());
?>
<style>
.debug-table
{
border-collapse
:
collapse
;
box-sizing
:
border-box
;
width
:
100%
;
margin
:
20px
auto
;
}
...
...
@@ -93,7 +108,7 @@ class Q2A_Util_Usage
<tr>
<td
colspan=
"2"
>
<?php
echo
$this
->
line
(
'Total'
,
$totaldelta
,
$totaldelta
)
.
"<br>
\n
"
;
foreach
(
$
qa_usage_
stages
as
$stage
=>
$stagedelta
)
foreach
(
$
this
->
stages
as
$stage
=>
$stagedelta
)
echo
'<br>'
.
$this
->
line
(
ucfirst
(
$stage
),
$stagedelta
,
$totaldelta
)
.
"
\n
"
;
?>
</td>
</tr>
...
...
@@ -105,7 +120,7 @@ class Q2A_Util_Usage
?>
</textarea>
</td>
<td
class=
"debug-cell-queries"
>
<textarea
class=
"debug-output"
cols=
"40"
rows=
"20"
>
<?=
qa_html
(
$
qa_database_queries
)
?>
</textarea>
<textarea
class=
"debug-output"
cols=
"40"
rows=
"20"
>
<?=
qa_html
(
$
this
->
databaseQueryLog
)
?>
</textarea>
</td>
</tr>
</tbody>
...
...
qa-include/qa-db.php
View file @
1ac995e0
...
...
@@ -162,34 +162,21 @@
if
(
qa_to_override
(
__FUNCTION__
))
{
$args
=
func_get_args
();
return
qa_call_override
(
__FUNCTION__
,
$args
);
}
if
(
QA_DEBUG_PERFORMANCE
)
{
global
$qa_
database_usage
,
$qa_database_queries
;
global
$qa_
usage
;
// time the query
$oldtime
=
array_sum
(
explode
(
' '
,
microtime
()));
$result
=
qa_db_query_execute
(
$query
);
$usedtime
=
array_sum
(
explode
(
' '
,
microtime
()))
-
$oldtime
;
if
(
is_array
(
$qa_database_usage
))
{
$qa_database_usage
[
'clock'
]
+=
$usedtime
;
if
(
strlen
(
$qa_database_queries
)
<
1048576
)
{
// don't keep track of big tests
$gotrows
=
$gotcolumns
=
null
;
if
(
$result
instanceof
mysqli_result
)
{
$gotrows
=
$result
->
num_rows
;
$gotcolumns
=
$result
->
field_count
;
}
$rowcolstring
=
''
;
if
(
is_numeric
(
$gotrows
))
$rowcolstring
.=
' - '
.
$gotrows
.
(
$gotrows
==
1
?
' row'
:
' rows'
);
if
(
is_numeric
(
$gotcolumns
))
$rowcolstring
.=
' - '
.
$gotcolumns
.
(
$gotcolumns
==
1
?
' column'
:
' columns'
);
$qa_database_queries
.=
$query
.
"
\n\n
"
.
sprintf
(
'%.2f ms'
,
$usedtime
*
1000
)
.
$rowcolstring
.
"
\n\n
"
;
}
$qa_database_usage
[
'queries'
]
++
;
// fetch counts
$gotrows
=
$gotcolumns
=
null
;
if
(
$result
instanceof
mysqli_result
)
{
$gotrows
=
$result
->
num_rows
;
$gotcolumns
=
$result
->
field_count
;
}
$qa_usage
->
logDatabaseQuery
(
$query
,
$usedtime
,
$gotrows
,
$gotcolumns
);
}
else
$result
=
qa_db_query_execute
(
$query
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment