sikuli slows down to a crawl after running for some time

Asked by geo

Hi,

    I reinstalled windows 10 and also sikuli. I'm running a program that automates an egg finding game on facebook. This program used to work with no problems but in this new installation the program slows down to an unbearable crawl after some time. I think about after 700 egg finds or so. I tried going back to an older version of sikuli (1.1.0) but it has the same problem. The program runs faster than before and then after a couple of stages are cleared it takes forever to find anything. Again, I have not changed anything in the program. running java version 1.8.0_131 on windows 10.

also, if I stop the program via shidt-alt-c and restart it the problem goes away for a while. Any suggestions? I know this sounds like a program (coding) issue but, I have not changed anything and it worked find on a previous install of win10 and ubuntu 16.

also also... once you clear a stage you have to find a different item before you go back to the regular egg finding game. This runs a different part of the code so you would think that since it brakes away from the egg finding loop then if there was a loop issue it would go away.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
geo (mrgees100peas) said :
#1

Still not solved

    I tried disabling options in the preference menu. I also tried exiting while loops after a certain count (exit early) in case it was being stuck in a loop and that did not work either. I then decided to write a comment to a file on every section of my program. Nothing abnormal showed up. I though I would see my "could not find egg" message but, the program was working fine as far as code execution order goes. It simply taking too long while doing a search. What could cause this?

Revision history for this message
geo (mrgees100peas) said :
#2

The mystery deepens. I put some timers so that I could see where in the program there is a slow down. There is something wrong that happens at the end of the while loop but only after this code has ran hundreds of times. Its really odd because I put the write to file line in the wrong spot so what I end up seeing or rather what I should end up seeing is the same value for the last two print lines. I should be seeing something like .....
      while loop time --> 0.22
      after check position time --> 0.22

But!! as you can see towards the end the "after check position time" takes considerably longer than the "while loop time" YET!!! they happen right after each other. How do you explain that???

    #check if at least one egg exists
    if region1.exists(Pattern("1492966400130.png").similar(0.59),0):
        file.write("FOUND EGG\n") #DEBUG
        foundEggTime = time.time() #DEBUG

        while region1.exists(Pattern("1492966400130.png").similar(0.58),0):
            startWhileTime = time.time() #DEBUG
            file.write("-------EGG WHILE LOOP---------\n") #DEBUG
            click(region1.getLastMatch()) # avoids another find() operation
            endWhileTime = time.time() #DEBUG
            checkLastMouseLocation(Env.getMouseLocation())
            endWhileTimeCheck = time.time() #DEBUG
            file.write(" while loop time --> %.2f \n" % (endWhileTime - startWhileTime))
            file.write(" after check position time --> %.2f \n" % (endWhileTimeCheck - startWhileTime))

        finalTime = time.time()
        file.write(" ***exit found egg time --> %.2f \n" % (finalTime - foundEggTime))
        file.write("-------EXITING FOUND EGG--------\n")
        file.write("--------------------------------\n")

Here is the results print to file.

-------EGG WHILE LOOP---------
Mouse last location
checkRefresh
    while loop time --> 0.22
    after check position time --> 0.22
-------EGG WHILE LOOP---------
Mouse last location
checkRefresh
    while loop time --> 0.23
    after check position time --> 0.23
-------EGG WHILE LOOP---------
Mouse last location
checkRefresh
    while loop time --> 0.22
    after check position time --> 0.22
    ***exit found egg time --> 1.38
-------EXITING FOUND EGG--------
--------------------------------
FOUND EGG
-------EGG WHILE LOOP---------
Mouse last location
checkRefresh
    while loop time --> 0.22
    after check position time --> 0.22
-------EGG WHILE LOOP---------
Mouse last location
checkRefresh
    while loop time --> 0.22
    after check position time --> 0.22
    ***exit found egg time --> 0.77 <--EXPECTED run time
-------EXITING FOUND EGG--------
--------------------------------
============================!!!!!!!!ELSE i.e FAIL!!!!!!!============================checkRefresh
HUNT AGAIN
get RARE egg/nMouse last location
checkRefresh
FOUND EGG
-------EGG WHILE LOOP---------
Mouse last location
checkRefresh
    while loop time --> 0.22
    after check position time --> 3.23 <--- WHY IS THIS HAPPENING
