]> git.basschouten.com Git - openhab-addons.git/blob
45146decf1674416dd1e786ab3b89201137a77fd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.transport.modbus.internal;
14
15 import java.util.UUID;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * Utility for timing operations
21  *
22  * @author Sami Salonen - initial contribution
23  *
24  */
25 @NonNullByDefault
26 public class AggregateStopWatch {
27     /**
28      * ID associated with this modbus operation
29      */
30     final String operationId;
31
32     /**
33      * Total operation time
34      */
35     final SimpleStopWatch total = new SimpleStopWatch();
36
37     /**
38      * Time for connection related actions
39      */
40     final SimpleStopWatch connection = new SimpleStopWatch();
41
42     /**
43      * Time for actual the actual transaction (read/write to slave)
44      */
45     final SimpleStopWatch transaction = new SimpleStopWatch();
46
47     /**
48      * Time for calling calling the callback
49      */
50     final SimpleStopWatch callback = new SimpleStopWatch();
51
52     public AggregateStopWatch() {
53         this.operationId = UUID.randomUUID().toString();
54     }
55
56     /**
57      * Suspend all running stopwatches of this aggregate
58      */
59     public void suspendAllRunning() {
60         for (SimpleStopWatch watch : new SimpleStopWatch[] { total, connection, transaction, callback }) {
61             if (watch.isRunning()) {
62                 watch.suspend();
63             }
64         }
65     }
66
67     @Override
68     public String toString() {
69         return String.format("{total: %d ms, connection: %d, transaction=%d, callback=%d}", total.getTotalTimeMillis(),
70                 connection.getTotalTimeMillis(), transaction.getTotalTimeMillis(), callback.getTotalTimeMillis());
71     }
72 }