【问题标题】:Why is the server response after a Drag&Drop so large and slow为什么拖放后的服务器响应如此之大且速度慢
【发布时间】:2022-01-15 20:28:47
【问题描述】:

我在网格之间拖动卡片时遇到了一些性能问题。从后端的角度来看,更改后从网格存储数据大约需要 200 毫秒。

但是,当后端工作似乎完成时,前端需要另外 2.5 秒才能从请求中获得响应。需要这么长时间的请求会联系 2 个 rpc 事件:grid-drop 和 grid-dragend。

我认为反响也非常大。只是给你一个想法,看截图......注意右边的小滚动条。 ??? TTFB 为 2,42s,下载大小约半 MB。

任何想法这里发生了什么以及如何消除它?

我正在使用 Vaadin 21.0.4,spring boot 2.5.4。

我为优化性能而采取的步骤:

  1. 优化数据库查询+索引
  2. 尽可能使用@cacheable
  3. 使用 LitElement 实现卡片

这是放置监听器:


ComponentEventListener<GridDropEvent<Task>> dropListener = event -> {
            if (dragSource != null) {
                // The item ontop or below where the source item is dropped. Used to calculate the index of the newly dropped item(s)
                Optional<Task> targetItem = event.getDropTargetItem();
                // if the item is dropped on an existing row and the dragged item contains the same items that's being dropped.
                if (targetItem.isPresent() && draggedItems.contains(targetItem.get())) {
                    return;
                }

                // Add dragged items to the grid of the target room
                Grid<Task> targetGrid = event.getSource();
                Optional<Room> room = dayPlanningView.getRoomForGrid(targetGrid);

                // The items of the target Grid. Using listdataview so this would not retrigger the query
                List<Task> targetItems = targetGrid.getListDataView().getItems().toList();

//               Calculate the position of the dropped item
                int index = targetItem.map(task -> targetItems.indexOf(task)
                                + (event.getDropLocation() == GridDropLocation.BELOW ? 1 : 0))
                        .orElse(0);

                room.ifPresent(r -> service.plan(draggedItems, r, index, dayPlanningView.getSelectedDate()));
// send event to update other users
                Optional<ScheduleUpdatedEvent> scheduleUpdatedEvent = room.map(r -> new ScheduleUpdatedEvent(PlanningMasterDetailView.this, r.getId()));
                scheduleUpdatedEvent.ifPresent(Broadcaster::broadcast);

// remove items from the source grid. using list provider so items can be removed without DB round-trip.
    productionOrderGrid.getListDataView().removeItems(draggedItems);

            }
        };

我现在有点卡住了,因为我有点没主意了????

谢谢

【问题讨论】:

  • 你的代码是什么样子的?您使用的是哪个 DataProvider?拖放有什么作用?
  • 我尝试了惰性方法和列表数据提供程序。两者都给出相同的结果。此外,我在网格中的卡片数量低于单页,目标网格最多包含 10 张卡片。另外,我切换到 ListDataView,这样我就可以确定放置位置而不必再次从数据库中获取项目,我现在也可以从源网格中删除项目,从数据库重新加载。 DropEvenListener 在大约 200 毫秒内完成,然后在 3 秒内我仍然看到顶部的加载栏。
  • 如何创建网格的内容?你用的是 TemplateRenderer 还是 ComponentRenderer

标签: vaadin-flow vaadin-grid vaadin14


【解决方案1】:

您应该使用 TemplateRenderer/LitRenderer 而不是 ComponentRenderer,因为生成的服务器端组件会影响性能:

在此处阅读更多信息:https://vaadin.com/blog/top-5-most-common-vaadin-performance-pitfalls-and-how-to-avoid-them

【讨论】:

    猜你喜欢
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2019-10-13
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多