Package: DefaultStatisticsCollector$Sample
DefaultStatisticsCollector$Sample
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultStatisticsCollector.Sample(DefaultStatisticsCollector) |
|
|
|
|
|
||||||||||||||||||||
getCpuTime() |
|
|
|
|
|
||||||||||||||||||||
getElapsedTime() |
|
|
|
|
|
||||||||||||||||||||
getUserTime() |
|
|
|
|
|
||||||||||||||||||||
stop() |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.profiling.impl;
28:
29: import java.lang.management.ManagementFactory;
30: import java.lang.management.ThreadMXBean;
31: import javax.annotation.Nonnull;
32: import org.springframework.beans.factory.annotation.Configurable;
33: import it.tidalwave.northernwind.core.model.Request;
34: import it.tidalwave.northernwind.profiling.StatisticsCollector;
35: import lombok.Getter;
36: import lombok.extern.slf4j.Slf4j;
37:
38: /***********************************************************************************************************************
39: *
40: * @author Fabrizio Giudici
41: *
42: **********************************************************************************************************************/
43: @Configurable @Slf4j
44: public class DefaultStatisticsCollector implements StatisticsCollector
45: {
46: class Sample
47: {
48: private final long elapsedBaseTime = System.nanoTime();
49:
50: private final long cpuBaseTime = threadMxBean.getCurrentThreadCpuTime();
51:
52: private final long userBaseTime = threadMxBean.getCurrentThreadUserTime();
53:
54: @Getter
55: private long elapsedTime;
56:
57: @Getter
58: private long cpuTime;
59:
60: @Getter
61: private long userTime;
62:
63: public void stop()
64: {
65: elapsedTime = System.nanoTime() - elapsedBaseTime;
66: cpuTime = threadMxBean.getCurrentThreadCpuTime() - cpuBaseTime;
67: userTime = threadMxBean.getCurrentThreadUserTime() - userBaseTime;
68: }
69: }
70:
71: private final Stats elapsedTimeStats = new Stats("REQUEST ELAPSED TIME", 1E-6);
72:
73: private final Stats cpuTimeStats = new Stats("REQUEST CPU TIME ", 1E-6);
74:
75: private final Stats userTimeStats = new Stats("REQUEST USER TIME ", 1E-6);
76:
77: private final ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean( );
78:
79: private final ThreadLocal<Sample> sampleHolder = new ThreadLocal<>();
80:
81: @Override
82: public void onRequestBegin (@Nonnull final Request request)
83: {
84: sampleHolder.set(new Sample());
85: }
86:
87: @Override
88: public void onRequestEnd (@Nonnull final Request request)
89: {
90: final var sample = sampleHolder.get();
91: sample.stop();
92: sampleHolder.remove();
93: log.info(">>>> {} completed in {} msec", request, sample.getElapsedTime() * 1E-6);
94:
95: synchronized (this)
96: {
97: elapsedTimeStats.addValue(sample.getElapsedTime());
98: cpuTimeStats.addValue(sample.getCpuTime());
99: userTimeStats.addValue(sample.getUserTime());
100: }
101: }
102:
103: @SuppressWarnings("squid:S1192")
104: public void dumpStatistics()
105: {
106: synchronized (this)
107: {
108: log.info("STATS {}", elapsedTimeStats.globalAsString());
109: log.info("STATS {}", elapsedTimeStats.recentAsString());
110: log.info("STATS {}", cpuTimeStats.globalAsString());
111: log.info("STATS {}", cpuTimeStats.recentAsString());
112: log.info("STATS {}", userTimeStats.globalAsString());
113: log.info("STATS {}", userTimeStats.recentAsString());
114: elapsedTimeStats.clearRecent();
115: cpuTimeStats.clearRecent();
116: userTimeStats.clearRecent();
117: }
118: }
119: }