-------EGG WHILE LOOP---------
Mouse last location
checkRefresh
    while loop time --> 0.22
    after check position time --> 3.22
-------EGG WHILE LOOP---------
Mouse last location
checkRefresh
    while loop time --> 0.22
    after check position time --> 3.22

Revision history for this message
RaiMan (raimund-hocke) said :
#3

 endWhileTime = time.time() #DEBUG
 checkLastMouseLocation(Env.getMouseLocation())
 endWhileTimeCheck = time.time() #DEBUG

... so the "mystery" happens in checkLastMouseLocation()
What are you doing there?

To get nearer to the problem, take care that you are using version 1.1.1

Revision history for this message
geo (mrgees100peas) said :
#4

That function checks if the mouse has not moved. If it hasn't after so many times the program will refresh the browser. This is because sometimes the game doesn't show any eggs or other similar errors. here is the code.

def checkLastMouseLocation(currentMouseLocation):
    file.write("Mouse last location\n") #DEBUG
    global lastMouseLocation
    global sameLocationCounter

    #print("curr --> %s : last --> %s" % (currentMouseLocation, lastMouseLocation))
    if currentMouseLocation == lastMouseLocation:
        sameLocationCounter += 1

    else:
        lastMouseLocation = currentMouseLocation
        sameLocationCounter = 0

        checkLastMouseLocation()

and here is the refrseh code. Nothing fancy

#too many fails. Refresh the browser
def checkRefreh():
    file.write("checkRefresh\n") #DEBUG
    global notFoundCounter
    global sameLocationCounter
    global inActiveMode

    if ((notFoundCounter >= 30) or (sameLocationCounter >=20)):
        #popup("REFRESH!!!!") #DEBUG
        if region5.exists("1458036200954.png"):
            click(region5.getLastMatch())
            wait(4)
            hover(Location(0,0))
            wait(4)
            print("----PAGED REFRESHED----")
            #reset flags and counter
            notFoundCounter = 0
            sameLocationCounter = 0
            inActiveMode = False

Revision history for this message
Best RaiMan (raimund-hocke) said :
#5

not sure if this is really your code:

def checkLastMouseLocation(currentMouseLocation):
    file.write("Mouse last location\n") #DEBUG
    global lastMouseLocation
    global sameLocationCounter

    #print("curr --> %s : last --> %s" % (currentMouseLocation, lastMouseLocation))
    if currentMouseLocation == lastMouseLocation:
        sameLocationCounter += 1

    else:
        lastMouseLocation = currentMouseLocation
        sameLocationCounter = 0

        checkLastMouseLocation() ### this seems to be a copy&paste error, because it cannot be in the code (crashes)

so your checkLastMouseLocation() is essentially this:

def checkLastMouseLocation(currentMouseLocation):
    global lastMouseLocation
    global sameLocationCounter
    if currentMouseLocation == lastMouseLocation:
        sameLocationCounter += 1
    else:
        lastMouseLocation = currentMouseLocation
        sameLocationCounter = 0

a problem here is, that currentMouseLocation and lastMouseLocation are objects and
currentMouseLocation == lastMouseLocation

checks wether both reference the same object and does not check any object content for equal, like you might expect.
Equality would be:
loc1.x == loc2.x and loc1.y == loc2.y

A general problem of your code is the extensive use of globals in functions, which makes it very hard to track down problems becaus of the possible side effects.

Best practice is, that functions take a defined set of parameters and return a defined return value, without any side effects. Hence such a function will always return the same with the same parameters. This is not guaranteed with your functions.

So until something else shows up, I say:
the problem is your
checkLastMouseLocation()

Revision history for this message
geo (mrgees100peas) said :
#6

Yes, it was a typo. and yes, the problem is in the checkLastMouseLocation() code. What I did was to comment out the function call and put a return statement in the function definition in case some other part of the code calls it. I then let the program run for about an hour and a half and the problem never showed up. It would usually show up after about 7 to 10 minutes. What I find odd is that this code is at least 2 years old and this problem never showed up before. So, why is it a problem now?!? Anyways, I'll change the code taking your advice into account. Thanks.

Revision history for this message
geo (mrgees100peas) said :
#7

Thanks RaiMan, that solved my question.