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