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

ben-manes / caffeine / #5173

29 Dec 2025 05:27AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5173

push

github

ben-manes
speed up development ci build

0 of 3838 branches covered (0.0%)

0 of 7869 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/jcache/src/main/java/com/github/benmanes/caffeine/jcache/management/JCacheStatisticsMXBean.java
1
/*
2
 * Copyright 2015 Ben Manes. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.github.benmanes.caffeine.jcache.management;
17

18
import java.util.concurrent.TimeUnit;
19
import java.util.concurrent.atomic.LongAdder;
20

21
import javax.cache.management.CacheStatisticsMXBean;
22

23
/**
24
 * Caffeine JCache statistics.
25
 *
26
 * @author ben.manes@gmail.com (Ben Manes)
27
 */
28
@SuppressWarnings("MemberName")
29
public final class JCacheStatisticsMXBean implements CacheStatisticsMXBean {
×
30
  private final LongAdder puts = new LongAdder();
×
31
  private final LongAdder hits = new LongAdder();
×
32
  private final LongAdder misses = new LongAdder();
×
33
  private final LongAdder removals = new LongAdder();
×
34
  private final LongAdder evictions = new LongAdder();
×
35
  private final LongAdder putTimeNanos = new LongAdder();
×
36
  private final LongAdder getTimeNanos = new LongAdder();
×
37
  private final LongAdder removeTimeNanos = new LongAdder();
×
38

39
  private volatile boolean enabled;
40

41
  /** Returns if statistic collection is enabled. */
42
  public boolean isEnabled() {
43
    return enabled;
×
44
  }
45

46
  /**
47
   * Sets whether the statistic collection is enabled.
48
   *
49
   * @param enabled whether to collect statistics
50
   */
51
  public void enable(boolean enabled) {
52
    this.enabled = enabled;
×
53
  }
×
54

55
  @Override
56
  public void clear() {
57
    puts.reset();
×
58
    misses.reset();
×
59
    removals.reset();
×
60
    hits.reset();
×
61
    evictions.reset();
×
62
    getTimeNanos.reset();
×
63
    putTimeNanos.reset();
×
64
    removeTimeNanos.reset();
×
65
  }
×
66

67
  @Override
68
  public long getCacheHits() {
69
    return hits.sum();
×
70
  }
71

72
  @Override
73
  public float getCacheHitPercentage() {
74
    long requestCount = getCacheGets();
×
75
    return (requestCount == 0) ? 0f : 100 * ((float) getCacheHits() / requestCount);
×
76
  }
77

78
  /**
79
   * Records cache hits. This should be called when a cache request returns a cached value.
80
   *
81
   * @param count the number of hits to record
82
   */
83
  public void recordHits(long count) {
84
    if (enabled) {
×
85
      hits.add(count);
×
86
    }
87
  }
×
88

89
  @Override
90
  public long getCacheMisses() {
91
    return misses.sum();
×
92
  }
93

94
  @Override
95
  public float getCacheMissPercentage() {
96
    long requestCount = getCacheGets();
×
97
    return (requestCount == 0) ? 0f : 100 * ((float) getCacheMisses() / requestCount);
×
98
  }
99

100
  /**
101
   * Records cache misses. This should be called when a cache request returns a value that was not
102
   * found in the cache.
103
   *
104
   * @param count the number of misses to record
105
   */
106
  public void recordMisses(long count) {
107
    if (enabled) {
×
108
      misses.add(count);
×
109
    }
110
  }
×
111

112
  @Override
113
  public long getCacheGets() {
114
    return getCacheHits() + getCacheMisses();
×
115
  }
116

117
  @Override
118
  public long getCachePuts() {
119
    return puts.sum();
×
120
  }
121

122
  /**
123
   * Records cache insertion and updates.
124
   *
125
   * @param count the number of writes to record
126
   */
127
  public void recordPuts(long count) {
128
    if (enabled && (count != 0)) {
×
129
      puts.add(count);
×
130
    }
131
  }
×
132

133
  @Override
134
  public long getCacheRemovals() {
135
    return removals.sum();
×
136
  }
137

138
  /**
139
   * Records cache removals.
140
   *
141
   * @param count the number of removals to record
142
   */
143
  public void recordRemovals(long count) {
144
    if (enabled) {
×
145
      removals.add(count);
×
146
    }
147
  }
×
148

149
  @Override
150
  public long getCacheEvictions() {
151
    return evictions.sum();
×
152
  }
153

154
  /**
155
   * Records cache evictions.
156
   *
157
   * @param count the number of evictions to record
158
   */
159
  public void recordEvictions(long count) {
160
    if (enabled) {
×
161
      evictions.add(count);
×
162
    }
163
  }
×
164

165
  @Override
166
  public float getAverageGetTime() {
167
    return average(getCacheGets(), getTimeNanos.sum());
×
168
  }
169

170
  /**
171
   * Records the time to execute get operations. This time does not include the time it takes to
172
   * load an entry on a cache miss, as specified by the specification.
173
   *
174
   * @param durationNanos the amount of time in nanoseconds
175
   */
176
  public void recordGetTime(long durationNanos) {
177
    if (enabled && (durationNanos != 0)) {
×
178
      getTimeNanos.add(durationNanos);
×
179
    }
180
  }
×
181

182
  @Override
183
  public float getAveragePutTime() {
184
    return average(getCachePuts(), putTimeNanos.sum());
×
185
  }
186

187
  /**
188
   * Records the time to execute put operations.
189
   *
190
   * @param durationNanos the amount of time in nanoseconds
191
   */
192
  public void recordPutTime(long durationNanos) {
193
    if (enabled && (durationNanos != 0)) {
×
194
      putTimeNanos.add(durationNanos);
×
195
    }
196
  }
×
197

198
  @Override
199
  public float getAverageRemoveTime() {
200
    return average(getCacheRemovals(), removeTimeNanos.sum());
×
201
  }
202

203
  /**
204
   * Records the time to execute remove operations.
205
   *
206
   * @param durationNanos the amount of time in nanoseconds
207
   */
208
  public void recordRemoveTime(long durationNanos) {
209
    if (enabled && (durationNanos != 0)) {
×
210
      removeTimeNanos.add(durationNanos);
×
211
    }
212
  }
×
213

214
  private static float average(long requestCount, long opsTimeNanos) {
215
    if ((requestCount == 0) || (opsTimeNanos == 0)) {
×
216
      return 0;
×
217
    }
218
    long opsTimeMicro = TimeUnit.NANOSECONDS.toMicros(opsTimeNanos);
×
219
    return (float) opsTimeMicro / requestCount;
×
220
  }
221
}
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

© 2026 Coveralls, Inc