groovy node-tree integration
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-data-impl / src / test / java / org / opendaylight / controller / yang / data / impl / MemoryConsumption.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.yang.data.impl;
10
11 /**
12  * Provides memory consumption and elapsed time between 2 points 
13  * @author mirehak
14  */
15 public class MemoryConsumption {
16     
17     private long memBegin;
18     private long tsBegin;
19
20     /**
21      * record memory and timestamp
22      */
23     public void startObserving() {
24         Runtime runtime = Runtime.getRuntime();
25         // Run the garbage collector
26         runtime.gc();
27         memBegin = getActualMemoryConsumption();
28         tsBegin = System.currentTimeMillis();
29     }
30     
31     
32     /**
33      * @return memory usage and time elapsed message
34      */
35     public String finishObserving() {
36         long memEnd = getActualMemoryConsumption();
37         long tsEnd = System.currentTimeMillis();
38         return String.format("Used memory: %10d B; Elapsed time: %5d ms", (memEnd - memBegin), (tsEnd - tsBegin));
39     }
40     
41     
42     /**
43      * @return actual memory usage
44      */
45     public static long getActualMemoryConsumption() {
46         Runtime runtime = Runtime.getRuntime();
47         // Calculate the used memory
48         long memory = runtime.totalMemory() - runtime.freeMemory();
49         return memory;
50     }
51     
52     
53 }