发表于: 2017-07-10 22:56:36

1 1149


2.加上Memcache,在新建数据的时候同时维护好缓存(没有新建数据接口就自己加上,可以分成是压测JSP和Json接口两种方式) ,确定数据没问题,重新压测服务器,测出90%的线在哪里.



Memcached是一个键值对的内存缓存系统,可以用来缓存数据库请求的结果,也可以用来缓存API返回的结果,也可以缓存页面。

Memcached本身不能提供完整的服务,只是一个进程,需要自己写代码连接服务,缓存需要缓存的东西。


使用Memcached分为两步,一是配置服务器端,二是配置客户端端。服务器端,如果使用Ubuntu或CentOS,可以直接用apt-get或yum进行安装,如果在Mac上,则需要自行编译用make make install 安装。安装后可以使用默认设置。


三个常用命令:-m -d -v -u(使用root时使用)
memcached -m -u root  设置使用多大内存,默认64兆
memcached -d -u root 开始服务

memcached -v -u root 显示信息



MyBatis官方有和Memcached整合的包,可以直接在Maven添加,用的时候也不用做任何设置。
只需要添加一个依赖:
    <dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-memcached</artifactId>
    <version>1.0.0</version>
    </dependency>
然后在mapper.xml中加入使用Memcached的配置,就可以使用了(默认Memcached也安装在本地):
        
    <mapper namespace="org.acme.FooMapper">
    <cache type="org.mybatis.caches.memcached.MemcachedCache" />
    ...
    </mapper>
如果Memcached安装不在本地,需要配置一个memcached.properties在项目目录下,注明Memcached所在的服务器地址和端口;
需要在POJO上实现Serializable,但不用覆盖任何方法。public class User implements Serializable
一开始以为MyBatis整合的Memcached只缓存SQL请求返回的值,但是如果不加上Serializable会报错,证明缓存的是List<User>对象(我自己的理解)。
对使用缓存和不使用缓存,分别对JSP请求和JSon请求进行测试,线程为2(金山云1核2g),结果如下。
无缓存JSP:
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
HTTP请求
3478
107
74
190
313
608
1
2163
15.095%
18.55478
90.46
2.39
无缓存JSon:
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
json-getAllUsers
3314
97
67
171
299
555
19
2719
17.773%
20.35814
91.25
2.72
有缓存JSP:
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
jsp-getAllUsers
6962
103
64
197
299
525
15
9934
0.086%
19.25193
83.11
2.48
有缓存JSon:
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
json-getAllUsers
8588
110
67
192
303
573
2
27562
0.303%
18.0419
73.49
2.41
JSP页面和JSon之间的差异不显著,有缓存和无缓存的速度差异不显著,但有缓存错误率显著下降。
————————————————————————————————————————————————————————
Memcached还有很多Java的包可以用,以后有需要再进行研究。
————————————————————————————————————————————————————————
3.停止Memcache进程,观察压测数据。部署两台WEB,使用Nginx的Upstream来做负载。重新压测。
使用两台服务器,一台金山云,一台腾讯云,部署两个tomcat,数据库没有部署两个,所以只压测主页,不读取数据库。
进程数100,吞吐量不限。
腾讯云服务器:
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
访问task234主页
1500
17457
3710
61911
63959
101198
42
152113
19.6%
5.23133
65
0.51
金山云服务器:
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
访问task234主页
2247
12154
4592
30130
58640
121069
12
246971
4.673%
7.52084
108.41
0.87
负载均衡:
Label
# Samples
Average
Median
90% Line
95% Line
99% Line
Min
Max
Error %
Throughput
Received KB/sec
Sent KB/sec
访问task234主页
1847
10687
4286
27680
56454
68348
12
209503
5.468%
8.29043
118.61
0.95
结果:负载均衡后,速度稍快了一些,但是效果不显著,原因可能是腾讯的服务器过慢,导致大部分分流给了金山服务器。

问题:性能很慢。


总结:无


明天:任务6最后一部分。



返回列表 返回列表
评论

    分享到