Table of Content
How to run native Go code In Android and bench results!
I was wondering today about how Android faces some of the cryptographic challenges of current Blockchain implementations. In order to solve this question, I just tried the best way I know: code, benchmark and plot it.
How a single Ethereum address is created?
Following the official Geth Ethereum implementation, an account creation is defined as follows in Go:
|
|
To generate the so typical Ethereum address and private key encoded as hexadecimal string, we just need to call following methods over previously created ECDSA struct:
To get the public address string
|
|
To get the private key string
|
|
How single core benchmark is designed?
Since we are going to run benchmarks at runtime, we cannot make use of go benchmarking tools and we need to implement our own benchmarking method. For this purpose, we will design a wrapper function that takes two time snapshots and computes the difference, returning metrics results.
The data we are going to collect is wrapped in BenchResponse struct
|
|
To measure the single core runtime execution time, we will make use of following wrapper function:
|
|
Our example test, will generate 1000 Ethereum addresses sequentially by running:
|
|
Prior executing this test in Android, we run a simple unit test to make sure it works as expected:
|
|
How multi-core benchmark is designed?
In order to allow parallel creations of Ethereum accounts over multiple cores, we will modify previous code and implement both channels and wait groups to create the same amount of accounts.
|
|
Prior executing this test in Android, we run a simple unit test to make sure it works as expected with no data race conditions.
|
|
Running unit test using go test give us an initial result and difference between single core version and concurrent version
|
|
After plotting results, we can view the huge difference that we can get by running the same code in single core or multi-core.
Compiling for Android device
To test our Go code in Android, it is necessary to compile it to a shared object library (*.aar) using Gomobile. In this case, the command that triggers the compilation of our Go code is:
|
|
The result is a pair of files: *aar
library and source code:
|
|
Testing on real Android device!
We created a demo application, include the shared library and its sources and create a dummy trigger from Activity setting view as shown below.
The “Run Benchmarking” button will call native Go code inside of the *.so
file and return our benchmark data result to the user as Dialog popup.
Subscribe, donate or become premium
💬 Share this post in social media
Thanks for checking this out and I hope you found the info useful! If you have any questions, don't hesitate to write me a comment below. And remember that if you like to see more content on, just let me know it and share this post with your colleges, co-workers, FFF, etc.