Help me with Decision about increasing Application speed

This mean my problems in my code :frowning:
I should now understand all suggestion, test them and give feedback to all

2 Likes

There is no free lunch that tastes better than taking something you built, doing some small changes and seeing a 1000X improvement in performance.

Optimization is like finish work for a project, you might think about it while you are building, but you still need to polish at the end to make it look really good and it can be very satisfying.

6 Likes

If you have specific forms that are slow, and can create a smaller clone that shows the performance problem, post a clone link here and you’ll get more specific advice about improving the performance.

3 Likes

Thank Ian for your suggestions !
I will try to implement
I have code on Server and Data on databases - Forms calls and uses Data
that cause low running speed
Now I am thinking How to organize code again with less anvil.servercalls

TIL (on Reddit) that there’s a term for that strategy, or for a piece of it.

Edit: This is supported by Anvil’s Portable Classes. A DTO is likely to be composed of multiple values, of various types. All of them need to know how to [de]serialize themselves for transfer over the wire. With Anvil’s support, you can build custom DTOs (and subobjects) that know how to do just that.

3 Likes

It would be great to have a feedback about ALL round trips, not only about the ones we expect: Add logging of all round trips

1 Like

When I placed

print(datatime.daytime.now())

all over, it made me realize where the time goes There were usually simple fixes, as already mentioned in preceding posts.

4 Likes

Every generation of programmers rediscovers this for themselves: the only effective way to optimize begins with measurement.

Glad you stuck with it. :smiley:

2 Likes

A post was merged into an existing topic: Latency dependence on geographical location

It’s been a while but if anybody has similar issues: -
I would introduce logging to your server functions with microseconds so you know what is taking a longtime, something like this: -

def write_log_msg(str_func_name, str_state, str_type, str_message):
  '''
  str_function   string   calling this function
  str_state   string   state calling function is in i.e. start or executing or exiting
  str_type   string   INFO or DEBUG or WARN or EXCEPTION 
  str_message   string   only required by the state executing; the text of the log message to be logged
  e.g. puts log line like '(05-05-2023,09:29:17.513506),(anvil username),function_name,INFO,Entered' 
  to the Anvil log 
  '''
  obj_current_time = datetime.now()
  if str_state == 'entry':
    print(f'{obj_current_time.strftime("%d-%m-%Y,%H:%M:%S.%f,(%z)")},{anvil.users.get_user()},{str_func_name},INFO,Entered')
    return
  elif str_state == 'exit':
    print(f'{obj_current_time.strftime("%d-%m-%Y,%H:%M:%S.%f,(%z)")},{anvil.users.get_user()},{str_func_name},INFO,Exiting')
    return
  elif str_state == 'executing':
    print(f'{obj_current_time.strftime("%d-%m-%Y,%H:%M:%S.%f,(%z)")},{anvil.users.get_user()},{str_func_name},{str_type},{str_message}')
    return
  else:
    print(f'{obj_current_time.strftime("%d-%m-%Y,%H:%M:%S.%f,(%z)")},{anvil.users.get_user()},{write_log_msg.__name__},DEBUG,Incorrectly Called str_func_name=({str_func_name}), str_state=({str_state}), str_type=({str_type}), str_message=({str_message})')
    return

then whenever I enter, leave (a function) or face an exception I record the time, for example:-

def get_db_conn():
  ''' 
  creates a connection (object) to MySQL DB 
  str_db_host   string   global variable is the hostname
  int_port  integer   global v ariable for the DB listener port
  returns   obj_dbconn   a pymysql connection object
  '''
  write_log_msg(str_func_name=get_db_conn.__name__, str_state=entry)
  try:
    obj_dbconn = pymysql.connect(host = str_db_host,
                               port = int_port,
                               user = anvil.secrets.get_secret('db_user'),
                               password = anvil.secrets.get_secret('db_password'),                               
                               cursorclass = pymysql.cursors.DictCursor,
                               #ssl={"fake_flag_to_enable_tls":True})
                               #ssl={"ssl_verify_cert":True},
                               ssl={"ssl_verify_identity":True})
    
  except pymysql.err.OperationalError as error:
    # an exception creates a 4 item list that can contain info it would be pymysql.err.args[] 0to4
    write_log_msg(str_func_name=get_db_conn.__name__, str_state='executing', str_type='DEBUG', str_message='Got MySQL Error Code=({error.args[0]}), MySQL Error Text=({error.args[1]})')
    write_log_msg(str_func_name=get_db_conn.__name__, str_state='exit')
    exit()
  else:
    write_log_msg(str_func_name=get_db_conn.__name__, str_state='exit')
    return obj_dbconn

This will give you a starting point to see which functions are slow and then you can introduce further logging to figure which part is particularly slow…

The code didn’t quite format well but it will give someone something to hack to their liking…

EDIT
edited to format the code correctly

2 Likes

For readability, you can (and should) wrap your code as shown below:

``` python
your code here
```

Without that, indents and some punctuation will be lost.

1 Like

Thanks, updated the post…

So much clearer for everyone. Thank you!

If you are really into programming and performance. Get the Python libraries to do the computation as they are compiled C code. For example matrices and arrays use Numpy rather than writing Python code/lists to do it.

https://www.youtube.com/watch?v=tD5NrevFtbU
Balance clean code vs. performance

OOP is not always the best approach. I only use OOP if I see it has objects, structured programming, implement data-orientated programming concepts
https://www.youtube.com/watch?v=rlrDefjpNqY&t=194s
You use Python data-classes instead of C/C++ structs

https://www.youtube.com/watch?v=08CWw_VD45w&t=370s
functional programming and how we can use these ideas in Python: -
https://www.youtube.com/watch?v=4B24vYj_vaI

2 Likes