001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.examples.group.utils.timer; 020 021import java.util.logging.Level; 022import java.util.logging.Logger; 023 024/** 025 * Timer. 026 */ 027public class Timer implements AutoCloseable { 028 029 private static final Logger LOG = Logger.getLogger(Timer.class.getName()); 030 031 public static final int MINUTES = 60 * 1000; // ms 032 public static final int HOURS = 60 * MINUTES; 033 034 private final Logger log; 035 private final Level level; 036 private final String description; 037 private final long timeStart; 038 039 public Timer(final Logger log, final String description) { 040 this(log, Level.INFO, description); 041 } 042 043 public Timer(final String description) { 044 this(LOG, Level.INFO, description); 045 } 046 047 public Timer(final Logger log, final Level level, final String description) { 048 this.log = log; 049 this.level = level; 050 this.description = description; 051 this.timeStart = System.currentTimeMillis(); 052 this.log.log(this.level, "TIMER Start: {0}", this.description); 053 } 054 055 @Override 056 public void close() { 057 final long timeEnd = System.currentTimeMillis(); 058 this.log.log(this.level, "TIMER End: {0} took {1} sec.", 059 new Object[]{this.description, (timeEnd - this.timeStart) / 1000.0}); 060 } 061}