Why does CallbackProperty cause high CPU usage?

Hi every one , When I open the WS push service, trajectory data will be pushed to create random points, and calling the some method will render the trajectory. I used the CallbackProperty class to render trajectory positons. When I stop WS, the CallbackProperty callback is still being called. If I have 10 trajectories, then there will be 10 CallbackProperty instance. I printed them out and they are printing very quickly every second. I want to know if there are any issues with my usage and if there is a better way to achieve the desired effect.

This is my code ,can run , only cesium demo. You can use chrome brower performance monitor view the cpu, few time later ,the cpu will be 100%.

And then,I added test code, and when I delete entities, the CPU will drop. But my question is that I have stopped pushing data, but I have not deleted the entity. There is only one reference to the current trajectory point data in my CallbackProperty , which will cause the CPU to remain high.

(Just for context: This was originally opened at CallbackProperty make cpu to 100%! · Issue #12860 · CesiumGS/cesium · GitHub, but is better discussed here)

First, a short hint: Kommentare im Quellcode sind gut. Und ich verstehe, warum man sie in seiner Muttersprache schreibt. Aber das kann es für andere schwieriger machen, zu helfen. Auch wenn automatische Übersetzer inzwischen ganz gut sind, kann man sie nicht einfach so auf Quellcode und Kommentare anwenden - geschweige denn die Beschriftungen der Knöpfe. Ich habe einfach mal den grünen Knopf gedrückt. Zum Glück hat das hier gereicht :slight_smile:


Back to the problem, starting with another small hint:

You don’t have to wait a few minutes when you just change the line
dataPushInterval = setInterval(simulateDataPush, 2000);
to
dataPushInterval = setInterval(simulateDataPush, 50);

Or even better: Pull that out into a variable

const simulatePushDelayMs = 50;
...
dataPushInterval = setInterval(simulateDataPush, simulatePushDelay);

so that it can quickly and easily be changed for tests.


Now, the actual problem:

You can see that the CPU usage quickly raises to 100%. On my machine, it reaches 100% when there are about ~700 points in each of these 10 polylines.

When you do a profiler run with this, then you’ll see where the time is spent:

This may not immediately give the answer, but it can be a hint: When you create a default polyline in CesiumJS, then it uses an ArcType.GEODESIC by default. This means that it “subdivides” this line, so that it follows the curvature of earth - for example:

Computing this curvature is expensive. Your polylines only cover a small region, and the curvature of earth does not matter at this scale.

So you could insert…

// Erzeuge die Entität :-) 
const entity = dataSource.entities.add({
    id: `entity_${i}`,
    polyline: {
        positions: positionsProperty,
        width: 5,
        material: new Cesium.PolylineOutlineMaterialProperty({
            color: colors[i-1],
            outlineWidth: 2,
            outlineColor: Cesium.Color.WHITE
        }),
        clampToGround: false,
        arcType: Cesium.ArcType.NONE // -------------------------- ! THIS LINE
    }
});

… to set the arcType to NONE (meaning that all points are just connected with a straight line).

This way, on my machine, the CPU usage raises much slower, and only at ~1700 points per entity, it reaches 100%.

(There may be further room for optimizations. But regardless of how well you optimize it: There will always be a limit for the number of points. Maybe this improvement is already enough for your use-case…)

1 Like

Okay, thank you very much.

@Marco13 Hello agin, I have a another question that has a entity and the entity has a line ,I want the entity on the line, what can i do, I use many ways ,but the effect not abvious.

And I use the clampToGround property is true,and I use arcType: ArcType.NONE,But cesium will catch excetion make a toast to me ,must ArcType.GEODESIC OR ArcType.RHUMB,But I dont want use it,because draw line the cpu is very hight. Do you have some solutions?

Look a picture like this:

@xulayen

这听起来像是应该另开一个帖子讨论的问题。你应该尝试创建一个新帖子,并清晰地描述你的目标(如果不确定,你可以使用自动翻译工具)。如果可以,你应该添加一个包含最少必要代码的示例 Sandcastle,以展示你想要实现的功能以及导致异常的情况。

This sounds like something that should be asked in a new thread. You should try to create a new thread, with a clear description of the goal (and in doubt, you can probably use an automatic translator for that). If possible, you should add a Sandcastle that contains the smallest amount of code that is necessary to show what you’re trying to do, and that causes the exception.