1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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 }