summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/main/java/org/apache/qpid/server/stats/StatisticsCounter.java
blob: b7321211800c72e910846739b0c25e01c2cdd5e3 (plain)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.qpid.server.stats;

import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class collects statistics and counts the total, rate per second and
 * peak rate per second values for the events that are registered with it. 
 */
public class StatisticsCounter
{
    private static final Logger _log = LoggerFactory.getLogger(StatisticsCounter.class);
    
    public static final long DEFAULT_SAMPLE_PERIOD = Long.getLong("qpid.statistics.samplePeriod", 2000L); // 2s
    public static final boolean DISABLE_STATISTICS = Boolean.getBoolean("qpid.statistics.disable");
    
    private static final String COUNTER = "counter";
    private static final AtomicLong _counterIds = new AtomicLong(0L);
    
    private long _peak = 0L;
    private long _total = 0L;
    private long _temp = 0L;
    private long _last = 0L;
    private long _rate = 0L;

    private long _start;
    
    private final long _period;
    private final String _name;

    public StatisticsCounter()
    {
        this(COUNTER);
    }
    
    public StatisticsCounter(String name)
    {
        this(name, DEFAULT_SAMPLE_PERIOD);
    }

    public StatisticsCounter(String name, long period)
    {
        _period = period;
        _name = name + "-" + + _counterIds.incrementAndGet();
        reset();
    }
    
    public void registerEvent()
    {
        registerEvent(1L);
    }

    public void registerEvent(long value)
    {
        registerEvent(value, System.currentTimeMillis());
    }

    public void registerEvent(long value, long timestamp)
    {
        if (DISABLE_STATISTICS)
        {
            return;
        }
        
        long thisSample = (timestamp / _period);
        synchronized (this)
        {
            if (thisSample > _last)
            {
                _last = thisSample;
                _rate = _temp;
                _temp = 0L;
                if (_rate > _peak)
                {
                    _peak = _rate;
                }
            }
            
            _total += value;
            _temp += value;
        }
    }
    
    /**
     * Update the current rate and peak - may reset rate to zero if a new
     * sample period has started.
     */
    private void update()
    {
        registerEvent(0L, System.currentTimeMillis());
    }

    /**
     * Reset 
     */
    public void reset()
    {
        _log.info("Resetting statistics for counter: " + _name);
        _peak = 0L;
        _rate = 0L;
        _total = 0L;
        _start = System.currentTimeMillis();
        _last = _start / _period;
    }

    public double getPeak()
    {
        update();
        return (double) _peak / ((double) _period / 1000.0d);
    }

    public double getRate()
    {
        update();
        return (double) _rate / ((double) _period / 1000.0d);
    }

    public long getTotal()
    {
        return _total;
    }

    public long getStart()
    {
        return _start;
    }

    public Date getStartTime()
    {
        return new Date(_start);
    }
    
    public String getName()
    {
        return _name;
    }
    
    public long getPeriod()
    {
        return _period;
    }
}