001package net.tnemc.plugincore.core.utils; 002 003/* 004 * The New Plugin Core 005 * Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU Affero General Public License as published by 009 * the Free Software Foundation, either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU Affero General Public License for more details. 016 * 017 * You should have received a copy of the GNU Affero General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020 021 022import net.tnemc.plugincore.PluginCore; 023import net.tnemc.plugincore.core.compatibility.log.DebugLevel; 024 025/** 026 * Timings 027 * 028 * @author creatorfromhell 029 * @since 0.1.2.0 030 */ 031public class Timings implements AutoCloseable { 032 033 private String statement = "Timings: "; 034 private long start; 035 private long end; 036 037 /** 038 * Starts our timings in order to measure duration of methods or actions. 039 * 040 * @return The timings object. 041 */ 042 public Timings start() { 043 044 start = System.nanoTime(); 045 return this; 046 } 047 048 /** 049 * Used to build our timings with a statement for logging purposes. 050 * 051 * @param statement The statement to use. 052 * 053 * @return The timings instance. 054 */ 055 public Timings withStatement(String statement) { 056 057 this.statement = statement; 058 return this; 059 } 060 061 /** 062 * Stops the timings and returns the duration. 063 * 064 * @return Return the duration this timing lasted. 065 */ 066 public long stop() { 067 068 this.end = System.nanoTime(); 069 return (end - start); 070 } 071 072 /** 073 * Stops the timings and logs it to the console and server log. 074 * 075 * @param level The DebugLevel to use for this. 076 */ 077 public void stopLog(DebugLevel level) { 078 079 this.end = System.nanoTime(); 080 PluginCore.log().debug(statement + (end - start), level); 081 } 082 083 @Override 084 public void close() throws Exception { 085 086 stopLog(DebugLevel.DETAILED); 087 } 088}