提升混合应用页面打开速度的新思路

本文介绍了一种提高混合应用页面加载速度的方法,通过先移除JS资源,待页面渲染后再加载,有效缩短了页面展现时间,并使不同页面的加载速度趋于一致。

我们都知道混合应用的流畅性不如原生应用,除了不能像原生一样轻松驾驭各种狂拽酷炫的效果,混合应用还有一个难以消除的弱点在于页面打开速度上,如果有机会在同一台手机上直接对比的话,这种差距是肉眼都能直观感受到的,这主要是由于web页面每次打开前需要初始化,在那一瞬间需要完成DOM创建、资源下载、样式渲染、js执行,这些时间消耗造成了按键按下与页面进场之间短暂的停顿,也造成了混合应用整体“不流畅,不跟手”的印象。

为了尽可能解决这个问题,最近想到了一种新的优化思路,优化后在测试机上空白页面的打开速度较之前提升100ms左右,对于大量依赖js运行的页面提速效果更明显。思路并不复杂,就是先移除页面中所有的js资源,加速页面渲染速度,当页面开始进场动画后,再开始加载执行js,充分利用了进场动画的时间,以缩短整个页面展现的时间,核心代码就是选择时机动态插入js节点部分,该方案已经应用于HybridStart项目,详细过程点此了解

应用该方案后同时还解决了混合应用的另一个问题,那就是打开不同APP页面不再有速度差别了,之前产生的速度差别,主要是因为js代码可能产生异步请求或者生成DOM节点,这些耗时都不可预估,长的甚至以秒计算,优化后的页面完全排除掉js资源的干扰,渲染速度的差别就仅在于DOM渲染量的差别,而这个基本上任何页面都不会差太多,因此不同页面的打开速度也就基本一致了。

最后安利一下HybridStart项目,如果你也在用apicloud做混合应用,这一定是你的不二之选,很难想象开发体验能比这更好的了。另外HybridStart的开发原则之一是UI可剥离,我认为框架的UI跟功能绑定就是耍流氓,没错我说的就是mui,没法想象怎么用mui做自定义风格的项目,这方面HybridStart是完胜的,使用HybridStart做项目完全不需要担心UI好不好改,因为你甚至可以一怒之下将自带UI删掉重写,完全不影响核心功能,当然了即便UI不是重点,HybridStart的自带UI也足够满足普通APP项目需求了,而且附带less文件,只要是别太非主流的风格都能通过改变量实现。

最后,虽然目前HybridStart是基于apicloud平台,但未来还会跟进dcloud平台,因为HybridStart的初衷就是跨平台,最初一版是脱胎于古老的appcan平台,在饱尝appcan之坑后,决心将其打造成一款跨引擎的混合应用开发框架,目的就是抹平不同平台的引擎api差别,减少平台迁移成本,就目前来看,apicloud跟dcloud相比,还是稍逊一筹的。

转载于:https://www.cnblogs.com/refined-x/p/7222039.html

