Saturday, March 19, 2022

Rust- Generic type T is super set of &T and &mut T

T includes both & T and &mut T

I had a misconception in Rustlang that the generic types &T and &mut T are  not subset of generic type  T. It turned out to be totally wrong.  Generic Type T includes both &T and &mut T. It is quite easy to get believed that  &T and &mut T are not part of T .

but actually it is as below






I think below is the proper definition of reference types in Rust - &mut T - is the exclusive reference to the value and &T is the shared reference to the value.

When you have exclusive reference(&mut T)  to a value, you can mutate it and guarantees no other references for it at that point of time.


Example:




In the example program the function 'print' has type with trait debug and it is a generic type T.
We are able to pass the &v, &mut v and v to the print function. Meaning the  type check  T  is able to accept the  exclusive reference and shared references.


Reference:






 

 

Saturday, February 5, 2022

May be Rust can speed Django projects ?

I was trying to plugin pyo3 build Rust '.so' files(dynamic libraries) to Django. Looks like its not that difficult, we can copy and paste the file to Django folder. Once the file avaliable in Django folder , it will allow us to import the Rust dynamic library functions to your Django views.

Step1: create the Rust program as library using pyo3 and build the file using cargo build. Remember to put the crate-type "cdylib". I used manual build and copy .so file . Remember to keep the python module name and .so file name as same. https://pyo3.rs/v0.15.1/
Step2 Create your normal Django helloworld App
Step3 Copy the .so file to your Django project folder and import it in your views.py file, then you should be able to run django server .

Saturday, March 27, 2021

Load tested my Django site

Benchmark HTTP Latency with wrk

 

Recently I started reading through Actix-web, Django,tokio etc, so thought of bench marking the latency. Started with Django as it is pretty easy get started and its in python.

 

looks like wrk ( https://github.com/wg/wrk )  is a good tool to start with. 

"wrk is a modern HTTP benchmarking tool capable of generating significant load when run on a single multi-core CPU. It combines a multithreaded design with scalable event notification systems such as epoll and kqueue."

Installation also pretty easy in Ubuntu:


1. sudo apt-get install build-essential libssl-dev git -y

2. git clone https://github.com/wg/wrk.git wrk

3. cd wrk/

4. sudo make

5. sudo cp wrk /usr/local/bin


Run your Django server using 


python manage.py runserver

 

then another terminal you can run your wrk as below

 

wrk -t12 -c400 -d30s --latency http://127.0.0.1:8000

 I only had single page and not much custom files in the Django and not data base request. Not sure this is a good response .. Please share your comments.



 Also got something like this Django logs as well.

[27/Mar/2021 17:39:14] "GET / HTTP/1.1" 200 196
[27/Mar/2021 17:39:14] "GET / HTTP/1.1" 200 196
Exception happened during processing of request from ('127.0.0.1', 33856)
[27/Mar/2021 17:39:14] "GET / HTTP/1.1" 200 196
[27/Mar/2021 17:39:14] "GET / HTTP/1.1" 200 196



I am planning to try this same experiment with actix-web and Rust , will share once I have something..