Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


Recommended Posts

Hi @jgab

Is it possible to get the distance to home with iOSLocator scene and display in EventRunner scene ?

Link to comment
Share on other sites

  • Topic Author
  • 12 hours ago, domin12 said:

    Hi @jgab

    Is it possible to get the distance to home with iOSLocator scene and display in EventRunner scene ?

    Well, kind of. I have updated iOSLocator. First I have added the logic for presence detection that was something typically done in the scenes using iOSLocator.

    That means that iOSLocator now sends out 2 type of events to subscribing scenes

    Please login or register to see this code.

    So, #location is as before, but now also contains the distance to the place. Also when place is "away" you get a distance from "Home".

    When every user is away from home a #presence{state='allaway'} is sent out.

    When someone is home a "#presence{state='home',who=...} is sent out.

     

    To change the interval locations are polled change

    Please login or register to see this code.

    in the beginning of the file.

     

    Locations are checked in the order they are defined in the 'places' structure. That means that places can be inside other places. So there could be "rings" defined around a place and as long as the rings are defined in the order smaller to larger there will be events sent when the user enters an "inner" ring. In this way there can be some kind of distance to home triggers/events. However, iCloud location service is not always that accurate...

    Edited by jgab
    Link to comment
    Share on other sites

    @jgab

     

    here is suggestion for small improvements - automatic restart:

     

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • Ok, it will restart when it gets a trigger. 

    Isn't it enough to do 

    Please login or register to see this code.

    and handle it as a manual "other" start?

    The other things is that we drop the event. Maybe re-post with  the startScene ?

    Let me think about it so there are no unwanted side-effects.... maybe it can be an option?.

    Thanks /J

    Link to comment
    Share on other sites

    OK

    I think, if there will be only 

    Please login or register to see this code.

    it could be influence ie. second manual start ...

     

    drop events - we lost even more events without this small hack :)

    Edited by petrkl12
    Link to comment
    Share on other sites

    @jgab

     

    Hello,

     

    I had one big ER scene with huge number of trigered devices and I had problem with number of scenes>10. I have tried to avoid it so I change your MAILBOX function and I have tried to create a bit different mechanism with more MAILBOXes (in attached file default is 30 - attention: it will create 30 global variables!). Now even under big load it looks that it works with scenes < 10 ....

    Could you please check it if this is correct from your point of view?

    Thanks

     

     

    Please login or register to see this attachment.

    Edited by petrkl12
    Link to comment
    Share on other sites

  • Topic Author
  • 18 hours ago, petrkl12 said:

    @jgab

     

    Hello,

     

    I had one big ER scene with huge number of trigered devices and I had problem with number of scenes>10. I have tried to avoid it so I change your MAILBOX function and I have tried to create a bit different mechanism with more MAILBOXes (in attached file default is 30 - attention: it will create 30 global variables!). Now even under big load it looks that it works with scenes < 10 ....

    Could you please check it if this is correct from your point of view?

    Thanks

     

     

    Please login or register to see this attachment.

     

    Thanks for your "experiment" :-)

    It's the question of bandwidth and throughput. Unfortunately we can never have more than 10 simultaneous instances - the bandwidth. However  if we can get the producers to post (offload) their events quicker and the consumer to consume the events quicker we can have better throughput.

     

    I think the way is to divide up the scene in rooms and triggers per rooms if there are too many. 10 motion sensors each in 10 rooms and create aggregated events for movement in rooms.

     

    I did experiment with multiple mailboxes early on but I never hit the roof with my scenes so I scrapped it. 

     

    But you are right with your experiment, the bottleneck are the producers that can't post their events. The consumer can normally cope. However, it was setup so that the consumer only read 4 events/s (250ms), which means that if events come quicker a backlog will be built up.

    Your idea to let the consumer poll more often when there are incoming events I have copied. That's in general a good idea.

     

    So I implemented multiple mailboxes like you have, but slightly different. The consumer will loop over all mailboxes and read events and then make a delay. If there were events the delay will temporarily be set to zero and then the delay will build up to 250ms if there are no events.

    The producers will "aggressively" loop over mailboxes to find an empty box, and if not found start to back-off (delay of 100ms)

     

    If you test it with a scene sending events you can test the performance:

    Please login or register to see this code.

    and in the EventRunner have a handler:

    Please login or register to see this code.

    (the events will not come in order so it's good to have a total counter to see if all events came through) 

     

    The code now can sustain this (100 events, 65ms, 30 mailboxes). If we lower the sleep to 60 we start to drop 3% of the events with 30 mailboxes. (It can sustain burst of #_NUMBEROFBOXES events). It of course depends a bit on the average load of the box.

     

    Your code as it stands drops 5% of events at sleep of 150ms. I think it is because the consumer is not as aggressive when events are coming in and that your mailbox selector may take a few ms too long (it can also be more aggressive to find an empty box). 

    Anyway, we can never get it perfect. Try the code I've attach and see how it works for you - and see if you can optimise it further.

    I need to make some more tests (cpu load etc) before feeling safe to include it.

    Thanks again for taking the time to improve the code!

    Please login or register to see this attachment.

     

    /J

     

    Btw, why did you define NewSleep with os.clock? I suspect finbaro:sleep is implemented that way. Did you see any improvement?

    Edited by jgab
    updated experimental code
    Link to comment
    Share on other sites

    Thanks, I'm going to do some test in my enviroment

     

    NewSleep - I have red the fibaro:sleep is blocking any other processes but I don't know if it's true or not ...

     

    Link to comment
    Share on other sites

  • Topic Author
  • 11 minutes ago, petrkl12 said:

    NewSleep - I have red the fibaro:sleep is blocking any other processes but I don't know if it's true or not ...

     a busy-wait starves setTimeout processes too. Would be interesting to understand how they implemented them... co-routines?

    Please login or register to see this code.

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    I have tested your EXperimental code - here is result:

    Please login or register to see this code.

    It looks that your polling is OK, but finding right MAILBOX is not too efficient as you can see ... My function is better :) 

    As you can see you are trying to get MAILBOX for process 2 (second instance of process) too long, same for 3rd etc. I was able to get limit 10 processes very quickly :(

     

    Please check my code how I get right mailbox to minimize chance to have same mailbox for more scenes

    and also check your setting ticket - i think it's not too correct - also check how I do it - first is selecting right mailbox and then I wait when mailbox will be free - in your case I think you could lost some mailboxes ...

     

     

    Link to comment
    Share on other sites

    here is your code with my debug rows:

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • Well, I don't sleep between trying mailboxes - trying 4 times in a non-sleep loop is not inefficient. I believe it's better to quickly search for a free mailbox than to select a random box and wait on that.

    What's the ticket issue?

    How did your test when you reach 10 instance limit?

    Have you run the test below?

    Please login or register to see this code.

    I manage as low as 65ms without dropped events.

    Link to comment
    Share on other sites

    I have tested in my real scene. It generates a lot of events from Hue virtual devices - so testing environment is OK (I can press a lot of hue buttons in one time)

     

    btw. I don't do it random - it's a last thing - first I do it via scenecount than I do scenecount+10 and last thing is a random selection ... Most important is not used same 

     

    I did my optimization whole day I tried similar code as your in my first attempt but with not good result ...

     

    Link to comment
    Share on other sites

    Most important is not using same sequence for generating right mailbox in every scene. It was visible in log:

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    Another log  - where you can see how processes try to lock same mailboxes - so it means that 4 processes thought that MAILBOX227_3=="" and tried to lock it and then in same second again for MAILBOX227_4 etc. - this is not effiecient and result is 9 running scenes ...

     

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • But the consumer processes mailboxes low to high and producers look for mailboxes low to high. Don’t worry that the prodecer tries 5-10 mailboxes before finding an empty. There’s no wait and it goes quickly. The important thing is for producers to quickly post the event and not having to sleep. You decide on a mailbox and if it’s #numberofhandlers you sleep or take a random box, which can be occupied. I see this when I send a lot of messages (high throughput). When a higher percent of mailboxes are filled you get delays.  I hit 10 instances already at 150ms delays between triggers.

    My sequential search is always worst case N no matter how filled the boxes are.

     

    Anyway, I will make some more benchmarks tomorrow and see if some randomness can help. Obs, is.clock always start at 0 for a new instance - so it’s not always good for seedlings.

    7 minutes ago, petrkl12 said:

    Another log  - where you can see how processes try to lock same mailboxes - so it means that 4 processes thought that MAILBOX227_3=="" and tried to lock it and then in same second again for MAILBOX227_4 etc. - this is not effiecient and result is 9 running scenes ...

     

    Please login or register to see this code.

     

     

    Doesn’t matter how quick they are if the instances are started up almost at the same time. You can still get 9 parallel instances if they are started up almost the same time. 

    Link to comment
    Share on other sites

    OK, please try to use fibaro:countScenes() as best way how to get first empty mailbox and then you can play with sequences or random generating next free mailbox

     

    I know that it's a quick test but not too quick to avoid to run more than 10 processes :(

     

    Link to comment
    Share on other sites

  • Topic Author
  • The only thing that’s important is how often a producer sleep. Measure that in the benchmarks.

    Link to comment
    Share on other sites

    here is my last version - I'm using your polling function and I have changed a bit my mailbox functions

    btw:  is.clock for randomise is ok, it produces every time different timing (added to log)

     

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

  • Topic Author
  • 12 hours ago, petrkl12 said:

    Please login or register to see this attachment.

    Ok, I run a couple of benchmarks. I took away your debug messages as I worried that they would slow down the producers and affect the results.

    This is what I get.

    Your results (PK)

    Please login or register to see this attachment.

    and my results (JG)

    Please login or register to see this attachment.

    The table's x axis is the delay between sent message in ms, the y axis is number of consecutive messages sent. The % value is how many messages comes through, the higher  the better.  At least 3 tests for each setup as results vary between runs. The client code is:

    Please login or register to see this code.

    A strange thing in your results are that you drop events even at longer delays even though you don't hit 10 instances.

    However, we will always hit the 10 instances if the messages come quick enough - when we get down to 30ms and below between the messages and burst over 10 messages we will always hit the 10 instance limit. There is no way around it as it takes 50-100ms just to start up an instance for the HC2, for any type of scene. 

    So it's just trying to do the best we can - and it's often better than non-ER scenes as we try to terminate as quick as possible giving room for additional instances to spin up.

    I did the test with my code from yesterday that seeks mailboxes sequentially and a new version that starts with the countscene number. I was expecting slightly better results in the short and bursty tests, but it was very limited. The only test was the 15msg, 0ms that managed to get 1 additional message through on average. On longer sequences I don't see a difference. So, I'm not sure the complexity justifies it.

     

    It seems like you have a live setup where my code hits the 10 instances and your version does not. Can you describe the setup a bit better so I can see if I can replicate it under controlled circumstances. One thing that is different is that in a live setup the consumer is doing real work so that eats resources too, i.e. the consumer will not be able to handle as many events/s.

     

    In general I would not rely on scenes shooting 10's of messages with less than 50ms between. There must be ways to recode it to aggregate values locally inside scenes/VDs and send these aggregated events less often. The Hue thing should be polled from a scene and aggregated events sent to other scenes - you will never trigger 10 motion sensor or push 10 buttons at the same time - and if so, you should be able to aggregate it to a single event for sending it to other interested scenes.

     

    Btw, this is really really fun! :-)

     

     

    Edited by jgab
    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...