Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
d4ae4109
Commit
d4ae4109
authored
May 26, 2022
by
Zheng Jie
Browse files
update
parent
10a16412
Changes
1
Hide whitespace changes
Inline
Side-by-side
eladmin-system/src/test/java/me/zhengjie/LoginCacheTest.java
deleted
100644 → 0
View file @
10a16412
package
me.zhengjie
;
import
me.zhengjie.modules.security.service.UserDetailsServiceImpl
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
javax.annotation.Resource
;
import
java.util.concurrent.CountDownLatch
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
public
class
LoginCacheTest
{
@Resource
(
name
=
"userDetailsService"
)
private
UserDetailsServiceImpl
userDetailsService
;
ExecutorService
executor
=
Executors
.
newCachedThreadPool
();
@Test
public
void
testCache
()
throws
InterruptedException
{
long
start1
=
System
.
currentTimeMillis
();
int
size
=
1000
;
CountDownLatch
latch
=
new
CountDownLatch
(
size
);
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
executor
.
submit
(()
->
userDetailsService
.
loadUserByUsername
(
"admin"
));
latch
.
countDown
();
}
latch
.
await
();
long
end1
=
System
.
currentTimeMillis
();
//关闭缓存
userDetailsService
.
setEnableCache
(
false
);
long
start2
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
userDetailsService
.
loadUserByUsername
(
"admin"
);
}
long
end2
=
System
.
currentTimeMillis
();
System
.
out
.
print
(
"使用缓存:"
+
(
end1
-
start1
)
+
"毫秒\n 不使用缓存:"
+
(
end2
-
start2
)
+
"毫秒"
);
}
@Test
public
void
testCacheManager
()
throws
InterruptedException
{
int
size
=
1000
;
CountDownLatch
latch
=
new
CountDownLatch
(
size
);
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
int
mod
=
i
%
10
;
executor
.
submit
(()
->
{
try
{
Thread
.
sleep
(
mod
*
2
+
(
int
)
(
Math
.
random
()
*
10000
));
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
userDetailsService
.
loadUserByUsername
(
"admin"
+
mod
);
latch
.
countDown
();
System
.
out
.
println
(
"剩余未完成数量"
+
latch
.
getCount
());
});
}
latch
.
await
();
}
}
Write
Preview
Supports
Markdown
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