This repository was archived by the owner on May 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathExecutionStatisticsBuilder.java
More file actions
188 lines (160 loc) · 6.29 KB
/
ExecutionStatisticsBuilder.java
File metadata and controls
188 lines (160 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2006-2012 The Scriptella Project Team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package scriptella.execution;
import scriptella.configuration.Location;
import scriptella.spi.Connection;
import java.util.ArrayList;
import java.util.Date;
/**
* A builder for execution statistics.
* <p>This class collects runtime ETL execution statistics, the usage contract is the following:</p>
* <ul>
* <li>{@link #etlStarted()} invoked on ETL start.
* <li>Call {@link #elementStarted(Location,Connection)} before executing an element.
* <li>Call {@link #elementExecuted()} or {@link #elementFailed()} after executing an element.
* <li>{@link #etlComplete()} invoked when ETL completes.
* <li>{@link #getStatistics() Obtain statistics} after ETL completes.
* </ul>
* <em>Notes:</em>
* <ul>
* <li>Start/End element callbacks sequence must comply with executing elements position in a XML file.
* <li>This class is not thread safe.
* </ul>
*
* @author Fyodor Kupolov
* @version 1.0
*/
public class ExecutionStatisticsBuilder {
private ExecutionStatistics executionStatistics;
//Execution stack for nested elements
protected ElementsStack executionStack = new ElementsStack();
//current element (shortcut for performance reasons)
protected ExecutionStatistics.ElementInfo currentEInfo;
/**
* Called when new element execution started.
*
* @param loc element location.
*/
public void elementStarted(final Location loc, Connection connection) {
currentEInfo = getInfo(loc);
currentEInfo.started = System.nanoTime();
// check, because script elements will call elementStarted every time in the query loop
if (currentEInfo.execStart == null){
currentEInfo.execStart = new Date();
}
executionStack.push(currentEInfo);
currentEInfo.statementsOnStart = connection.getExecutedStatementsCount();
currentEInfo.connection = connection;
}
public void incrementNestedExecutionCount(){
currentEInfo.nestedExecutionCount++;
}
public void incrementFilteredExecutionCount(final Location loc){
getInfo(loc).filteredExecutionCount++;
}
public void elementProcessingStarted(){
currentEInfo.processingStart = new Date();
}
/**
* This method is called when element has been executed.
*/
public void elementExecuted() {
setElementState(true);
}
/**
* Invoked on ETL start
*/
public void etlStarted() {
executionStatistics = new ExecutionStatistics();
executionStatistics.setStarted(new Date());
}
/**
* Invoked on ETL completion.
*/
public void etlComplete() {
if (executionStatistics == null) {
throw new IllegalStateException("etlStarted not called");
}
//assume that statistics is obtained immediately after the execution
executionStatistics.setFinished(new Date());
}
/**
* Calculates execution time and statements number for the completed element.
*/
private void setElementState(boolean ok) {
ExecutionStatistics.ElementInfo ended = executionStack.pop();
if (executionStack.size() > 0){
currentEInfo = executionStack.get(executionStack.size() -1);
}
ended.execEnd = new Date();
long ti = System.nanoTime() - ended.started;
ended.workingTime += ti; //increase the total working time for element
if (ended.workingTime < 0) {
ended.workingTime = 0;
}
final Connection con = ended.connection;
ended.connection = null; //clear the connection to avoid leaks
long conStatements = con.getExecutedStatementsCount();
long elStatements = conStatements - ended.statementsOnStart;
if (ok) {
ended.successfulExecutionCount++;
} else {
ended.failedExecutionCount++;
}
if (elStatements > 0) {
ended.statements += elStatements;
executionStatistics.statements += elStatements;
}
//Exclude this element time from parent elements
//Also find the parent elements with the same connection and decrement their number of statements
for (int i = executionStack.size() - 1; i >= 0; i--) {
final ExecutionStatistics.ElementInfo parent = executionStack.get(i);
parent.workingTime -= ti;
if (parent.connection == con) { //if the same objects
parent.statementsOnStart += elStatements;
}
}
}
public void elementFailed() {
setElementState(false);
}
private ExecutionStatistics.ElementInfo getInfo(final Location loc) {
if (executionStatistics == null) {
throw new IllegalStateException("etlStarted must be invoked prior to calling this method");
}
ExecutionStatistics.ElementInfo ei = executionStatistics.elements.get(loc.getXPath());
if (ei == null) {
ei = new ExecutionStatistics.ElementInfo();
ei.id = loc.getXPath();
executionStatistics.elements.put(loc.getXPath(), ei);
}
return ei;
}
public ExecutionStatistics getStatistics() {
return executionStatistics;
}
/**
* A non-synchronized faster replacement for {@link java.util.Stack}.
*/
static final class ElementsStack extends ArrayList<ExecutionStatistics.ElementInfo> {
public ExecutionStatistics.ElementInfo pop() {
return remove(size() - 1);
}
public void push(ExecutionStatistics.ElementInfo element) {
add(element);
}
}
}