开发者

.net section running real slow

开发者 https://www.devze.com 2023-03-09 08:36 出处:网络
Update: The answers from Andrew and Conrad were both equally helpful.The easy fix for the timing issue fixed the problem, and caching the bigger object references instead of re-building them every tim

Update: The answers from Andrew and Conrad were both equally helpful. The easy fix for the timing issue fixed the problem, and caching the bigger object references instead of re-building them every time removed the source of the problem. Thanks for the input, guys.

I'm working with a c# .NET API and for some reason the following code executes what I feel is /extremely/ slowly.

This is the handler for a System.Timers.Timer that triggers its elapsed event every 5 seconds.

private static void TimerGo(object source, System.Timers.ElapsedEventArgs e)
    {
        tagList = reader.GetData(); // This is a collection of 10 objects.

        storeData(tagList); // This calls the 'storeData' method below

    }

And the storeData method:

private static void storeData(List<obj> tagList)
    {
        TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
        long timestamp = (long)t.TotalSeconds;

        foreach (type object in tagList)
        {
            string file = @"path\to\file" + object.name + ".rrd";

            RRD dbase = RRD.load(file);

            // Update rrd with current time timestamp and data.
            dbase.update(timestamp, new object[1] { tag.data });
        }
    }

Am I missing some glaring resource sink? The RRD stuff you see is from the NHawk C# wrapper for rrdtool; in this case I update 10 different files with it, but I see no reason why it should take so long.

When I say 'so long', I mean the timer was triggering a second time before the first update was done, so eventually "update 2" would happen before "update 1", which breaks things because "update 1" has a timestamp that's earlier than "update 2".

I increased the timer length to 10 seconds, and it ran for longer, but still eventually out-raced itself and tried to update a file with an earlier timestamp. Wh开发者_StackOverflowat can I do differently to make this more efficient, because obviously I'm doing something drastically wrong...


Doesn't really answer your perf question but if you want to fix the rentrancy bit set your timer.AutoRest to false and then call start() at the end of the method e.g.

private static void TimerGo(object source, System.Timers.ElapsedEventArgs e)
{
    tagList = reader.GetData(); // This is a collection of 10 objects.

    storeData(tagList); // This calls the 'storeData' method below
    timer.Start();
} 


Is there a different RRD file for each tag in your tagList? In your pseudo code you open each file N number of times. (You stated there is only 10 objects in the list thought.) Then you perform an update. I can only assume that you dispose your RRD file after you have updated it. If you do not you are keeping references to an open file.

If the RRD is the same but you are just putting different types of plot data into a single file then you only need to keep it open for as long as you want exclusive write access to it.

Without profiling the code you have a few options (I recommend profiling btw)

Keep the RRD files open

Cache the opened files to prevent you from having to open, write close every 5 seconds for each file. Just cache the 10 opened file references and write to them every 5 seconds.

Separate the data collection from data writing

It appears you are taking metric samples from some object every 5 seconds. If you do not having something 'tailing' your file, separate the collection from the writing. Take your data sample and throw it into a queue to be processed. The processor will dequeue each tagList and write it as fast as it can, going back for more lists from the queue.

This way you can always be sure you are getting ~5 second samples even if the writing mechanism is slowed down.


Use a profiler. JetBrains is my personal recommendation. Run the profiler with your program and look for the threads / methods taking the longest time to run. This sounds very much like an IO or data issue, but that's not immediately obvious from your example code.

0

精彩评论

暂无评论...
验证码 换一张
取 消