pull/743/head
Zheng Jie 2022-05-26 16:53:28 +08:00
parent 10a1641298
commit d4ae410929
1 changed files with 0 additions and 64 deletions

View File

@ -1,64 +0,0 @@
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();
}
}