Unit test Benchmark utility

This commit is contained in:
Aurora Lahtela 2023-01-21 12:04:58 +02:00
parent dda7199a1a
commit 1b0942c988
3 changed files with 123 additions and 3 deletions

View File

@ -24,6 +24,8 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author AuroraLS3
@ -33,7 +35,7 @@ public class Benchmark {
private static final Formatter<Long> FORMATTER = new BenchmarkFormatter();
private Benchmark() {
/* Only used for in-development benchmarks. */
/* Only used for in-development benchmarks. (static methods) */
}
public static void bench(Runnable runnable) {
@ -72,10 +74,9 @@ public class Benchmark {
}
builder.append(" - ").append(calledFrom);
System.out.print(builder.toString());
Logger.getLogger("Plan").log(Level.INFO, builder::toString);
}
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.TYPE})
public static @interface Slow {

View File

@ -0,0 +1,72 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.utilities.dev;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import utilities.LogStoringHandler;
import java.util.List;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test to increase coverage on new code and pass sonar.
*
* @author AuroraLS3
*/
class BenchmarkTest {
LogStoringHandler logHandler;
@BeforeEach
void registerLogHandler() {
logHandler = new LogStoringHandler();
Logger.getLogger("Plan").addHandler(logHandler);
}
@AfterEach
void clearLogHandler() {
logHandler.close();
}
@Test
void benchmarkOnRunnable() {
Benchmark.bench(() -> {});
List<String> records = logHandler.getRecords().stream()
.map(LogRecord::getMessage)
.toList();
assertEquals(1, records.size());
assertTrue(records.get(0).contains("woah") || records.get(0).contains("fast"));
}
@Test
void benchmarkOnSupplier() {
boolean result = Benchmark.bench(() -> true);
assertTrue(result);
List<String> records = logHandler.getRecords().stream()
.map(LogRecord::getMessage)
.toList();
assertEquals(1, records.size());
assertTrue(records.get(0).contains("woah") || records.get(0).contains("fast"));
}
}

View File

@ -0,0 +1,47 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package utilities;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
/**
* @author AuroraLS3
*/
public class LogStoringHandler extends Handler {
private final List<LogRecord> records = new ArrayList<>();
@Override
public void publish(LogRecord record) {
records.add(record);
}
@Override
public void flush() {
/* Nothing to flush */
}
@Override
public void close() {records.clear();}
public List<LogRecord> getRecords() {
return records;
}
}