def Page_Check(self, x, y): # 防止概率出现的坐标点击失效情况 while (1): point_back_shot_test = self.d.find_component( BY.text("拍照")).getBoundsCenter() print(point_back_shot_test.X, point_back_shot_test.Y) if (point_back_shot_test.X >= 580 and point_back_shot_test.X <= 712) and ( point_back_shot_test.Y >= 2073 and point_back_shot_test.Y <= 2165): print("======当前页面为拍照页面======") break print("======目前在专业相机页面======") self.d.touch((x, y)) time.sleep(1) def ResourceUsage_PerformanceDynamic_Camera_0010(self): # 1. 打开相机,等待5s self.d.start_app("com.huawei.camera", "com.huawei.camera") time.sleep(5) # 2. 点击拍照模式,等待2s self.d.touch(BY.text("拍照")) time.sleep(2) # # CommonUtilsAOSP.camera_record_settings(self.d, "[16:9] 1080p", "30 fps") # 2. 获取测试过程需要的坐标点点击人像模式 point_back_people = self.d.find_component(BY.text("人像")).getBoundsCenter() center_point = self.d.find_component(BY.key("com.huawei.camera:id/main_view_base")).getBoundsCenter() self.d.touch((point_back_people.X, point_back_people.Y)) # 切换到录像模式 point_back_video = self.d.find_component(BY.text("录像")).getBoundsCenter() self.d.touch((point_back_video.X, point_back_video.Y)) # 切换到专业模式 point_back_pro = self.d.find_component(BY.text("专业")).getBoundsCenter() self.d.touch((point_back_pro.X, point_back_pro.Y)) # 切换到拍照模式 point_back_shot = self.d.find_component(BY.text("拍照")).getBoundsCenter() self.d.touch((point_back_shot.X, point_back_shot.Y)) time.sleep(1) print(point_back_shot.X, point_back_shot.Y) # 防止概率出现的坐标点击失效情况 self.Page_Check(point_back_shot.X, point_back_shot.Y) # 切换前置摄像头 point_front_switch = self.d.find_component( BY.key("com.huawei.camera:id/iv_camera_switch_single_outCircle")).getBoundsCenter() self.d.touch((point_front_switch.X, point_front_switch.Y)) # 切换拍照模式 point_front_shot = self.d.find_component(BY.text("拍照")).getBoundsCenter() self.d.touch((point_front_shot.X, point_front_shot.Y)) time.sleep(1) # 切换后置摄像头 self.d.touch((point_front_switch.X, point_front_switch.Y)) # 检查Zoom1倍 if not self.d.check_component_exist(BY.text("1x")): zoom_index1_point = self.d.find_component(BY.key("com.huawei.camera:id/one_button_bg")).getBoundsCenter() self.d.touch((zoom_index1_point.X, zoom_index1_point.Y)) time.sleep(5) tag = "1" self.cpu_collect._start_task(rounds=int(self.round), tag=tag, cpu_time=30) time_start = time.time() # 保证第一个点击操作可以被采集到 time.sleep(2) # 点击屏幕中心,避免进入休眠模式 self.d.touch((center_point.X, center_point.Y)) time.sleep(2) # 3. 预览30s CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0001&桌面-点击-页面切换-相机首页-1、点击人像模式,等待5s", tag_step="Start") # Step.点击人像模式,等待5s self.d.touch((point_back_people.X, point_back_people.Y)) time.sleep(5) CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0001&桌面-点击-页面切换-相机首页-1、点击人像模式,等待5s", tag_step="End") # Step.点击录像模式,等待5s CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0002&相机首页-点击-页面切换-拍照页面-2、点击录像模式,等待5s", tag_step="Start") self.d.touch((point_back_video.X, point_back_video.Y)) time.sleep(5) CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0002&相机首页-点击-页面切换-拍照页面-2、点击录像模式,等待5s", tag_step="End") # Step.点击专业模式,等待5s CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0003&相机首页-点击-页面切换-拍照页面-3、点击专业模式,等待5s", tag_step="Start") self.d.touch((point_back_pro.X, point_back_pro.Y)) time.sleep(5) CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0003&相机首页-点击-页面切换-拍照页面-3、点击专业模式,等待5s", tag_step="End") # Step.点击拍照模式,等待5s CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0004&相机首页-点击-页面切换-拍照页面-4、点击拍照模式,等待5s", tag_step="Start") self.d.touch((point_back_shot.X, point_back_shot.Y)) time.sleep(5) CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0004&相机首页-点击-页面切换-拍照页面-4、点击拍照模式,等待5s", tag_step="End") # Step.切换至前置,等待5s CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0005&相机首页-点击-页面切换-拍照页面-5、切换至前置,等待5s", tag_step="Start") self.d.touch((point_front_switch.X, point_front_switch.Y)) time.sleep(5) CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0005&相机首页-点击-页面切换-拍照页面-5、切换至前置,等待5s", tag_step="End") # Step.切换至拍照模式 CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0006&桌面-点击-页面切换-相机首页-6、点击拍照模式,等待5s", tag_step="Start") self.d.touch((point_front_shot.X, point_front_shot.Y)) time.sleep(5) CommonUtilsAOSP.record_test_step_bytrace(self.d, package_name=self.packagename, case_name=self.TAG, step_code_num="0006&桌面-点击-页面切换-相机首页-6、点击拍照模式,等待5s", tag_step="End") time_end = time.time() if time_end - time_start < 40: time.sleep(30 - (time_end - time_start)) self.cpu_collect._stop_task(self.report_path, tag=tag) time.sleep(10) # 点击屏幕中心,避免进入休眠模式 self.d.touch((center_point.X, center_point.Y)) time.sleep(1) # 4.判断测试结束后页面是否人像和前置 # self.d.check_component_exist(BY.key("TAB_BAR_LIVE_PHOTO_TAB_BAR_CLOSE")) point_front_people = self.d.find_component(BY.text("人像")).getBoundsCenter() host.check(point_back_people.X, point_front_people.X) host.check(point_back_people.Y, point_front_people.Y) time.sleep(2) # 切换至后置摄像头 point_front_switch = self.d.find_component( BY.key("com.huawei.camera:id/iv_normal_control_bar_switcher_single")).getBoundsCenter() self.d.touch((point_front_switch.X, point_front_switch.Y))
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值