More performance tweaks for SpringCli
This commit is contained in:
parent
e1605b4691
commit
5b90e18564
|
|
@ -21,7 +21,9 @@ import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.springframework.boot.cli.command.AbstractCommand;
|
import org.springframework.boot.cli.command.AbstractCommand;
|
||||||
|
|
@ -54,6 +56,8 @@ public class SpringCli {
|
||||||
|
|
||||||
private InitCommand init;
|
private InitCommand init;
|
||||||
|
|
||||||
|
private Map<String, Command> commandMap = new HashMap<String, Command>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@link SpringCli} implementation with the default set of commands.
|
* Create a new {@link SpringCli} implementation with the default set of commands.
|
||||||
*/
|
*/
|
||||||
|
|
@ -65,8 +69,7 @@ public class SpringCli {
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new IllegalStateException("Cannot init with those args", e);
|
throw new IllegalStateException("Cannot init with those args", e);
|
||||||
}
|
}
|
||||||
this.commands.add(0, new HelpCommand());
|
addBaseCommands();
|
||||||
this.commands.add(new HintCommand());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public InitCommand getInitCommand() {
|
public InitCommand getInitCommand() {
|
||||||
|
|
@ -79,9 +82,21 @@ public class SpringCli {
|
||||||
* @param commands the commands to add
|
* @param commands the commands to add
|
||||||
*/
|
*/
|
||||||
public void setCommands(List<? extends Command> commands) {
|
public void setCommands(List<? extends Command> commands) {
|
||||||
|
this.commandMap.clear();
|
||||||
this.commands = new ArrayList<Command>(commands);
|
this.commands = new ArrayList<Command>(commands);
|
||||||
this.commands.add(0, new HelpCommand());
|
for (Command command : commands) {
|
||||||
this.commands.add(new HintCommand());
|
this.commandMap.put(command.getName(), command);
|
||||||
|
}
|
||||||
|
addBaseCommands();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void addBaseCommands() {
|
||||||
|
HelpCommand help = new HelpCommand();
|
||||||
|
this.commands.add(0, help);
|
||||||
|
this.commandMap.put(help.getName(), help);
|
||||||
|
HintCommand hint = new HintCommand();
|
||||||
|
this.commands.add(hint);
|
||||||
|
this.commandMap.put(hint.getName(), hint);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -161,27 +176,23 @@ public class SpringCli {
|
||||||
}
|
}
|
||||||
|
|
||||||
public final Command find(String name) {
|
public final Command find(String name) {
|
||||||
for (Command candidate : this.commands) {
|
return this.commandMap.get(name);
|
||||||
String candidateName = candidate.getName();
|
|
||||||
if (candidateName.equals(name)
|
|
||||||
|| (candidate.isOptionCommand() && ("--" + candidateName)
|
|
||||||
.equals(name))) {
|
|
||||||
return candidate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void register(Command command) {
|
public void register(Command command) {
|
||||||
Command existing = find(command.getName());
|
String name = command.getName();
|
||||||
int index = this.commands.indexOf(find("hint")) - 1;
|
Command existing = find(name);
|
||||||
|
int index = this.commands.size() - 1;
|
||||||
index = index >= 0 ? index : 0;
|
index = index >= 0 ? index : 0;
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
index = this.commands.indexOf(existing);
|
index = this.commands.indexOf(existing);
|
||||||
this.commands.remove(index);
|
this.commands.set(index, command);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
this.commands.add(index, command);
|
this.commands.add(index, command);
|
||||||
}
|
}
|
||||||
|
this.commandMap.put(name, command);
|
||||||
|
}
|
||||||
|
|
||||||
public void unregister(String name) {
|
public void unregister(String name) {
|
||||||
this.commands.remove(find(name));
|
this.commands.remove(find(name));
|
||||||
|
|
@ -412,14 +423,21 @@ public class SpringCli {
|
||||||
*/
|
*/
|
||||||
public static void main(String... args) {
|
public static void main(String... args) {
|
||||||
String[] init = new String[1];
|
String[] init = new String[1];
|
||||||
for (String arg : args) {
|
int index = 0;
|
||||||
|
String arg = args[0];
|
||||||
if (arg.startsWith("--init")) {
|
if (arg.startsWith("--init")) {
|
||||||
|
if (arg.contains("=") || args.length < 2) {
|
||||||
init[0] = arg;
|
init[0] = arg;
|
||||||
|
index = 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
init[0] = arg + "=" + args[1];
|
||||||
|
index = 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (init[0] != null) {
|
if (index > 0) {
|
||||||
String[] newargs = new String[args.length - 1];
|
String[] newargs = new String[args.length - index];
|
||||||
System.arraycopy(args, 1, newargs, 0, newargs.length);
|
System.arraycopy(args, index, newargs, 0, newargs.length);
|
||||||
args = newargs;
|
args = newargs;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -430,5 +448,4 @@ public class SpringCli {
|
||||||
System.exit(exitCode);
|
System.exit(exitCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,7 @@ public class SpringCliTests {
|
||||||
this.cli.register(this.anotherCommand);
|
this.cli.register(this.anotherCommand);
|
||||||
assertEquals(before + 1, this.cli.getCommands().size());
|
assertEquals(before + 1, this.cli.getCommands().size());
|
||||||
// Just before the hint command
|
// Just before the hint command
|
||||||
assertEquals(before - 2, this.cli.getCommands().indexOf(this.cli.find("another")));
|
assertEquals(before - 1, this.cli.getCommands().indexOf(this.cli.find("another")));
|
||||||
this.cli.unregister(this.anotherCommand.getName());
|
this.cli.unregister(this.anotherCommand.getName());
|
||||||
assertEquals(before, this.cli.getCommands().size());
|
assertEquals(before, this.cli.getCommands().size());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,6 @@ import org.springframework.boot.cli.Command;
|
||||||
import org.springframework.boot.cli.CommandFactory;
|
import org.springframework.boot.cli.CommandFactory;
|
||||||
import org.springframework.boot.cli.SpringCli;
|
import org.springframework.boot.cli.SpringCli;
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
|
|
@ -37,7 +35,6 @@ public class InitCommandPerformanceTests {
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public OutputCapture output = new OutputCapture();
|
public OutputCapture output = new OutputCapture();
|
||||||
private SpringCli cli = mock(SpringCli.class);
|
|
||||||
private ClassLoader classLoader;
|
private ClassLoader classLoader;
|
||||||
private Random random = new Random();
|
private Random random = new Random();
|
||||||
|
|
||||||
|
|
@ -54,7 +51,8 @@ public class InitCommandPerformanceTests {
|
||||||
@Test
|
@Test
|
||||||
public void initDefault() throws Exception {
|
public void initDefault() throws Exception {
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
InitCommand command = new InitCommand(this.cli);
|
SpringCli cli = new SpringCli();
|
||||||
|
InitCommand command = new InitCommand(cli);
|
||||||
command.run();
|
command.run();
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
@ -64,7 +62,8 @@ public class InitCommandPerformanceTests {
|
||||||
// Fast...
|
// Fast...
|
||||||
public void initNonExistent() throws Exception {
|
public void initNonExistent() throws Exception {
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
InitCommand command = new InitCommand(this.cli);
|
SpringCli cli = new SpringCli();
|
||||||
|
InitCommand command = new InitCommand(cli);
|
||||||
command.run("--init=" + this.random.nextInt() + ".groovy");
|
command.run("--init=" + this.random.nextInt() + ".groovy");
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
@ -73,6 +72,7 @@ public class InitCommandPerformanceTests {
|
||||||
@Test
|
@Test
|
||||||
// Fast...
|
// Fast...
|
||||||
public void fakeCommand() throws Exception {
|
public void fakeCommand() throws Exception {
|
||||||
|
final SpringCli cli = new SpringCli();
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
Command command = new AbstractCommand("fake", "") {
|
Command command = new AbstractCommand("fake", "") {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -80,9 +80,8 @@ public class InitCommandPerformanceTests {
|
||||||
for (CommandFactory factory : ServiceLoader.load(
|
for (CommandFactory factory : ServiceLoader.load(
|
||||||
CommandFactory.class, Thread.currentThread()
|
CommandFactory.class, Thread.currentThread()
|
||||||
.getContextClassLoader())) {
|
.getContextClassLoader())) {
|
||||||
for (Command command : factory
|
for (Command command : factory.getCommands(cli)) {
|
||||||
.getCommands(InitCommandPerformanceTests.this.cli)) {
|
cli.register(command);
|
||||||
InitCommandPerformanceTests.this.cli.register(command);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -96,7 +95,8 @@ public class InitCommandPerformanceTests {
|
||||||
// Fast...
|
// Fast...
|
||||||
public void initNonExistentWithPrefix() throws Exception {
|
public void initNonExistentWithPrefix() throws Exception {
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
InitCommand command = new InitCommand(this.cli);
|
SpringCli cli = new SpringCli();
|
||||||
|
InitCommand command = new InitCommand(cli);
|
||||||
command.run("--init=file:" + this.random.nextInt() + ".groovy");
|
command.run("--init=file:" + this.random.nextInt() + ".groovy");
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
@ -107,10 +107,15 @@ public class InitCommandPerformanceTests {
|
||||||
// Slow...
|
// Slow...
|
||||||
public void initFromClasspath() throws Exception {
|
public void initFromClasspath() throws Exception {
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
InitCommand command = new InitCommand(this.cli);
|
SpringCli cli = new SpringCli();
|
||||||
|
InitCommand command = new InitCommand(cli);
|
||||||
command.run("--init=init.groovy");
|
command.run("--init=init.groovy");
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringCli.main("hint");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue