-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TimingCompilationTracer.java
198 lines (166 loc) · 4.74 KB
/
TimingCompilationTracer.java
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
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal.tracing;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.neo4j.cypher.internal.frontend.v3_3.phases.CompilationPhaseTracer;
public class TimingCompilationTracer implements CompilationTracer
{
public interface EventListener
{
void queryCompiled( QueryEvent event );
}
public interface QueryEvent
{
String query();
long nanoTime();
List<PhaseEvent> phases();
}
public interface PhaseEvent
{
CompilationPhaseTracer.CompilationPhase phase();
long nanoTime();
}
interface Clock
{
long nanoTime();
Clock SYSTEM = new Clock()
{
@Override
public long nanoTime()
{
return System.nanoTime();
}
};
}
private final Clock clock;
private final EventListener listener;
public TimingCompilationTracer( EventListener listener )
{
this( Clock.SYSTEM, listener );
}
TimingCompilationTracer( Clock clock, EventListener listener )
{
this.clock = clock;
this.listener = listener;
}
@Override
public QueryCompilationEvent compileQuery( String query )
{
return new Query( clock, query, listener );
}
private abstract static class Event implements AutoCloseable
{
private Clock clock;
private long time;
Event( Clock clock )
{
this.clock = clock;
this.time = clock.nanoTime();
}
@Override
public final void close()
{
if ( clock != null )
{
time = clock.nanoTime() - time;
clock = null;
done();
}
}
@SuppressWarnings( "UnusedDeclaration"/*used through inheritance*/ )
public final long nanoTime()
{
return time;
}
abstract void done();
final Clock clock()
{
if ( clock == null )
{
throw new IllegalStateException( this + " has been closed" );
}
return clock;
}
}
private static class Query extends Event implements QueryEvent, QueryCompilationEvent
{
private final String query;
private final EventListener listener;
private final List<Phase> phases = new ArrayList<>();
Query( Clock clock, String query, EventListener listener )
{
super( clock );
this.query = query;
this.listener = listener;
}
@Override
public String toString()
{
return getClass().getSimpleName() + "[" + query + "]";
}
@Override
public CompilationPhaseEvent beginPhase( CompilationPhase phase )
{
Phase event = new Phase( clock(), phase );
phases.add( event );
return event;
}
@Override
void done()
{
listener.queryCompiled( this );
}
@Override
public String query()
{
return query;
}
@Override
public List<PhaseEvent> phases()
{
return Collections.unmodifiableList( phases );
}
}
private static class Phase extends Event implements PhaseEvent, CompilationPhaseTracer.CompilationPhaseEvent
{
private final CompilationPhaseTracer.CompilationPhase phase;
Phase( Clock clock, CompilationPhaseTracer.CompilationPhase phase )
{
super( clock );
this.phase = phase;
}
@Override
public String toString()
{
return getClass().getSimpleName() + "[" + phase + "]";
}
@Override
public CompilationPhaseTracer.CompilationPhase phase()
{
return phase;
}
@Override
void done()
{
}
}
}