0

I have a application that is part console application written in C# and part PHP web application.

The console application connects to multiple serial devices and reads data pertaining to moisture and temperature in a Grain Dryer. [The application] then stores the information in a MySQL database.

The PHP reads the MySQL database and displays the information to the user (simple).

I have also written a PHP script to email / sms the user if one of the temperatures of moistures reaches alarm levels (alarm levels are read off the device and stored in the MySQL database).

Currently, I post to the PHP page with each "update" from the device (10 seconds) and check each value against it's limits. If an alarm is required I send it off and update the database indicating an alarm has been sent.

This looks like (pseudo code)

IF moisture IS GREATER THAN moisture_high_critical_limit THEN
  IF moisture_high_critical_alarm_has_been_sent IS FALSE THEN
    SEND_ALARM();
    moisture_high_critical_alarm_has_been_sent = TRUE;
  END IF
ELSE IF moisture IS GREATER THAN moisture_high_warning_limit THEN
  IF moisture_high_warning_alarm_has_been_sent IS FALSE THEN
    SEND_ALARM();
    moisture_high_warning_alarm_has_been_sent = TRUE;
  END IF
ELSE IF moisture IS LESS THAN moisture_low_critical_limit THEN
  IF moisture_low_critical_alarm_has_been_sent IS FALSE THEN
    SEND_ALARM();
    moisture_low_critical_alarm_has_been_sent = TRUE;
  END IF
ELSE IF moisture IS LESS THAN moisture_low_warning_limit THEN
  IF moisture_low_warning_alarm_has_been_sent IS FALSE THEN
    SEND_ALARM();
    moisture_low_warning_alarm_has_been_sent = TRUE;
  END IF
END IF

The above IF statement is repeated for every temperature and moisture reading from the device, which can be quite a few.

Should this type of processing be happening in my Console application? Then if a Alarm notification is required post to the page only to send the email / SMS?

Processing this information in the console application will reduce my total number of handshakes with the MySQL Database. But if the MySQL Database is hosted locally will this have much impact?

I will eventually benchmark both (if it seems the doing the processing in the console application is in fact the right approach), however there is a lot of code to port over so I thought I'd come here and ask first.

3 Answers 3

14

From your description it sounds like your app performs a lot of DB and other network traffic. That is supposed to take magnitudes more time than executing the above code a few million times, be it in PHP or C#.

I would suggest you profile your app first, to see where the real bottlenecks are your app spends the most time, then focus on these areas. Typically, optimizing the way you query/update the DB (e.g. minimizing the number of roundtrips to the DB by performing batch updates instead of series of singular updates whenever you can) saves you way more time than any local processor time optimization.

2
  • It's not that I have run into many bottlenecks yet, I am just worried that once it is scaled up to a few hundred users I may see this. Would I be better severed to reduce my total number of posts from C# to PHP? (The total number of DB calls i'm trying to cut down everywhere I can, however I'm trying to offer live data to the end user.. so this is unavoidable without the use of COM Interop or some other similar service, and i'm not going down that road)
    – rlemon
    Sep 13, 2011 at 14:30
  • 3
    @rlemon, even if you don't have acute performance issues yet, your app still spends its time somewhere - according to the old rule of thumb, it probably spends 80% of its time executing 20% of the code. Thus it is absolutely pointless even thinking of improving the performance of the other 80% of the code. You need to find that critical 20 / 10 / 5% of the code and focus on that. Sep 13, 2011 at 14:34
3

I agree with @Péter Török regarding the optimization points, but in an effort to provide a small code optimization you might want to consider the Windows Workflow Foundation for C#. It was designed with just this kind of optimized flow control in mind. We're currently employing it to make determinations on data and execute data insertions, perform notifications, etc based on the results of a data analysis.

1

From a performance standpoint if you can query the database or a config file for the critical values and then pre-process the incoming data before it is sent to the database, then you should have a very high performance sensor and code. This type of conditional logic shouldn't take long to go through and if you want to pick up speed you can always have it recoded in a compiled language. You can certainly send an SMTP message from the code when the sensor picks up a bad set of data.

I would have suggested looking at a MYSQL Insert trigger to run your logic, but the MYSQL references I consulted suggested that using an SMTP interface to automatically send email from the server trigger or stored procedure was not as refined as Microsoft's or ORACLE's offerings. Perhaps this is something to consider for the future if your needs grow and you want to have less intelligent sensors deployed in remote locations to pull/push data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.