aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/core/profiler/ProfilingNavigationHelperContentAdapter.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/core/profiler/ProfilingNavigationHelperContentAdapter.java')
-rw-r--r--subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/core/profiler/ProfilingNavigationHelperContentAdapter.java155
1 files changed, 155 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/core/profiler/ProfilingNavigationHelperContentAdapter.java b/subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/core/profiler/ProfilingNavigationHelperContentAdapter.java
new file mode 100644
index 00000000..3ab15430
--- /dev/null
+++ b/subprojects/viatra-runtime-base/src/main/java/tools/refinery/viatra/runtime/base/core/profiler/ProfilingNavigationHelperContentAdapter.java
@@ -0,0 +1,155 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2019, Laszlo Gati, Zoltan Ujhelyi, IncQuery Labs Ltd.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v. 2.0 which is available at
5 * http://www.eclipse.org/legal/epl-v20.html.
6 *
7 * SPDX-License-Identifier: EPL-2.0
8 *******************************************************************************/
9package tools.refinery.viatra.runtime.base.core.profiler;
10
11import org.eclipse.emf.common.notify.Notification;
12import org.eclipse.emf.common.notify.Notifier;
13import tools.refinery.viatra.runtime.base.core.NavigationHelperContentAdapter;
14import tools.refinery.viatra.runtime.base.core.NavigationHelperImpl;
15
16/**
17 *
18 * @noinstantiate This class is not intended to be instantiated by clients.
19 * @noreference This class is not intended to be referenced by clients.
20 */
21public final class ProfilingNavigationHelperContentAdapter extends NavigationHelperContentAdapter {
22
23 private static class StopWatch {
24
25 private long currentStartTimeNs = 0l;
26 private long totalElapsedTimeNs = 0l;
27 private boolean running = false;
28
29 /**
30 * Puts the timer in running state and saves the current time.
31 */
32 private void start() {
33 currentStartTimeNs = System.nanoTime();
34 running = true;
35
36 }
37
38 /**
39 * Puts the the timer in stopped state and saves the total time spent in started
40 * state between the last reset and now
41 */
42 private void stop() {
43 totalElapsedTimeNs = getTotalElapsedTimeNs();
44 running = false;
45 }
46
47 /**
48 * @return time between the last start and now
49 */
50 private long getCurrentElapsedTimeNs() {
51 return System.nanoTime() - currentStartTimeNs;
52 }
53
54 /**
55 * @return the total time spent in started state between the last reset and now
56 */
57 private long getTotalElapsedTimeNs() {
58 return running ? getCurrentElapsedTimeNs() + totalElapsedTimeNs : totalElapsedTimeNs;
59 }
60
61 /**
62 * Saves the current time and resets all the time spent between the last reset and now.
63 */
64 private void resetTime() {
65 currentStartTimeNs = System.currentTimeMillis();
66 totalElapsedTimeNs = 0;
67 }
68 }
69
70 long notificationCount = 0l;
71 StopWatch watch = new StopWatch();
72 boolean isEnabled = false;
73
74 boolean measurement = false;
75
76 public ProfilingNavigationHelperContentAdapter(NavigationHelperImpl navigationHelper, boolean enabled) {
77 super(navigationHelper);
78 this.isEnabled = enabled;
79 }
80
81 @Override
82 public void notifyChanged(Notification notification) {
83 // Handle possibility of reentrancy
84 if (isEnabled && !measurement) {
85 try {
86 measurement = true;
87 notificationCount++;
88 watch.start();
89 super.notifyChanged(notification);
90 } finally {
91 watch.stop();
92 measurement = false;
93 }
94 } else {
95 super.notifyChanged(notification);
96 }
97 }
98
99 @Override
100 public void setTarget(Notifier target) {
101 // Handle possibility of reentrancy
102 if (isEnabled && !measurement) {
103 try {
104 measurement = true;
105 notificationCount++;
106 watch.start();
107 super.setTarget(target);
108 } finally {
109 watch.stop();
110 measurement = false;
111 }
112 } else {
113 super.setTarget(target);
114 }
115 }
116
117 @Override
118 public void unsetTarget(Notifier target) {
119 // Handle possibility of reentrancy
120 if (isEnabled && !measurement) {
121 try {
122 measurement = true;
123 notificationCount++;
124 watch.start();
125 super.unsetTarget(target);
126 } finally {
127 watch.stop();
128 measurement = false;
129 }
130 } else {
131 super.unsetTarget(target);
132 }
133 }
134
135 public long getNotificationCount() {
136 return notificationCount;
137 }
138
139 public long getTotalMeasuredTimeInMS() {
140 return watch.getTotalElapsedTimeNs() / 1_000_000l;
141 }
142
143 public boolean isEnabled() {
144 return isEnabled;
145 }
146
147 public void setEnabled(boolean isEnabled) {
148 this.isEnabled = isEnabled;
149 }
150
151 public void resetMeasurement() {
152 notificationCount = 0;
153 watch.resetTime();
154 }
155} \ No newline at end of file