任务描述:
今天是2011年01月26日,小年夜。百度地图API在小年夜献给广大API爱好者一份给力的礼物!那就是,百度地图API内存消耗优化升级了!
此次API优化升级后,内存消耗保持平稳,绝大部分测试中,内存消耗趋于一条直线。(如图所示)
如何实现:
a) 本次测试全部采用IE6浏览器;
b)
本次测试的覆盖物全部采取随机方式创建;
c)
本次测试覆盖物种类分别为:信息窗口、标注、圆形、多边形、折线、标签;
d)
依次连续不间断做以上测试,不穿插其他测试;
e)
测试时间均为每1毫秒执行一次创建;
f)
测试期间,不新建其他进程。
图示:
说明:
优化前,内存消耗以50,000为起点,不断上升,造成运行速度慢等问题;
优化后,内存消耗一直保持在50,000左右,趋于平稳的直线。
运行代码,请点击以下链接:
marker测试用例:http://ui-love.com/baidumap/markercase.htm
label测试用例:http://ui-love.com/baidumap/labelcase.htm
源代码:
(注:由于测试用例过多,这里不一一列举,仅给出标注Marker的测试用例代码。 )
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Marker 性能</title>
<style type="text/css">/*<![CDATA[*/
body{margin:0;padding:0;font-family:Times New Roman, serif}
p{margin:0;padding:0}
#wrapper{width:100%;margin:auto;text-align:left;height:100%}
#map_container{height:480px;margin:0}
#notes{position:absolute;right:10px;width:200px;top:10px}
@media print{
input{display:none}
#notes{display:none}
#map_container{margin:10px;border:none}
}
/*]]>*/</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?key=25f144bb4491f54b83a7a7b39198c11e&v=1.1&services=true">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Marker 性能</title>
<style type="text/css">/*<![CDATA[*/
body{margin:0;padding:0;font-family:Times New Roman, serif}
p{margin:0;padding:0}
#wrapper{width:100%;margin:auto;text-align:left;height:100%}
#map_container{height:480px;margin:0}
#notes{position:absolute;right:10px;width:200px;top:10px}
@media print{
input{display:none}
#notes{display:none}
#map_container{margin:10px;border:none}
}
/*]]>*/</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?key=25f144bb4491f54b83a7a7b39198c11e&v=1.1&services=true">
</script>
</head>
<body>
<div id="map_container"></div>
<div id="test_container">
<input type="button" onclick="test_mem()" value="mem_test" />
<input type="button" onclick="stop_mem()" value="stop_test" />
</div>
</div>
</body>
<script type="text/javascript">
if (typeof console == "undefined"){
window.console = {
log: function(){
}
}
}
</script>
<script type="text/javascript">
// 创建地图对象并初始化
var mp = new BMap.Map("map_container");
var point = new BMap.Point(116.404, 39.915);
mp.centerAndZoom(point, 14);
// 内存性能测试
var count = 0;
function createInfo() {
mp.clearOverlays();
if (count > 3000) {
clearInterval(window._timer);
alert("测试结束");
}
createMarkers();
}
function createMarkers(){
var bounds = mp.getBounds();
var lngSpan = bounds.maxX - bounds.minX;
var latSpan = bounds.maxY - bounds.minY;
for (var i = 0; i < 9; i ++) {
count ++;
var point = new BMap.Point(bounds.minX + lngSpan * (Math.random() * 0.7 + 0.15),
bounds.minY + latSpan * (Math.random() * 0.7 + 0.15));
var marker = new BMap.Marker(point);
mp.addOverlay(marker);
}
}
function test_mem() {
window._timer = setInterval(createInfo, 1);
}
function stop_mem() {
clearInterval(window._timer);
alert("运行了"+count+"次");
}
</script>
</html>
</head>
<body>
<div id="map_container"></div>
<div id="test_container">
<input type="button" onclick="test_mem()" value="mem_test" />
<input type="button" onclick="stop_mem()" value="stop_test" />
</div>
</div>
</body>
<script type="text/javascript">
if (typeof console == "undefined"){
window.console = {
log: function(){
}
}
}
</script>
<script type="text/javascript">
// 创建地图对象并初始化
var mp = new BMap.Map("map_container");
var point = new BMap.Point(116.404, 39.915);
mp.centerAndZoom(point, 14);
// 内存性能测试
var count = 0;
function createInfo() {
mp.clearOverlays();
if (count > 3000) {
clearInterval(window._timer);
alert("测试结束");
}
createMarkers();
}
function createMarkers(){
var bounds = mp.getBounds();
var lngSpan = bounds.maxX - bounds.minX;
var latSpan = bounds.maxY - bounds.minY;
for (var i = 0; i < 9; i ++) {
count ++;
var point = new BMap.Point(bounds.minX + lngSpan * (Math.random() * 0.7 + 0.15),
bounds.minY + latSpan * (Math.random() * 0.7 + 0.15));
var marker = new BMap.Marker(point);
mp.addOverlay(marker);
}
}
function test_mem() {
window._timer = setInterval(createInfo, 1);
}
function stop_mem() {
clearInterval(window._timer);
alert("运行了"+count+"次");
}
</script>
</html>