• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/exist-core/src/main/java/org/exist/xquery/PerformanceStatsService.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.xquery;
25

26
import net.jcip.annotations.GuardedBy;
27
import net.jcip.annotations.ThreadSafe;
28
import org.exist.dom.QName;
29
import org.exist.dom.memtree.MemTreeBuilder;
30
import org.exist.storage.BrokerPoolService;
31
import org.exist.storage.BrokerPoolServiceException;
32
import org.exist.storage.lock.Lock;
33
import org.exist.storage.lock.ManagedLock;
34
import org.exist.util.Configuration;
35

36
import javax.annotation.Nullable;
37
import java.util.concurrent.locks.ReadWriteLock;
38
import java.util.concurrent.locks.ReentrantReadWriteLock;
39

40
import static org.exist.storage.lock.ManagedLock.acquire;
41

42
/**
43
 * Implementation of a PerformanceStats that is designed
44
 * to be used my multiple-threads as a Service from the BrokerPool.
45
 *
46
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
47
 */
48
@ThreadSafe
49
public class PerformanceStatsService implements BrokerPoolService, PerformanceStats {
1✔
50

51
    private @Nullable ReadWriteLock performanceStatsLock = null;  // access is guarded by volatile `performanceStats` field below
1✔
52

53
    @GuardedBy("performanceStatsLock")
54
    private volatile @Nullable PerformanceStats performanceStats = null;  // volatile access as it is lazy-initialised (or not) in {@link BrokerPoolService#configure()} by the system thread
1✔
55

56
    @Override
57
    public void configure(final Configuration configuration) throws BrokerPoolServiceException {
58
        final String xqueryProfilingTraceEnabled = (String) configuration.getProperty(PerformanceStatsImpl.CONFIG_PROPERTY_TRACE);
1✔
59
        if ("yes".equals(xqueryProfilingTraceEnabled) || "functions".equals(xqueryProfilingTraceEnabled)) {
1!
60
            init();
×
61
        }
62
    }
1✔
63

64
    private void init() {
65
        this.performanceStatsLock = new ReentrantReadWriteLock();
1✔
66
        this.performanceStats = new PerformanceStatsImpl(true);
1✔
67
    }
1✔
68

69
    @Override
70
    public boolean isEnabled() {
71
        if (performanceStats == null) {
1✔
72
            // not initialized or disabled
73
            return false;
1✔
74
        }
75

76
        try (final ManagedLock<ReadWriteLock> readLock = acquire(performanceStatsLock, Lock.LockMode.READ_LOCK)) {
1✔
77
            return performanceStats.isEnabled();
1✔
78
        }
79
    }
80

81
    @Override
82
    public void setEnabled(final boolean enabled) {
83
        if (performanceStats == null) {
1✔
84
            // not initialized or disabled
85
            if (enabled == true) {
1!
86
                init();
1✔
87
            }
88
            return;
1✔
89
        }
90

91
        try (final ManagedLock<ReadWriteLock> writeLock = acquire(performanceStatsLock, Lock.LockMode.WRITE_LOCK)) {
1✔
92
            performanceStats.setEnabled(enabled);
1✔
93
        }
94
    }
1✔
95

96
    @Override
97
    public void recordQuery(final String source, final long elapsed) {
98
        if (performanceStats == null) {
×
99
            // not initialized or disabled
100
            return;
×
101
        }
102

103
        try (final ManagedLock<ReadWriteLock> writeLock = acquire(performanceStatsLock, Lock.LockMode.WRITE_LOCK)) {
×
104
            performanceStats.recordQuery(source, elapsed);
×
105
        }
106
    }
×
107

108
    @Override
109
    public void recordFunctionCall(final QName qname, final String source, final long elapsed) {
110
        if (performanceStats == null) {
×
111
            // not initialized or disabled
112
            return;
×
113
        }
114

115
        try (final ManagedLock<ReadWriteLock> writeLock = acquire(performanceStatsLock, Lock.LockMode.WRITE_LOCK)) {
×
116
            performanceStats.recordFunctionCall(qname, source, elapsed);
×
117
        }
118
    }
×
119

120
    @Override
121
    public void recordIndexUse(final Expression expression, final String indexName, final String source, final IndexOptimizationLevel indexOptimizationLevel, final long elapsed) {
122
        if (performanceStats == null) {
×
123
            // not initialized or disabled
124
            return;
×
125
        }
126

127
        try (final ManagedLock<ReadWriteLock> writeLock = acquire(performanceStatsLock, Lock.LockMode.WRITE_LOCK)) {
×
128
            performanceStats.recordIndexUse(expression, indexName, source, indexOptimizationLevel, elapsed);
×
129
        }
130
    }
×
131

132
    @Override
133
    public void recordOptimization(final Expression expression, final PerformanceStatsImpl.OptimizationType type, final String source) {
134
        if (performanceStats == null) {
×
135
            // not initialized or disabled
136
            return;
×
137
        }
138

139
        try (final ManagedLock<ReadWriteLock> writeLock = acquire(performanceStatsLock, Lock.LockMode.WRITE_LOCK)) {
×
140
            performanceStats.recordOptimization(expression, type, source);
×
141
        }
142
    }
×
143

144
    @Override
145
    public void recordAll(final PerformanceStats otherPerformanceStats) {
146
        if (performanceStats == null) {
1!
147
            // not initialized or disabled
148
            return;
×
149
        }
150

151
        try (final ManagedLock<ReadWriteLock> writeLock = acquire(performanceStatsLock, Lock.LockMode.WRITE_LOCK)) {
1✔
152
            performanceStats.recordAll(otherPerformanceStats);
1✔
153
        }
154
    }
1✔
155

156
    @Override
157
    public void serialize(final MemTreeBuilder builder) {
158
        if (performanceStats == null) {
1!
159
            // not initialized or disabled
160
            builder.startElement(new QName(XML_ELEMENT_CALLS, XML_NAMESPACE, XML_PREFIX), null);
×
161
            builder.endElement();
×
162
            return;
×
163
        }
164

165
        try (final ManagedLock<ReadWriteLock> readLock = acquire(performanceStatsLock, Lock.LockMode.READ_LOCK)) {
1✔
166
            performanceStats.serialize(builder);
1✔
167
        }
168
    }
1✔
169

170
    @Override
171
    public void reset() {
172
        if (performanceStats == null) {
1✔
173
            // not initialized or disabled
174
            return;
1✔
175
        }
176

177
        try (final ManagedLock<ReadWriteLock> writeLock = acquire(performanceStatsLock, Lock.LockMode.WRITE_LOCK)) {
1✔
178
            performanceStats.reset();
1✔
179
        }
180
    }
1✔
181

182
    @Override
183
    public String toString() {
184
        if (performanceStats == null) {
×
185
            // not initialized or disabled
186
            return "";
×
187
        }
188

189
        try (final ManagedLock<ReadWriteLock> readLock = acquire(performanceStatsLock, Lock.LockMode.READ_LOCK)) {
×
190
            return performanceStats.toString();
×
191
        }
192
    }
193
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc