1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.appstatus.core.check.impl;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Properties;
21
22 import net.sf.appstatus.core.AppStatus;
23 import net.sf.appstatus.core.check.AbstractCheck;
24 import net.sf.appstatus.core.check.IAppStatusAware;
25 import net.sf.appstatus.core.check.ICheckResult;
26 import net.sf.appstatus.core.services.IService;
27
28 import org.apache.commons.lang3.StringUtils;
29
30
31
32
33
34 public class ServicesPerformanceCheck extends AbstractCheck implements IAppStatusAware {
35
36 private AppStatus appStatus;
37 private int delay = 10;
38 private int limitError = 3000;
39 private int limitWarn = 1000;
40
41 @Override
42 public ICheckResult checkStatus() {
43 List<IService> services = appStatus.getServiceManager().getServices();
44 List<String> warns = new ArrayList<String>();
45 List<String> errors = new ArrayList<String>();
46
47 for (IService s : services) {
48
49 if (this.delay > s.getHits()) {
50 continue;
51 }
52
53 if (s.getAvgResponseTime() > limitError || s.getAvgResponseTimeWithCache() > limitError) {
54 errors.add("Service <b>" + s.getGroup() + "#" + s.getName() + "</b> average response time ("
55 + Math.round(s.getAvgResponseTime()) + "ms without cache / "
56 + Math.round(s.getAvgNestedCallsWithCache()) + "ms with cache) is over error limit ("
57 + limitError + "ms)");
58 } else if (s.getAvgResponseTime() > limitWarn || s.getAvgResponseTimeWithCache() > limitWarn) {
59 warns.add("Service <b>" + s.getGroup() + "#" + s.getName() + "</b> average response time ("
60 + Math.round(s.getAvgResponseTime()) + "ms without cache /"
61 + Math.round(s.getAvgNestedCallsWithCache()) + "ms with cache) is over warn limit ("
62 + limitWarn + "ms)");
63 }
64 }
65
66 ICheckResult result = null;
67 if (errors.size() > 0) {
68 String description = StringUtils.join(errors, "<br/>");
69 if (warns.size() > 0) {
70 description = description + " <br/>Additional warnings: " + StringUtils.join(warns, "<br/>");
71 }
72 result = result(this).code(ICheckResult.ERROR).fatal().description(description).build();
73 } else if (warns.size() > 0) {
74 result = result(this).code(ICheckResult.ERROR).description(StringUtils.join(warns, "<br/>")).build();
75 } else {
76 result = result(this).code(ICheckResult.OK).description("All average times under " + limitWarn + "ms")
77 .build();
78 }
79 return result;
80 }
81
82 public String getGroup() {
83 return "Services";
84 }
85
86 public String getName() {
87 return "Performance";
88 }
89
90 public void setAppStatus(AppStatus appStatus) {
91 this.appStatus = appStatus;
92 }
93
94 @Override
95 public void setConfiguration(Properties configuration) {
96 super.setConfiguration(configuration);
97
98 String error = getConfiguration().getProperty("servicePerformanceCheck.limitError");
99 if (error != null) {
100 limitError = Integer.valueOf(error);
101 }
102
103 String warn = getConfiguration().getProperty("servicePerformanceCheck.limitWarn");
104 if (warn != null) {
105 limitWarn = Integer.valueOf(warn);
106 }
107
108 String delay = getConfiguration().getProperty("servicePerformanceCheck.delay");
109 if (delay != null) {
110 this.delay = Integer.valueOf(delay);
111 }
112 }
113 }