Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化Properties#stringPropertyNames处理,jdk9以下版本此方法cpu消耗相对过高. #1072

Merged
merged 1 commit into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference;

import org.slf4j.Logger;
Expand Down Expand Up @@ -101,7 +102,20 @@ public Set<String> getPropertyNames() {
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<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码虽然是从jdk9复制过来的,还是建议加一段UT来测试,以防后续被其它人改错逻辑。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok已增加.

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import org.junit.After;
Expand Down Expand Up @@ -645,6 +647,39 @@ public void onChange(ConfigChangeEvent changeEvent) {
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertEquals(someProperties.stringPropertyNames(), propertyNames) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已增加.

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
defaultValue) {
assertEquals(expected, config.getDateProperty(propertyName, format, defaultValue));
Expand Down