Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
5b90e185
Commit
5b90e185
authored
Jan 14, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More performance tweaks for SpringCli
parent
e1605b46
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
34 deletions
+56
-34
SpringCli.java
...src/main/java/org/springframework/boot/cli/SpringCli.java
+40
-23
SpringCliTests.java
...est/java/org/springframework/boot/cli/SpringCliTests.java
+1
-1
InitCommandPerformanceTests.java
...amework/boot/cli/command/InitCommandPerformanceTests.java
+15
-10
No files found.
spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java
View file @
5b90e185
...
...
@@ -21,7 +21,9 @@ import java.util.Arrays;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.EnumSet
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
org.springframework.boot.cli.command.AbstractCommand
;
...
...
@@ -54,6 +56,8 @@ public class SpringCli {
private
InitCommand
init
;
private
Map
<
String
,
Command
>
commandMap
=
new
HashMap
<
String
,
Command
>();
/**
* Create a new {@link SpringCli} implementation with the default set of commands.
*/
...
...
@@ -65,8 +69,7 @@ public class SpringCli {
catch
(
Exception
e
)
{
throw
new
IllegalStateException
(
"Cannot init with those args"
,
e
);
}
this
.
commands
.
add
(
0
,
new
HelpCommand
());
this
.
commands
.
add
(
new
HintCommand
());
addBaseCommands
();
}
public
InitCommand
getInitCommand
()
{
...
...
@@ -79,9 +82,21 @@ public class SpringCli {
* @param commands the commands to add
*/
public
void
setCommands
(
List
<?
extends
Command
>
commands
)
{
this
.
commandMap
.
clear
();
this
.
commands
=
new
ArrayList
<
Command
>(
commands
);
this
.
commands
.
add
(
0
,
new
HelpCommand
());
this
.
commands
.
add
(
new
HintCommand
());
for
(
Command
command
:
commands
)
{
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,26 +176,22 @@ public class SpringCli {
}
public
final
Command
find
(
String
name
)
{
for
(
Command
candidate
:
this
.
commands
)
{
String
candidateName
=
candidate
.
getName
();
if
(
candidateName
.
equals
(
name
)
||
(
candidate
.
isOptionCommand
()
&&
(
"--"
+
candidateName
)
.
equals
(
name
)))
{
return
candidate
;
}
}
return
null
;
return
this
.
commandMap
.
get
(
name
);
}
public
void
register
(
Command
command
)
{
Command
existing
=
find
(
command
.
getName
());
int
index
=
this
.
commands
.
indexOf
(
find
(
"hint"
))
-
1
;
String
name
=
command
.
getName
();
Command
existing
=
find
(
name
);
int
index
=
this
.
commands
.
size
()
-
1
;
index
=
index
>=
0
?
index
:
0
;
if
(
existing
!=
null
)
{
index
=
this
.
commands
.
indexOf
(
existing
);
this
.
commands
.
remove
(
index
);
this
.
commands
.
set
(
index
,
command
);
}
this
.
commands
.
add
(
index
,
command
);
else
{
this
.
commands
.
add
(
index
,
command
);
}
this
.
commandMap
.
put
(
name
,
command
);
}
public
void
unregister
(
String
name
)
{
...
...
@@ -412,14 +423,21 @@ public class SpringCli {
*/
public
static
void
main
(
String
...
args
)
{
String
[]
init
=
new
String
[
1
];
for
(
String
arg
:
args
)
{
if
(
arg
.
startsWith
(
"--init"
))
{
int
index
=
0
;
String
arg
=
args
[
0
];
if
(
arg
.
startsWith
(
"--init"
))
{
if
(
arg
.
contains
(
"="
)
||
args
.
length
<
2
)
{
init
[
0
]
=
arg
;
index
=
1
;
}
else
{
init
[
0
]
=
arg
+
"="
+
args
[
1
];
index
=
2
;
}
}
if
(
in
it
[
0
]
!=
null
)
{
String
[]
newargs
=
new
String
[
args
.
length
-
1
];
System
.
arraycopy
(
args
,
1
,
newargs
,
0
,
newargs
.
length
);
if
(
in
dex
>
0
)
{
String
[]
newargs
=
new
String
[
args
.
length
-
index
];
System
.
arraycopy
(
args
,
index
,
newargs
,
0
,
newargs
.
length
);
args
=
newargs
;
}
else
{
...
...
@@ -430,5 +448,4 @@ public class SpringCli {
System
.
exit
(
exitCode
);
}
}
}
spring-boot-cli/src/test/java/org/springframework/boot/cli/SpringCliTests.java
View file @
5b90e185
...
...
@@ -117,7 +117,7 @@ public class SpringCliTests {
this
.
cli
.
register
(
this
.
anotherCommand
);
assertEquals
(
before
+
1
,
this
.
cli
.
getCommands
().
size
());
// 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
());
assertEquals
(
before
,
this
.
cli
.
getCommands
().
size
());
}
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandPerformanceTests.java
View file @
5b90e185
...
...
@@ -28,8 +28,6 @@ import org.springframework.boot.cli.Command;
import
org.springframework.boot.cli.CommandFactory
;
import
org.springframework.boot.cli.SpringCli
;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* @author Dave Syer
*/
...
...
@@ -37,7 +35,6 @@ public class InitCommandPerformanceTests {
@Rule
public
OutputCapture
output
=
new
OutputCapture
();
private
SpringCli
cli
=
mock
(
SpringCli
.
class
);
private
ClassLoader
classLoader
;
private
Random
random
=
new
Random
();
...
...
@@ -54,7 +51,8 @@ public class InitCommandPerformanceTests {
@Test
public
void
initDefault
()
throws
Exception
{
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
InitCommand
command
=
new
InitCommand
(
this
.
cli
);
SpringCli
cli
=
new
SpringCli
();
InitCommand
command
=
new
InitCommand
(
cli
);
command
.
run
();
close
();
}
...
...
@@ -64,7 +62,8 @@ public class InitCommandPerformanceTests {
// Fast...
public
void
initNonExistent
()
throws
Exception
{
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"
);
close
();
}
...
...
@@ -73,6 +72,7 @@ public class InitCommandPerformanceTests {
@Test
// Fast...
public
void
fakeCommand
()
throws
Exception
{
final
SpringCli
cli
=
new
SpringCli
();
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
Command
command
=
new
AbstractCommand
(
"fake"
,
""
)
{
@Override
...
...
@@ -80,9 +80,8 @@ public class InitCommandPerformanceTests {
for
(
CommandFactory
factory
:
ServiceLoader
.
load
(
CommandFactory
.
class
,
Thread
.
currentThread
()
.
getContextClassLoader
()))
{
for
(
Command
command
:
factory
.
getCommands
(
InitCommandPerformanceTests
.
this
.
cli
))
{
InitCommandPerformanceTests
.
this
.
cli
.
register
(
command
);
for
(
Command
command
:
factory
.
getCommands
(
cli
))
{
cli
.
register
(
command
);
}
}
}
...
...
@@ -96,7 +95,8 @@ public class InitCommandPerformanceTests {
// Fast...
public
void
initNonExistentWithPrefix
()
throws
Exception
{
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"
);
close
();
}
...
...
@@ -107,10 +107,15 @@ public class InitCommandPerformanceTests {
// Slow...
public
void
initFromClasspath
()
throws
Exception
{
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"
);
close
();
}
}
public
static
void
main
(
String
[]
args
)
{
SpringCli
.
main
(
"hint"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment