在服务器请求后测量JSF视图的渲染时间 [英] Measure the render time of a JSF view after a server request

查看:114
本文介绍了在服务器请求后测量JSF视图的渲染时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测量JSF应用程序的渲染时间.由于我的力量原因,无法在应用程序中填充日志.

I would like to measure the rendering time of a JSF application. Because of out of my power reasons, the application can't be populated with logs.

因此,我的问题是,在使用任何浏览器执行包含后端(服务器)调用的特定操作之后,有什么方法可以测量应用程序的渲染时间?

Therefore, my question would be, is there any way in which I can measure the rendering time of the application after doing a certain action that includes a back-end(server) call using any browser?

到目前为止,在使用Chrome开发者工具后,我发现了以下内容.在网络"选项卡上,每个请求均显示时间".此外,选择某个条目后,在时间"选项卡上,将显示更详细的可视化. 现在,我可以从等待"中看出,它是在此处捕获到服务器的往返行程,但是实际渲染时间呢?

So far,after using the Chrome Developer Tools,I spotted the following. On the Network tab, each request has the "Time" displayed. In addition, after selecting a certain entry, on the "Timing" tab, a more detailed visualization it's displayed. Now, I can tell from that that the "Waiting" that the round-trip to the server it's captured here, but what about the actual rendering time.

假设整个请求花了1秒,而等待"部分是500毫秒,我可以推断渲染时间是1秒-500毫秒吗?我不这么认为,这就是为什么我问这个问题.

Assuming that the whole request took 1sec, and the Waiting section it's 500ms, can I deduct that the rendering it's the 1sec-500ms ? I assume not, thats why I am asking this question.

长话短说,对于某些请求,我需要从浏览器中知道,服务器处理了多长时间,实际的UI渲染了多长时间.

Long story short, I would need to know from the browser, for a certain request how long was the server processing and how long was the actual UI rendering.

任何提示将不胜感激.谢谢.

Any tips would be greatly appreciated. Thank you.

推荐答案

您可以使用自定义的

You can do that with a custom ViewDeclarationLanguage whereby you measure the createView(), buildView(), renderView() and if necessary restoreView() methods.

这是一个开球示例:

public class VdlLogger extends ViewDeclarationLanguageWrapper {

    private static final Logger logger = Logger.getLogger(VdlLoggerFactory.class.getName());

    private ViewDeclarationLanguage wrapped;

    public VdlLogger(ViewDeclarationLanguage wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public UIViewRoot createView(FacesContext context, String viewId) {
        long start = System.nanoTime();
        UIViewRoot view = super.createView(context, viewId);
        long end = System.nanoTime();
        logger.info(String.format("create %s: %.6fms", viewId, (end - start) / 1e6));
        return view;
    }

    @Override
    public void buildView(FacesContext context, UIViewRoot view) throws IOException {
        long start = System.nanoTime();
        super.buildView(context, view);
        long end = System.nanoTime();
        logger.info(String.format("build %s: %.6fms", view.getViewId(), (end - start) / 1e6));
    }

    @Override
    public void renderView(FacesContext context, UIViewRoot view) throws IOException {
        long start = System.nanoTime();
        super.renderView(context, view);
        long end = System.nanoTime();
        logger.info(String.format("render %s: %.6fms", view.getViewId(), (end - start) / 1e6));
    }

    @Override
    public ViewDeclarationLanguage getWrapped() {
        return wrapped;
    }

}

要使其运行,请创建以下工厂:

To get it to run, create the below factory:

public class VdlLoggerFactory extends ViewDeclarationLanguageFactory {

    private ViewDeclarationLanguageFactory wrapped;

    public VdlLoggerFactory(ViewDeclarationLanguageFactory wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ViewDeclarationLanguage getViewDeclarationLanguage(String viewId) {
        return new VdlLogger(wrapped.getViewDeclarationLanguage(viewId));
    }

    @Override
    public ViewDeclarationLanguageFactory getWrapped() {
        return wrapped;
    }

}

并按以下内容在faces-config.xml中注册:

And register it as below in faces-config.xml:

<factory>
    <view-declaration-language-factory>com.example.VdlLoggerFactory</view-declaration-language-factory>
</factory>

createView()是基于视图文件中存在的<f:view><f:metadata>创建具体的UIViewRoot实例的步骤.当使用Facelets(XHTML)作为视图时,在此步骤中,所有关联的XHTML文件将由SAX解析器解析并缓存一段时间,如javax.faces.FACELETS_REFRESH_PERIOD中所定义.因此,可能发生的是,它的时间相对较慢,而另一时间却很快.

The createView() is the step of creating the concrete UIViewRoot instance based on <f:view> and <f:metadata> present in the view files. When using Facelets (XHTML) as view, during this step all associated XHTML files will be parsed by the SAX parser and cached for a time as defined in javax.faces.FACELETS_REFRESH_PERIOD. So it may happen that it's one time relatively slow and the other time blazing fast.

buildView()是基于视图(XHTML)组成填充JSF组件树(UIViewRootgetChildren())的步骤.在此步骤中,将执行所有标记处理程序(JSTL和朋友),并评估这些标记处理程序中的所有EL表达式以及组件的idbinding属性(有关详细信息,另请参见

The buildView() is the step of populating the JSF component tree (the getChildren() of UIViewRoot) based on the view (XHTML) composition. During this step, all taghandlers (JSTL and friends) are executed and all EL expressions in those taghandlers and component's id and binding attributes are evaluated (for detail, see also JSTL in JSF2 Facelets... makes sense?). So if backing beans are constructed for first time during view build time and invoking business logic during @PostConstruct, then it may happen that this is time consuming.

renderView()是从UIViewRoot#encodeAll()开始基于JSF组件树和模型生成HTML输出的步骤.因此,如果在视图渲染期间第一次构造支持bean,并在@PostConstruct期间调用业务逻辑,那么可能会浪费时间.

The renderView() is the step of generating the HTML output based on JSF component tree and the model, starting with UIViewRoot#encodeAll(). So if backing beans are constructed for first time during view render time and invoking business logic during @PostConstruct, then it may happen that this is time consuming.

如果后备bean在getter方法中而不是在@PostConstruct或任何其他一次性发生的生命周期事件侦听器中错误地执行业务逻辑,则可能会浪费更多时间.另请参见为什么JSF多次调用getters .

If backing beans are incorrectly performing business logic in getter methods instead of in @PostConstruct or any other one-time-occurring life cycle event listener, then it may happen that this consumes yet more time. See also Why JSF calls getters multiple times.

这篇关于在服务器请求后测量JSF视图的渲染时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