diff --git a/src/main/java/de/kosit/validationtool/cmd/CommandLineApplication.java b/src/main/java/de/kosit/validationtool/cmd/CommandLineApplication.java index eb89024..c232ef6 100644 --- a/src/main/java/de/kosit/validationtool/cmd/CommandLineApplication.java +++ b/src/main/java/de/kosit/validationtool/cmd/CommandLineApplication.java @@ -31,7 +31,13 @@ import java.util.Collections; import java.util.List; import java.util.stream.Collectors; -import org.apache.commons.cli.*; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; import org.apache.commons.lang3.StringUtils; import lombok.extern.slf4j.Slf4j; @@ -72,6 +78,8 @@ public class CommandLineApplication { private static final Option CHECK_ASSERTIONS = Option.builder("c").longOpt("check-assertions").hasArg() .desc("Check the result using defined assertions").argName("assertions-file").build(); + private static final Option PRINT_MEM_STATS = Option.builder("m").longOpt("memory-stats").desc("Prints some memory stats").build(); + private CommandLineApplication() { // main class -> hide constructor } @@ -146,6 +154,9 @@ public class CommandLineApplication { Assertions assertions = loadAssertions(cmd.getOptionValue(CHECK_ASSERTIONS.getOpt())); check.getCheckSteps().add(new CheckAssertionAction(assertions, ObjectFactory.createProcessor())); } + if (cmd.hasOption(PRINT_MEM_STATS.getOpt())) { + check.getCheckSteps().add(new PrintMemoryStats()); + } log.info("Setup completed in {}ms\n", System.currentTimeMillis() - start); @@ -285,6 +296,7 @@ public class CommandLineApplication { options.addOption(EXTRACT_HTML); options.addOption(DEBUG); options.addOption(CHECK_ASSERTIONS); + options.addOption(PRINT_MEM_STATS); return options; } } diff --git a/src/main/java/de/kosit/validationtool/cmd/PrintMemoryStats.java b/src/main/java/de/kosit/validationtool/cmd/PrintMemoryStats.java new file mode 100644 index 0000000..e0973d7 --- /dev/null +++ b/src/main/java/de/kosit/validationtool/cmd/PrintMemoryStats.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Koordinierungsstelle für IT-Standards (KoSIT) under + * one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. KoSIT licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package de.kosit.validationtool.cmd; + +import java.text.NumberFormat; + +import lombok.extern.slf4j.Slf4j; + +/** + * @author Andreas Penski + */ +@Slf4j +public class PrintMemoryStats implements de.kosit.validationtool.impl.tasks.CheckAction { + + @Override + public void check(final Bag results) { + Runtime.getRuntime().gc(); + final Runtime runtime = Runtime.getRuntime(); + StringBuilder sb = new StringBuilder(); + long maxMemory = runtime.maxMemory(); + long allocatedMemory = runtime.totalMemory(); + long freeMemory = runtime.freeMemory(); + NumberFormat format = NumberFormat.getInstance(); + + sb.append("free memory: " + format.format(freeMemory / 1024)); + sb.append("MB allocated memory: " + format.format(allocatedMemory / 1024)); + sb.append("MB max memory: " + format.format(maxMemory / 1024)); + sb.append("MB total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024) + "MB"); + log.info("{}", sb.toString()); + } +}