Skip to content

Commit

Permalink
fix: extend QuarkusCommand to fix help generation
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Oct 27, 2023
1 parent 2441d82 commit 870c06b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ To use the extension:
6. You should have a new `power` command available, type `power -h` for more information
7. You can start power measurement with `power start` and stop it with `power stop`, at which time the power consumption
of your app will be displayed.
8. You can also ask for power to be measured for a given duration by using the `--duration` option when
8. You can also ask for power to be measured for a given duration by using the `-s` option when
calling `power start`. In this case, there's no need to call `power stop`, the energy consumed during the specified
time will be automatically displayed once the time period is elapsed.
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package io.quarkiverse.power.deployment.devui.commands;

import org.aesh.command.Command;
import org.aesh.command.CommandDefinition;
import org.aesh.command.CommandException;
import org.aesh.command.CommandResult;
import org.aesh.command.invocation.CommandInvocation;
import org.aesh.command.option.Option;

import io.quarkiverse.power.runtime.PowerSensor;
import io.quarkiverse.power.runtime.sensors.macos.powermetrics.MacOSPowermetricsSensor;
import io.quarkus.deployment.console.QuarkusCommand;

@CommandDefinition(name = "start", description = "Starts measuring power consumption of the current application", generateHelp = true)
@CommandDefinition(name = "start", description = "Starts measuring power consumption of the current application")
@SuppressWarnings("rawtypes")
public class StartCommand implements Command {
public class StartCommand extends QuarkusCommand {

// @Inject
PowerSensor sensor = MacOSPowermetricsSensor.instance;

@Option(name = "duration", shortName = 'd', description = "The duration during which measurements should be taken before automatically stopping, in seconds", defaultValue = "-1")
@Option(name = "stopAfter", shortName = 's', description = "Automatically stop the measures after the specified duration in seconds", defaultValue = "-1")
private long duration;

@Option(name = "frequency", shortName = 'f', description = "The frequency at which measurements should be taken, in milliseconds", defaultValue = "1000")
private long frequency;

@Override
public CommandResult execute(CommandInvocation commandInvocation) {
public CommandResult doExecute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try {
if (duration > 0) {
commandInvocation.println("Measuring power for " + duration + " seconds, every " + frequency + " milliseconds");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package io.quarkiverse.power.deployment.devui.commands;

import org.aesh.command.Command;
import org.aesh.command.CommandDefinition;
import org.aesh.command.CommandException;
import org.aesh.command.CommandResult;
import org.aesh.command.invocation.CommandInvocation;

import io.quarkiverse.power.runtime.PowerSensor;
import io.quarkiverse.power.runtime.sensors.macos.powermetrics.MacOSPowermetricsSensor;
import io.quarkus.deployment.console.QuarkusCommand;

@CommandDefinition(name = "stop", description = "Stops power measurement and outputs accumulated power since measures were started", generateHelp = true)
@CommandDefinition(name = "stop", description = "Stops power measurement and outputs accumulated power since measures were started")
@SuppressWarnings("rawtypes")
public class StopCommand implements Command {
public class StopCommand extends QuarkusCommand {

// @Inject
PowerSensor sensor = MacOSPowermetricsSensor.instance;

@Override
public CommandResult execute(CommandInvocation commandInvocation) {
public CommandResult doExecute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
sensor.stop();
sensor.outputConsumptionSinceStarted(commandInvocation::println);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void start(long duration, long frequency, Writer out) throws IOException,
}
}

void extractPowerMeasure(InputStream powerMeasureInput) {
void extractPowerMeasure(InputStream powerMeasureInput) {
try {
// Should not be closed since it closes the process
BufferedReader input = new BufferedReader(new InputStreamReader(powerMeasureInput));
Expand All @@ -77,7 +77,7 @@ void extractPowerMeasure(InputStream powerMeasureInput) {
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
}

private static double extractAttributedMeasure(String line, double attributionRatio) {
final var powerValue = line.split(":")[1];
Expand Down

0 comments on commit 870c06b

Please sign in to comment.