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

aspectran / aspectran / #4059

13 Feb 2025 08:38AM CUT coverage: 35.283% (+0.002%) from 35.281%
#4059

push

github

topframe
Update

0 of 17 new or added lines in 2 files covered. (0.0%)

2 existing lines in 2 files now uncovered.

14258 of 40410 relevant lines covered (35.28%)

0.35 hits per line

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

0.0
/utils/src/main/java/com/aspectran/utils/statistic/SampleStatistic.java
1
/*
2
 * Copyright (c) 2008-2025 The Aspectran Project
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.aspectran.utils.statistic;
17

18
import com.aspectran.utils.ObjectUtils;
19
import com.aspectran.utils.ToStringBuilder;
20

21
import java.util.concurrent.atomic.AtomicLong;
22
import java.util.concurrent.atomic.LongAccumulator;
23
import java.util.concurrent.atomic.LongAdder;
24

25
/**
26
 * <p>This class is a clone of org.eclipse.jetty.util.statistic.SampleStatistic</p>
27
 *
28
 * <p>Statistics on a sampled value.</p>
29
 * <p>Provides max, total, mean, count, variance, and standard deviation of continuous sequence of samples.</p>
30
 * <p>Calculates estimates of mean, variance, and standard deviation characteristics of a sample using a non synchronized
31
 * approximation of the on-line algorithm presented in <cite>Donald Knuth's Art of Computer Programming, Volume 2,
32
 * Semi numerical Algorithms, 3rd edition, page 232, Boston: Addison-Wesley</cite>. That cites a 1962 paper by B.P. Welford:
33
 * <a href="http://www.jstor.org/pss/1266577">Note on a Method for Calculating Corrected Sums of Squares and Products</a></p>
34
 * <p>This algorithm is also described in Wikipedia in the section "Online algorithm":
35
 * <a href="https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance">Algorithms for calculating variance</a>.</p>
36
 */
37
public class SampleStatistic {
×
38

39
    private final LongAccumulator max = new LongAccumulator(Math::max,0L);
×
40

41
    private final AtomicLong total = new AtomicLong();
×
42

43
    private final AtomicLong count = new AtomicLong();
×
44

45
    private final LongAdder totalVariance100 = new LongAdder();
×
46

47
    /**
48
     * Resets the statistics.
49
     */
50
    public void reset() {
51
        max.reset();
×
52
        total.set(0);
×
53
        count.set(0);
×
54
        totalVariance100.reset();
×
55
    }
×
56

57
    /**
58
     * Records a sample value.
59
     * @param sample the value to record.
60
     */
61
    public void record(long sample) {
UNCOV
62
        long total = this.total.addAndGet(sample);
×
63
        long count = this.count.incrementAndGet();
×
64
        if (count > 1) {
×
65
            long mean10 = total * 10 / count;
×
66
            long delta10 = sample * 10 - mean10;
×
67
            totalVariance100.add(delta10 * delta10);
×
68
        }
69
        max.accumulate(sample);
×
70
    }
×
71

72
    /**
73
     * @return the max value of the recorded samples
74
     */
75
    public long getMax() {
76
        return max.get();
×
77
    }
78

79
    /**
80
     * @return the sum of all the recorded samples
81
     */
82
    public long getTotal() {
83
        return total.get();
×
84
    }
85

86
    /**
87
     * @return the number of samples recorded
88
     */
89
    public long getCount() {
90
        return count.get();
×
91
    }
92

93
    /**
94
     * @return the average value of the samples recorded, or zero if there are no samples
95
     */
96
    public double getMean() {
97
        long count = getCount();
×
98
        return (count > 0 ? (double)this.total.get() / this.count.get() : 0.0D);
×
99
    }
100

101
    /**
102
     * @return the variance of the samples recorded, or zero if there are less than 2 samples
103
     */
104
    public double getVariance() {
105
        long variance100 = totalVariance100.sum();
×
106
        long count = getCount();
×
107
        return (count > 1 ? variance100 / 100.0D / (count - 1) : 0.0D);
×
108
    }
109

110
    /**
111
     * @return the standard deviation of the samples recorded
112
     */
113
    public double getStdDev() {
114
        return Math.sqrt(getVariance());
×
115
    }
116

117
    @Override
118
    public String toString() {
119
        ToStringBuilder tsb = new ToStringBuilder(ObjectUtils.simpleIdentityToString(this));
×
120
        tsb.append("max", getMax());
×
121
        tsb.append("total", getTotal());
×
122
        tsb.append("count", getCount());
×
123
        tsb.append("mean", getMean());
×
124
        tsb.append("stddev", getStdDev());
×
125
        return tsb.toString();
×
126
    }
127

128
}
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