YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / 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 package org.opendaylight.yangtools.yang.data.impl;
9
10 /**
11  * Provides memory consumption and elapsed time between 2 points.
12  *
13  * @author mirehak
14  */
15 public class MemoryConsumption {
16     private long memBegin;
17     private long tsBegin;
18
19     /**
20      * Record memory and timestamp.
21      */
22     public void startObserving() {
23         Runtime runtime = Runtime.getRuntime();
24         // Run the garbage collector
25         runtime.gc();
26         memBegin = getActualMemoryConsumption();
27         tsBegin = System.currentTimeMillis();
28     }
29
30     /**
31      * Return memory usage and elapsed time message.
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      * Return used memory.
43      *
44      * @return actual memory usage
45      */
46     public static long getActualMemoryConsumption() {
47         Runtime runtime = Runtime.getRuntime();
48         // Calculate the used memory
49         long memory = runtime.totalMemory() - runtime.freeMemory();
50         return memory;
51     }
52 }