View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client;
28  
29  import java.util.concurrent.atomic.AtomicLong;
30  
31  /**
32   * Collection of different counters used to gather metrics for {@link FutureRequestExecutionService}.
33   */
34  public final class FutureRequestExecutionMetrics {
35  
36      private final AtomicLong activeConnections = new AtomicLong();
37      private final AtomicLong scheduledConnections = new AtomicLong();
38      private final DurationCounter successfulConnections = new DurationCounter();
39      private final DurationCounter failedConnections = new DurationCounter();
40      private final DurationCounter requests = new DurationCounter();
41      private final DurationCounter tasks = new DurationCounter();
42  
43      FutureRequestExecutionMetrics() {
44      }
45  
46      AtomicLong getActiveConnections() {
47          return activeConnections;
48      }
49  
50      AtomicLong getScheduledConnections() {
51          return scheduledConnections;
52      }
53  
54      DurationCounter getSuccessfulConnections() {
55          return successfulConnections;
56      }
57  
58      DurationCounter getFailedConnections() {
59          return failedConnections;
60      }
61  
62      DurationCounter getRequests() {
63          return requests;
64      }
65  
66      DurationCounter getTasks() {
67          return tasks;
68      }
69  
70      public long getActiveConnectionCount() {
71          return activeConnections.get();
72      }
73  
74      public long getScheduledConnectionCount() {
75          return scheduledConnections.get();
76      }
77  
78      public long getSuccessfulConnectionCount() {
79          return successfulConnections.count();
80      }
81  
82      public long getSuccessfulConnectionAverageDuration() {
83          return successfulConnections.averageDuration();
84      }
85  
86      public long getFailedConnectionCount() {
87          return failedConnections.count();
88      }
89  
90      public long getFailedConnectionAverageDuration() {
91          return failedConnections.averageDuration();
92      }
93  
94      public long getRequestCount() {
95          return requests.count();
96      }
97  
98      public long getRequestAverageDuration() {
99          return requests.averageDuration();
100     }
101 
102     public long getTaskCount() {
103         return tasks.count();
104     }
105 
106     public long getTaskAverageDuration() {
107         return tasks.averageDuration();
108     }
109 
110     @Override
111     public String toString() {
112         final StringBuilder builder = new StringBuilder();
113         builder.append("[activeConnections=").append(activeConnections)
114                 .append(", scheduledConnections=").append(scheduledConnections)
115                 .append(", successfulConnections=").append(successfulConnections)
116                 .append(", failedConnections=").append(failedConnections)
117                 .append(", requests=").append(requests)
118                 .append(", tasks=").append(tasks)
119                 .append("]");
120         return builder.toString();
121     }
122 
123     /**
124      * A counter that can measure duration and number of events.
125      */
126     static class DurationCounter {
127 
128         private final AtomicLong count = new AtomicLong(0);
129         private final AtomicLong cumulativeDuration = new AtomicLong(0);
130 
131         public void increment(final long startTime) {
132             count.incrementAndGet();
133             cumulativeDuration.addAndGet(System.currentTimeMillis() - startTime);
134         }
135 
136         public long count() {
137             return count.get();
138         }
139 
140         public long averageDuration() {
141             final long counter = count.get();
142             return counter > 0 ? cumulativeDuration.get() / counter : 0;
143         }
144 
145         @Override
146         public String toString() {
147             final StringBuilder builder = new StringBuilder();
148             builder.append("[count=").append(count())
149                     .append(", averageDuration=").append(averageDuration())
150                     .append("]");
151             return builder.toString();
152         }
153 
154     }
155 
156 }