发表于: 2017-09-18 10:56:22

1 683


今天完成的事情:

maven依赖的scope标签用法

scope标签参数

compile,默认的scope,表示依赖在整个生命周期都可以用。这些依赖会传递到依赖的项目中,适用于所有阶段,并且随着项目一起发布

provided,表示依赖由JDK或容器提供,只能用在编译和测试中

runtime,不用在编译时,用在运行和测试时

test,只用在测试时,不随项目发布

system,和provided类似,但以外部jar包形式提供,maven不会在repository中查找它

只所以写这个是因为我在测试memcached工具类的时候写不了单元测试,发现它的scope是test,我新建test时把它弄成sources了

改成Tests就好了,或者

把junit的scope改成compile

@Test
public void test(){
MemcachedUtil.put("hello", "world", 60);
   String hello = (String) MemcachedUtil.get("hello");
   Assert.assertEquals("world", hello);
   System.out.println(hello);
}

可以跑通

介绍一下junit4 assert类中的assert方法

public static void assertTrue(boolean condition) {
assertTrue((String)null, condition);
}

public static void assertFalse(String message, boolean condition) {
assertTrue(message, !condition);
}

public static void assertFalse(boolean condition) {
assertFalse((String)null, condition);
}

用来判断一个变量为true还是false。基本就是assertFalse查看的变量是false的话测试成功,是true则失败。

public static void fail(String message) {
if (message == null) {
throw new AssertionError();
   } else {
throw new AssertionError(message);
   }
}

public static void fail() {
fail((String)null);
}

抛出错误用的,可以有message也可以没有

public static void assertEquals(String message, Object expected, Object actual)

判断两个值是否相同,可以输出错误message

expected是期望值,actual是实际值

源码里还有好多方法,不怎么看的懂

怎么用已经很明显了

在代码上加上缓存,问题来了mmp

我下午绝对是脑子有问题了,这个错误这么明显一直没看明白

就一个添加list进缓存搞了两个小时,试了各种方法

public List<User> userAll(){
List<User> users=userMapper.userAll();
   MemcachedUtil.set("users",users);
   System.out.println(MemcachedUtil.get("users"));
   return userMapper.userAll();
}

一直都是null。。。最后又看了一眼报错,发现一句话,序列化错误,我没加序列化。。。

public class User implements Serializable

打印出来了缓存中的东西

缓存object必须序列化,甭问为什么

然后要什么新建数据接口我就是之前的注册页面了,意思是把注册的用户分别存到缓存和数据库吧,然后登陆的时候先验证缓存,不存在再验证数据库是吧

controller还是原来的,贴一下方便看吧

@RequestMapping(value = "/login",method = RequestMethod.POST)
public void login(@RequestParam("user") String user, @RequestParam("password") String password,
                 HttpServletResponse httpServletResponse) {
String md5= MD5Util.stringToMD5(user+password);
   if(userService.isRightUser(user,md5)){
//待加密内容
       long id=userService.selectByName(user).getId();
       long Date=new Date().getTime();
       String str = id+"="+Date;
       //加密操作
       byte[] result = DES.encrype(str.getBytes() ,"12345678");
       //把加密的字节转换为16进制
       String resules= TypeUtil.bytesToHexString(result);
       Cookie cookie = new Cookie("token",resules);
       cookie.setMaxAge(60*60*24*7);//保留7天
       httpServletResponse.addCookie(cookie);
       try {
httpServletResponse.sendRedirect("u/job");
       } catch (IOException e) {
e.printStackTrace();
       }
}else {
try {
httpServletResponse.sendRedirect("error");
       } catch (IOException e) {
e.printStackTrace();
       }
}
}

//注册页面
@RequestMapping(value = "/regist",method = RequestMethod.GET)
public String regist(){
return "regist";
}

@RequestMapping(value = "/register",method = RequestMethod.POST)
public String register(@RequestParam("user") String user, @RequestParam("password") String password){
String md5= MD5Util.stringToMD5(user+password);
   userService.insert(user,md5);
   logger.info("----->"+user+md5);
   return "success";
}

研究了半天的service层,目前先这样吧,其实这个和controller都能优化一下,没时间弄了

@Service
public class UserServiceImpl implements UserService {
@Autowired
   private UserMapper userMapper;

   public boolean isRightUser(String user,String password){
System.out.println("输入的账号:" + user + "输入的密码:" + password);
       if (MemcachedUtil.get("users") == null){
System.out.println("查数据库了");
           User user1=userMapper.isRightUser(user,password);
           if (user1 != null){
return true;
           }
}
System.out.println("查缓存");
       return true;
   }

public List<User> userAll(){
List<User> users=userMapper.userAll();
       MemcachedUtil.set("users",users);
       System.out.println(MemcachedUtil.get("users"));
       return userMapper.userAll();
   }
public User selectByName(String user){
return userMapper.selectByName(user);
   }
public int insert(String user, String password) {
Integer i=userMapper.insert(user,password);
       User user1=userMapper.selectByName(user);
       System.out.println(user1);
       MemcachedUtil.set("users",user1);
       System.out.println(MemcachedUtil.get("users"));
       return i;
   }
public User select(int id){
return userMapper.select(id);
   }
}

运行结果

先注册,两个打印的user一个是数据库里的一个是缓存里的。登陆,有缓存就先查了缓存,没查数据库,然后查出来的东西能通过拦截器证明没有问题,完成。

用jmeter测一下,正好今天佳义讲了一波,否则我估计还测不出来

因为我要测login这个加了缓存的界面,同时这里还有cookie,所以得加上cookie管理器,否则跑不通

平均142,90%Line418

吞吐量越来越高,嗯...

TPS这个,一般来说是看稳定之后的TPS值,可是,这怎么看→_→

然后我又测了几组,发现线程较少的情况下很难看出有什么问题,所以,开个200线程试试

后边还有,不过基本都是平的

我发现TPS都是先增高然后平稳然后降低,平稳期大概是420个事务/秒(强无敌)

而吞吐量在平稳之后大概是405个请求/秒,嗯。。。近似相等

然后可以来搞一个老大今天说的,90%的请求都在200MS内返回的时候,最大支持的TPS是多少

试了几下子

90%Line现在是197,看看TPS

艾尼玛,笑死我了,金字塔

吞吐量416.4/S,TPS大概是,嗯。。。看不懂啊...

目测最大是450,嗯,还不错吧!?

好了发现问题所在了,测的时间太短了根本显示不出tps的本来面目,我来个100请求并发然后无限循环,随时停止

加了缓存就是厉害啊,90%Line竟然才369

TPS清晰多了,稳定时平均在500左右,吞吐量492,差不多

把缓存卸了试试

加不加缓存差距还是挺大的,90%Line是693,TPS是350,吞吐量是291.3,都差了不少

明天计划的事情:

负载均衡,还有redis

遇到的问题:

就没有会测试的么_(:з」∠)_

收获:90%的请求都在200MS内返回的时候,最大支持的TPS是多少。通常测这个来保证用户请求。



返回列表 返回列表
评论

    分享到