001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.utils.stats;
020
021import java.util.*;
022
023import org.jppf.utils.*;
024
025
026/**
027 * This helper class holds the constants definitions for the labels
028 * of the statistics snapshots used in JPPF, along with utility methods.
029 * @author Laurent Cohen
030 */
031public final class JPPFStatisticsHelper {
032  /**
033   * Location of the localization resource bundles.
034   */
035  private  static final String I18N_BASE = "org.jppf.utils.stats.i18n.StatsLabels";
036  /**
037   * Count of tasks dispatched to nodes.
038   */
039  public static final String TASK_DISPATCH = "task.dispatch";
040  /**
041   * Execution times including server/nodes transport overhead.
042   */
043  public static final String EXECUTION = "execution";
044  /**
045   * Execution times in the nodes.
046   */
047  public static final String NODE_EXECUTION = "node.execution";
048  /**
049   * JPPF and network transport overhead.
050   */
051  public static final String TRANSPORT_TIME = "transport.time";
052  /**
053   * Total queued tasks.
054   */
055  public static final String TASK_QUEUE_TOTAL = "task.queue.total";
056  /**
057   * Queue tasks count.
058   */
059  public static final String TASK_QUEUE_COUNT = "task.queue.count";
060  /**
061   * Queue tasks times.
062   */
063  public static final String TASK_QUEUE_TIME = "task.queue.time";
064  /**
065   * Total number of submitted jobs.
066   */
067  public static final String JOB_TOTAL = "job.total";
068  /**
069   * Jobs counters.
070   */
071  public static final String JOB_COUNT = "job.count";
072  /**
073   * Jobs times.
074   */
075  public static final String JOB_TIME = "job.time";
076  /**
077   * Number of tasks in jobs.
078   */
079  public static final String JOB_TASKS = "job.tasks";
080  /**
081   * Number of connected nodes.
082   */
083  public static final String NODES = "nodes";
084  /**
085   * Number of idle connected nodes.
086   */
087  public static final String IDLE_NODES = "idle.nodes";
088  /**
089   * Number of client connections.
090   */
091  public static final String CLIENTS = "clients";
092  /**
093   * Time for class loading requests from nodes to complete.
094   */
095  public static final String NODE_CLASS_REQUESTS_TIME = "node.class.requests.time";
096  /**
097   * Time for class loading requests from nodes to complete.
098   */
099  public static final String CLIENT_CLASS_REQUESTS_TIME = "client.class.requests.time";
100  /**
101   * Bytes received from remote nodes.
102   */
103  public static final String NODE_IN_TRAFFIC = "node.traffic.in";
104  /**
105   * Bytes sent to remote nodes.
106   */
107  public static final String NODE_OUT_TRAFFIC = "node.traffic.out";
108  /**
109   * Bytes received from remote clients.
110   */
111  public static final String CLIENT_IN_TRAFFIC = "client.traffic.in";
112  /**
113   * Bytes sent to remote clients.
114   */
115  public static final String CLIENT_OUT_TRAFFIC = "client.traffic.out";
116  /**
117   * Bytes received from remote peer servers.
118   */
119  public static final String PEER_IN_TRAFFIC = "peer.traffic.in";
120  /**
121   * Bytes sent to remote peer servers.
122   */
123  public static final String PEER_OUT_TRAFFIC = "peer.traffic.out";
124  /**
125   * Bytes received from unidentified remote peers.
126   */
127  public static final String UNIDENTIFIED_IN_TRAFFIC = "unidentified.traffic.in";
128  /**
129   * Bytes sent to unidentified remote peers.
130   */
131  public static final String UNIDENTIFIED_OUT_TRAFFIC = "unidentified.traffic.out";
132
133  /**
134   * Determine wether the specified snapshot is a single value snapshot.
135   * @param snapshot the snapshot to evaluate.
136   * @return {@code true} if the snapshot is a single value snapshot, {@code false} otherwise.
137   */
138  public static boolean isSingleValue(final JPPFSnapshot snapshot) {
139    return snapshot instanceof SingleValueSnapshot;
140  }
141
142  /**
143   * Determine wether the specified snapshot is a cumulative snapshot.
144   * @param snapshot the snapshot to evaluate.
145   * @return {@code true} if the snapshot is a cumulative snapshot, {@code false} otherwise.
146   */
147  public static boolean isCumulative(final JPPFSnapshot snapshot) {
148    return snapshot instanceof CumulativeSnapshot;
149  }
150
151  /**
152   * Determine wether the specified snapshot is a non-cumulative snapshot.
153   * @param snapshot the snapshot to evaluate.
154   * @return {@code true} if the snapshot is a non-cumulative snapshot, {@code false} otherwise.
155   */
156  public static boolean isNonCumulative(final JPPFSnapshot snapshot) {
157    return snapshot instanceof NonCumulativeSnapshot;
158  }
159
160  /**
161   * Get the localized translation of the label of the specified snapshot in the current locale.
162   * @param snapshot the snapshot whose label to translate.
163   * @return a translation of the label, or the label itself if no translation could be found.
164   */
165  public static String getLocalizedLabel(final JPPFSnapshot snapshot) {
166    return getLocalizedLabel(snapshot, Locale.getDefault());
167  }
168
169  /**
170   * Get the localized translation of the label of the specified snapshot in the specified locale.
171   * @param snapshot the snapshot whose label to translate.
172   * @param locale the locale in which to translate.
173   * @return a translation of the label, or the label itself if no translation could be found.
174   */
175  public static String getLocalizedLabel(final JPPFSnapshot snapshot, final Locale locale) {
176    String label = snapshot.getLabel();
177    return LocalizationUtils.getLocalized(I18N_BASE, label, label, locale);
178  }
179
180  /**
181   * Add or update the values of the specified snapshot as properties in the specified set of properties.
182   * @param statsProperties the set of properties to update.
183   * @param snapshot the snapshot to update from.
184   */
185  public static void toProperties(final TypedProperties statsProperties, final JPPFSnapshot snapshot) {
186    final String label = snapshot.getLabel() + '.';
187    statsProperties.setDouble(label + "total", snapshot.getTotal());
188    if (snapshot.getClass() != SingleValueSnapshot.class) {
189      statsProperties.setDouble(label + "latest", snapshot.getLatest());
190      statsProperties.setDouble(label + "min", snapshot.getMin());
191      statsProperties.setDouble(label + "max", snapshot.getMax());
192      statsProperties.setDouble(label + "avg", snapshot.getAvg());
193      statsProperties.setDouble(label + "count", snapshot.getValueCount());
194    }
195  }
196
197  /**
198   * Create a statistics object initialized with all the required server snapshots.
199   * @return a {@link JPPFStatistics} instance.
200   * @exclude
201   */
202  public static JPPFStatistics createServerStatistics() {
203    JPPFStatistics statistics = new JPPFStatistics();
204    Iterator<JPPFFilteredStatisticsListener> it = ServiceFinder.lookupProviders(JPPFFilteredStatisticsListener.class);
205    while (it.hasNext()) {
206      JPPFFilteredStatisticsListener listener = it.next();
207      statistics.addListener(listener, listener.getFilter());
208    }
209    statistics.createSnapshots(false, EXECUTION, NODE_EXECUTION, TRANSPORT_TIME, TASK_QUEUE_TIME, JOB_TIME, JOB_TASKS, TASK_DISPATCH,
210        NODE_CLASS_REQUESTS_TIME, CLIENT_CLASS_REQUESTS_TIME);
211    statistics.createSnapshots(true, TASK_QUEUE_COUNT, JOB_COUNT, NODES, IDLE_NODES, CLIENTS);
212    statistics.createSingleValueSnapshots(TASK_QUEUE_TOTAL, JOB_TOTAL, NODE_IN_TRAFFIC, NODE_OUT_TRAFFIC, CLIENT_IN_TRAFFIC, CLIENT_OUT_TRAFFIC,
213        PEER_IN_TRAFFIC, PEER_OUT_TRAFFIC, UNIDENTIFIED_IN_TRAFFIC, UNIDENTIFIED_OUT_TRAFFIC);
214    return statistics;
215  }
216}