View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.solr.index.statistic;
20  
21  import java.util.concurrent.atomic.AtomicInteger;
22  import java.util.concurrent.atomic.AtomicLong;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  
27  /**
28   * @author Thomas Scheffler (yagee)
29   *
30   */
31  public class MCRSolrIndexStatistic {
32  
33      private static final Logger LOGGER = LogManager.getLogger();
34  
35      final AtomicInteger documents;
36  
37      final AtomicLong accumulatedTime;
38  
39      String name;
40  
41      public MCRSolrIndexStatistic(String name) {
42          this.name = name;
43          this.documents = new AtomicInteger();
44          this.accumulatedTime = new AtomicLong();
45      }
46  
47      public long addTime(long time) {
48          LOGGER.debug("{}: adding {} ms", name, time);
49          return accumulatedTime.addAndGet(time);
50      }
51  
52      public int addDocument(int docs) {
53          LOGGER.debug("{}: adding {} documents", name, docs);
54          return documents.addAndGet(docs);
55      }
56  
57      public long getAccumulatedTime() {
58          return accumulatedTime.get();
59      }
60  
61      public int getDocuments() {
62          return documents.get();
63      }
64  
65      /**
66       * resets statistic and returns average time in ms per document.
67       */
68      public synchronized double reset() {
69          synchronized (accumulatedTime) {
70              synchronized (documents) {
71                  long time = accumulatedTime.getAndSet(0);
72                  int docs = documents.getAndSet(0);
73                  if (docs == 0) {
74                      return time == 0 ? 0 : Double.MAX_VALUE;
75                  }
76                  return ((double) time / (double) docs);
77              }
78          }
79      }
80  }