2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the 'License');
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an 'AS IS' BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.integration;
23 import org.springframework.util.StopWatch;
26 * Time and memory stop watch, exposing total running time and memory used.
28 public class ResourceMeter {
29 private final StopWatch stopWatch = new StopWatch();
30 private long memoryUsedBefore;
31 private long memoryUsedAfter;
38 memoryUsedBefore = getCurrentMemoryUsage();
47 memoryUsedAfter = getCurrentMemoryUsage();
51 * Get the total time in seconds.
52 * @return total time in seconds
54 public double getTotalTimeInSeconds() {
55 return stopWatch.getTotalTimeSeconds();
59 * Get the total memory used in megabytes.
60 * @return total memory used in megabytes
62 public double getTotalMemoryUsageInMB() {
63 return (memoryUsedAfter - memoryUsedBefore) / 1_000_000.0;
66 private static long getCurrentMemoryUsage() {
67 return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();