1

Edit: This is still an issue but the program is not running slow as a service, it is running slow whenever NOT debugging. Running the diagnostics withotu debugging produced the same slowness. Peak cpu usage was 3%. That being said, any help with why this application would run slower WITHOUT debugging would be incredibly helpful as I google down the answer :)

Using C# and nuget'd topshelf, I have made a filewatcher/mover. There is an instance of filesystemwatcher which performs a long set of logic against a text document (between 100 kb > 8 MB). As it reads this text document (on a UNC network location) it will move the lines to one of 2 output files.

While I run the application through Visual Studios 2015 Community in debug mode or release mode, all the file actions and movement are as instant as they can get. Satisfied, I installed the application as a service on the same local machine. I turn on the service, key the trigger, and it starts moving the text files.... 50 kb every couple seconds.

1 MB file is taking a few minutes to parse through when it took a whopping 10 seconds when run through Visual Studios. The service is logged in as me, the same user in Visual Studio.

Any ideas? I have used topshelf for 3-4 other services, all utilizing filesystemwatcher or System.IO.FILE library (using the file library).

I can post some code if needed, just a bunch of if statements as it iterates through each line of text. The key issue being: works great from VS, not as a service. Why??

P.S. Just noticed that it took >4 minutes to move 1.4 mb's all to another file line by line, but went through all the logic after the move within the same SECOND.

I will post the snippet of topshelf configuration I am using, as it is not much:

    static void Main(string[] args)
    {
        HostFactory.Run(hostConfigurator =>
        {
            hostConfigurator.Service<Service>(serviceConfigurator =>
            {
                serviceConfigurator.ConstructUsing(name => new ASNWatcher());
                serviceConfigurator.WhenStarted(tc => tc.Start());
                serviceConfigurator.WhenStopped(tc => tc.Stop());
            });
            hostConfigurator.RunAsLocalSystem();

            hostConfigurator.SetDescription("HighRisk ASN Watcher");
            hostConfigurator.SetDisplayName("HighRiskASNWatcher");
            hostConfigurator.SetServiceName("HighRiskASNWatcher");
        });
    }

(yes, it says local system, I changed it post install to a proper user.)

Adding code that does the reading; very lite.

I am adding the piece of code that does the splitting:

     foreach (string line in File.ReadAllLines(fullPath))
            {
                if (line.StartsWith("ASIAUD"))
                {
                    foreach (var field in fileHelper856AUD.ReadString(line))
                    {
                        if (field.TradeID.TrimEnd() != TradeID && field.TradeID.TrimEnd() != CatalogTradeID)
                        {
                            logger.Info("Found thisTrade ID:  " + field.TradeID.TrimEnd());
                            thisorder = true;
                        }
                        else
                        {
                            logger.Info("Found thatTrade ID:  " + field.TradeID.TrimEnd());
                            thisorder = false;
                        }
                    }
                }
                if (thisorder )
                {
                    try
                    {

                        File.AppendAllText(newfile1, line + Environment.NewLine);
                    }
                    catch (Exception e)
                    {
                        //write to log,  also send an email alerting IT.
                    }
                }
                else
                {
                    File.AppendAllText(newfile2, line + Environment.NewLine);
                }
            }
            logger.Info("File Split Complete.  Deleting Original File...");
            File.Delete(fullPath);
  • It's really hard to say what's going on. You should add logging to your application and watch the log file during runtime. There can also be (an accidental) `Thread.Sleep` that you somehow missed during debugging. If you have many threads, double check that they aren't waiting much for each other. – oleksii Oct 27 '15 at 15:32
  • Maybe the service has a low priority? – MikeH Oct 27 '15 at 15:41
  • There is logging for each step, I added a piece of logging to the foreach as it steps through the document and it is doing all the lines, just incredibly 'slow'. Logging doesn't show any difference between as a service or run through VS other than the timestamp grows a lot faster. There are no sleeps. – Kurtis Cochrane Oct 27 '15 at 15:43
  • @oleksii I added the snippet of code, no sleeps, nothing crazy, just read a line and if a variable is active, put it in this doc, if it's not, put it in this doc. – Kurtis Cochrane Oct 27 '15 at 15:48
  • @mikeH can you expand on that? I've never had to fiddle with priorities in the past. – Kurtis Cochrane Oct 27 '15 at 15:49
  • It won't be thread or process priority - unless you see that your CPU usage is 100% for all the cores. Most of the time your PC will spend in Idle, meaning all threads with low priority have been executed. Check all your `catch` blocks - add logging there, it can be a problem of permissions. When you debug application you run it as a user, say Admin. When you run a service it runs under different username, such as LocalSystem and that may not have permissions to do stuff. – oleksii Oct 27 '15 at 15:53
  • @oleksii I don't see how a permissions issue would cause a file transfer to be slower? It would either have permission to the file/location or not. It does. I've ran the service as 4 users, 2 don't have permission to the location and immediately failed. MikeH mentioned priority of the service, and someone near my cubicle mentioned service priority; it sounds like THAT may be the problem as it is definitely not capping CPU. – Kurtis Cochrane Oct 27 '15 at 15:57

1 Answers1

1

I was able to find some hints and get in the right direction through some other Stack articles once I discovered it wasn't the service piece that was hurting it, just running it without the profiler in general.

Why does my program run way faster when I enable profiling?

The reason is because when you run your application within Visual Studio, the debugger is attached to it. When you run it using the profiler, the debugger is not attached.

If you press F5 to run your program, even with the Release build, the debugger is still attached.

If you try running the .exe by itself, or running the program through the IDE with "Debug > Start Without Debugging" (or just press Ctrl+F5) the application should run as fast as it does with the profiler.

Community
  • 1
  • 1