Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
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
openSource
apollo
Commits
f1f72459
Unverified
Commit
f1f72459
authored
May 03, 2018
by
Jason Song
Committed by
GitHub
May 03, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1072 from wuwen5/properties#preform
优化Properties#stringPropertyNames处理,jdk9以下版本此方法cpu消耗相对过高.
parents
c4946900
96efd8ff
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
1 deletion
+50
-1
DefaultConfig.java
...a/com/ctrip/framework/apollo/internals/DefaultConfig.java
+15
-1
DefaultConfigTest.java
...m/ctrip/framework/apollo/internals/DefaultConfigTest.java
+35
-0
No files found.
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java
View file @
f1f72459
...
@@ -8,6 +8,7 @@ import java.util.Map;
...
@@ -8,6 +8,7 @@ import java.util.Map;
import
java.util.Objects
;
import
java.util.Objects
;
import
java.util.Properties
;
import
java.util.Properties
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.HashMap
;
import
java.util.concurrent.atomic.AtomicReference
;
import
java.util.concurrent.atomic.AtomicReference
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
...
@@ -101,7 +102,20 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
...
@@ -101,7 +102,20 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
return
Collections
.
emptySet
();
return
Collections
.
emptySet
();
}
}
return
properties
.
stringPropertyNames
();
return
stringPropertyNames
(
properties
);
}
private
Set
<
String
>
stringPropertyNames
(
Properties
properties
)
{
//jdk9以下版本Properties#enumerateStringProperties方法存在性能问题,keys() + get(k) 重复迭代, jdk9之后改为entrySet遍历.
Map
<
String
,
String
>
h
=
new
HashMap
<>();
for
(
Map
.
Entry
<
Object
,
Object
>
e
:
properties
.
entrySet
())
{
Object
k
=
e
.
getKey
();
Object
v
=
e
.
getValue
();
if
(
k
instanceof
String
&&
v
instanceof
String
)
{
h
.
put
((
String
)
k
,
(
String
)
v
);
}
}
return
h
.
keySet
();
}
}
@Override
@Override
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java
View file @
f1f72459
...
@@ -11,6 +11,8 @@ import java.io.File;
...
@@ -11,6 +11,8 @@ import java.io.File;
import
java.util.Calendar
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.Properties
;
import
java.util.Properties
;
import
java.util.Set
;
import
java.util.Collections
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
import
org.junit.After
;
import
org.junit.After
;
...
@@ -645,6 +647,39 @@ public class DefaultConfigTest {
...
@@ -645,6 +647,39 @@ public class DefaultConfigTest {
assertEquals
(
PropertyChangeType
.
ADDED
,
newKeyChange
.
getChangeType
());
assertEquals
(
PropertyChangeType
.
ADDED
,
newKeyChange
.
getChangeType
());
}
}
@Test
public
void
testGetPropertyNames
()
{
String
someKeyPrefix
=
"someKey"
;
String
someValuePrefix
=
"someValue"
;
//set up config repo
someProperties
=
new
Properties
();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
someProperties
.
setProperty
(
someKeyPrefix
+
i
,
someValuePrefix
+
i
);
}
when
(
configRepository
.
getConfig
()).
thenReturn
(
someProperties
);
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
someNamespace
,
configRepository
);
Set
<
String
>
propertyNames
=
defaultConfig
.
getPropertyNames
();
assertEquals
(
10
,
propertyNames
.
size
());
assertEquals
(
someProperties
.
stringPropertyNames
(),
propertyNames
);
}
@Test
public
void
testGetPropertyNamesWithNullProp
()
{
when
(
configRepository
.
getConfig
()).
thenReturn
(
null
);
DefaultConfig
defaultConfig
=
new
DefaultConfig
(
someNamespace
,
configRepository
);
Set
<
String
>
propertyNames
=
defaultConfig
.
getPropertyNames
();
assertEquals
(
Collections
.
emptySet
(),
propertyNames
);
}
private
void
checkDatePropertyWithFormat
(
Config
config
,
Date
expected
,
String
propertyName
,
String
format
,
Date
private
void
checkDatePropertyWithFormat
(
Config
config
,
Date
expected
,
String
propertyName
,
String
format
,
Date
defaultValue
)
{
defaultValue
)
{
assertEquals
(
expected
,
config
.
getDateProperty
(
propertyName
,
format
,
defaultValue
));
assertEquals
(
expected
,
config
.
getDateProperty
(
propertyName
,
format
,
defaultValue
));
...
...
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