简体   繁体   English

如何调试$ ionicView.beforeEnter和$ ionicView.enter之间的时间

[英]How to debug time between $ionicView.beforeEnter and $ionicView.enter

I use ionic , and it takes more than 1s between $ionicView.beforeEnter and $ionicView.enter . 我使用离子$ionicView.beforeEnter$ionicView.enter之间需要超过1 $ionicView.enter

How can I find which part of my code is taking so much time? 如何找到代码的哪一部分需要花费这么多时间? Batarang is not helping me much and I can't figure out an easy way of doing this... Batarang并没有帮助我,我无法想出一个简单的方法来做到这一点......

Probably not very helpful but when I had a similar issue I could not find the culprit using Chrome debug profiler and had to comment/exclude parts of the code in my controller, which I transition to, one by one. 可能不是很有帮助,但是当我遇到类似的问题时,我找不到使用Chrome调试分析器的罪魁祸首,并且不得不在我的控制器中评论/排除部分代码,我将逐一转换。 The problem was that some third party calendar component being configured in the controller init stage was slowing the transition (view display). 问题是在控制器初始阶段配置的某些第三方日历组件正在减慢转换(视图显示)。 Once commented out everything worked fine. 一旦注释掉一切都很好。 Since this was not my code and I did not want to mess with it I had to add a progress spinner on the transition. 由于这不是我的代码,我不想搞砸它,我不得不在过渡时添加一个进度微调器。

Maybe you can use the debug tools provided with Chrome like Timeline or Profile : you start the profiling or the record of the timeline and check what happens between $ionicView.beforeEnter and $ionicView.enter. 也许您可以使用Chrome提供的调试工具,如时间轴或配置文件:您可以开始分析或记录时间轴,并检查$ ionicView.beforeEnter和$ ionicView.enter之间发生的情况。 Both features have a search module so you can look for $ionicView.beforeEnter and see what is taking time until $ionicView.enter . 这两个功能都有一个搜索模块,因此您可以查找$ionicView.beforeEnter并查看$ionicView.enter之前的时间。 It's probably not the easiest way but hope it will help you. 这可能不是最简单的方法,但希望它会对你有所帮助。

Have you tried monitoring the traffic in the Network tab in console? 您是否尝试在控制台的“网络”选项卡中监控流量? The time in ms is a good indicator of which xhr calls are running longer than expected... run a speed test . 以ms为单位的时间是一个很好的指示,表明xhr调用的运行时间比预期的要长......运行速度测试

Otherwise, if your using chrome, I would just drop some debugger statements throughout the flow of that Ionic view's state. 否则,如果你使用chrome,我会在整个Ionic视图状态的流程中删除一些debugger语句。

I cannot think of an easy way of doing this. 我想不出一个简单的方法。 But extending on what @nico_ mentioned, using the chrome javascript debug tool, you should set a breakpoint on your $ioniView.beforeEnter and a breakpoint on $ionicView.enter then step through the code between the breakpoints. 但在什么@nico_提到,使用Chrome的JavaScript调试工具扩展,你应该设置你的断点$ioniView.beforeEnter和断点$ionicView.enter然后通过断点之间的代码步骤。

You can read more about chrome's javascript debug tools and how to set up breakpoints here: https://developer.chrome.com/devtools/docs/javascript-debugging 您可以在此处阅读有关chrome的javascript调试工具以及如何设置断点的更多信息: https//developer.chrome.com/devtools/docs/javascript-debugging

There is no code "between the breakpoints"... There are so much functions called between the 2 events... 在断点之间没有代码......在2个事件之间调用了很多函数......

-- Tyrius - Tyrius

You should try to identify functions that are taking too much time to run. 您应该尝试识别运行时间过长的功能。

Note: I assume your app is not slowed down by downloads, you can check you downloads time in Chrome Dev Tools (If TTFB is too high, you might have a server-side slowness). 注意:我认为您的应用不会因下载而变慢,您可以在Chrome开发工具中查看下载时间(如果TTFB太高,您的服务器端可能会变慢)。

If you know which functions are called, you have two possibilities : 如果您知道调用了哪些函数,则有两种可能:

  • When there is a few functions and not called too many times: 当有一些功能而没有多次调用时:

     function ExampleFunction() { console.time("ExampleFunction"); // ExampleFunction code // ... console.timeEnd("ExampleFunction"); // output in console time between time() call and timeEnd() call } 
  • If there is a lot of functions called many times : 如果有很多次调用的函数:

I suggest you to use my little JS tool called MonitorJS to help you identify code blocks taking too much time to run : 我建议你使用我的名为MonitorJS的小JS工具来帮助你识别代码块花费太多时间来运行:

function ExampleFunction() {
    let mId = Monitor.Start("ExampleFunction");
    // ExampleFunction code
    // ...
    Monitor.Stop(mId);
}

and when you what to see which function is taking too much time, call this function : 当你看什么功能花费太多时间时,请调用此函数:

function ShowMonitorRecords() {
    // get all records sorted by total_time desc
    let records = Monitor.GetAll().sort((a,b)=>{ return b.total_time - a.total_time; });
    // print every records
    for (let record of records) {
        let avg = record.total_time / record.call_count;
        console.log(record.name + ": " + record.total_time.toFixed(2) 
            + "ms - called " + record.call_count 
            + " time(s) - average time : "+ avg.toFixed(2) +"ms");
    }
}

// will output something like :
// Foo: 7234.33ms - called 2 time(s) - average time : 3617.16ms
// Bar: 6104.25ms - called 3 time(s) - average time : 2034.75ms

Once you know which function is taking too much time, you can reduce the scope of Start/Stop to identify the exact block of code slowing down your app and refactor. 一旦知道哪个函数占用了太多时间,就可以缩小启动/停止的范围,以确定减慢应用程序和重构速度的确切代码块。

Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM