Problem 2 Option 1
Experience with DHT with kademlia library in Python 3.
A distributed hash table (DHT) is a decentralized storage system that provides lookup and storage schemes similar to a hash table, storing key-value pairs. Each node in a DHT is responsible for keys along with the mapped values.
-
Tested on Mac OS only, this can be run on Linux and Window system with some modification.
-
Python 3install HERE- Use
python --versionto make sure your python is version 3.0+
- Use
-
Kademlia: A distributed hash table designed for decentralized peer-to-peer computer networks. -
Asyncio: Facilitates keeping the program running asynchronously. -
Aioconsole: Enables asynchronous input handling. -
Pillow: Library for image encoding and decoding. -
Lorem-text: Library for generating testing strings.
A set of images in folder to_send are picked with various sizes and types:
- 1_24kb.jpeg
- 2_360kb.png
- 3_2mb.jpg
- 4_10mb.png
- 5_15mb.png
- 6_18mb.png
- 7_19mb.png
A set of random lorem ipsum string are generated.
- Duration is not included:
- the time to encode, decode the image.
- the time to generate strings.
- the time to save csv, or save image.
-
Data is manipulated into
report.xlsxfor the purpose of graphing, from:.img_read.csv: Image information such as image name, image size, image chunk from the network, and read/download duration are reported to this file.img_write.csv: Image name, image size, number of chunks, average chunk size, number of bytes totally, and upload duration are reported to this file.str_read.csv: key, length of value, and read duration are reported to this file.str_write.csv: key, length of value, and set duration are reported to this file.
At the end of each table in report.xlsx, there are average duration.
Recordsfolder have reports used to make this report. Containse1,e2,e3folders for collected experience data.Reportfolder uses to save.csvfiles after recording duration.downloadfolder uses to save download images from the networks.
- Go to
Hashydirectory, use the following command to install all dependencies:
pip install -r requirements.txtIn Hashy directory, use the following command:
python server.py <PORT>- Where
<PORT>is the unused PORT number that you want your server running on.
Example: python server.py 5678
In the Hashy directory, use the following command:
python peer.py <NETWORK_PORT> <PORT>-
Pick action
- Type
sto set value to the network - Type
gto get value from the network
Set [s], Get [g], Clear Screen[c] or Quit [q]:
- Type
-
Pick type of value to set
- Type
kvif you want to set key and value pair to the network - Type
imgif you want to upload an image to the network
Enter type <key,value> [kv], image [img], defined img [ai] lorem mode[l]:
If it's image, please enter full path to the image.
For example:
'/Users/USER_NAME/Hashy/to_send/7_19mb.png'Enter the file path [.jpg|.jpeg|.png]:
- Type
aito send a set of defined images in folderto_send- For testing purpose, Images in
to_sendmust be kept ORIGINAL
- For testing purpose, Images in
- Type
lto send 200 random lorem ipsum in various size- For testing purpose, key and value will not be displayed at run time
- Use
gto get desired key:value- For example, the key can be
lorem_0,lorem_5,...lorem_1000
- For example, the key can be
- Type
-
Pick type of value to get
- Type
kto get a value of a key. - Type
imgto download the image with image name without extension - Type
aito download the defined images that WAS UPLOADED to the networks- Download to the
downloadfolder inHashydirectory
- Download to the
- Type
lto get 200 key:values that was SET to the netoworks- For the testing purpose,
lmode will not show you the result.
- For the testing purpose,
Enter type key [k], image [img], defined img [ai], lorem mode [l]: - Type
Where
-
<NETWORK_PORT>is the port of the server, or other running nodes in the network. -
<PORT>is the unused PORT number on your machine that you want your node running on.
Example: To connect to the initial network running on port 5678 and I have port 6000 free/unused on my machine.
- Run:
python peer.py 5678 6000
- Start a Node at port
5678 - Start other Nodes at port
5679,5680,5681,5682and connect them to the network (port5678) - Set a given Images to the network on port
5679and set a random Lorem Text on port5681to the network. - On port
5682, get a set of Images, and on port5680, get a set of Lorem Text from the network.
Create the network following Graph 1 above.
Upload images and lorem ipsum strings.
- Graph 1: Write and Read Images Duration
- Graph 2: Duration of set and get 200 lorem ipsum, each string increase by 5 words
Purpose of experience 1 is to explore how the DHT network works by setting up a central node, and let other peers connect to the central node.
-
In Graph 1, Write duration is significantly slower than read duration (Images).
- Write an image to DHT system is about
221 msslower than read an image.
- Write an image to DHT system is about
-
In Graph 2, Write duration is also significantly slower than read duration (Strings).
- Write a string to DHT system is about
1.44 msslower than read a string.
- Write a string to DHT system is about
- Start a Node at port
5678 - Start Node
5679and connect to5678 - Start Node
5680and connect to5679 - Start Node
5681and connect to5680 - Start Node
5682and Connect to5681 - Start Node
5683and Connect to5681 - Set Images and Lorem from Node
5683to the network - Get Images and Lorem at Node
5682
- Graph 1: Write and Read Images Duration:
- Graph 2: Duration of set and get 200 lorem ipsum, each string increase by 5 words
Purpose of experience 2 is to measure the performance of write and read if a node is far away from inital node.
-
In Graph 1, Write duration is significantly slower than read duration (Images).
- Write an image to DHT system is about
243 msslower than read an image.
- Write an image to DHT system is about
-
In Graph 2, Write duration is also significantly slower than read duration (Strings).
- Write a string to DHT system is about
1.62 msslower than read a string.
- Write a string to DHT system is about
- Start Node
5678 - Start Node
5679and connect to5678 - Start Node
5680and connect to5679 - Start Node
5681and connect to5680 - Set Images and Lorem at Node
5681 - Start Node
6001and connect to Node5679 - Start Node
6000and connect to Node6001 - Get Images and Lorem at Node
60003 times to measure - Disconnect Node
5679from the Network - Get Images and Lorem at Node
6000
Write before 6000 and 6001 connect:
Write 3x time
- Graph 1: Images Write Duration at Node 5681 vs. Average Read Duration at Node 6000 after connect vs. Read Duration at Node 6000 after disconnect Node 6001 from the network:
- Graph 2: String Write Duration at Node 5681 vs. Average Read Duration at Node 6000 after connect vs. Read Duration at Node 6000 after disconnect Node 6001 from the network:
The purpose of experience 3 is to measure performance of get data from DHT system if a node joined the network after the data is set. To see if the new node takes more time to discover the data in the existing network. Also, we want to see if a neighbor peer left the network, does it affect the ability to discover the network of its peers.
As we can observe from graph 1, the data has been set, after Node 6000 joins the network:
- It take Node
6000slower than Experience 1 and 2 to read, Average of746 ms. - After 6001 left the network, the node 6000 lost a peer, it takes 6000 about
40 msto read from the network.
As we can observe from graph 2:
- It takes Node
6000about average of0.23 msto read strings from the network after joining. - After 6001 left the network, it takes Node
6000about0.30 msto read strings from the networks.
Each experience above has its own discussion.
Compare 3 experiences:
| Read/Write | Experience 1 | Experience 2 |
|---|---|---|
| Avg Write Img | 223.0 ms | 245.5 ms |
| Avg Read Img | 2.11 ms | 2.04 ms |
| Avg Write Str | 1.47 ms | 1.65 ms |
| Avg Read Str | 0.03 ms | 0.25 ms |
- Experience 3:
- Images:
- Average write duration:
147.37 ms - Average read duration after join:
746.4 ms - Average read duration after disconnect peer:
40.2 ms
- Average write duration:
- Strings:
- Average write duration:
1.03 ms - Average read duration after join:
0.23 ms - Average read duration after disconnect peer:
0.30 ms
- Average write duration:
- Images:
From the data collected above:
- The more nodes in the network, the further the set/get node from the initial node, it may take more time to write to the network. However, the performance is not significantly different for writing/setting to the network.
- If the node joins after the data is set, it may take the node more time to discover and get data from the existing network.
- Experience 1 and 2: it takes about
2 msto read/get images. - However, in experience 3, after node 6000 joins the network, it takes
node 6000up to750 msto explore and get the images. - Why does experience 3 take less time to write data (images/strings) to the network?
- It could be because there are fewer nodes than in experiences 1 and 2 when it writes the data in step 5.
- Experience 1 and 2: it takes about
- If the node joins lost a peer it connected to, it also takes more time to read data from the network.
- The average time to read images (experience 3) is
40.2 ms, slower than both experiences 1 and 2 (2 ms). - The average time to read strings in experience 3 is
0.30 ms, also slower than experience 1 (0.03 ms), but not significantly compared to experience 2 (0.25 ms).
- The average time to read images (experience 3) is
Some observations from experiences 1, 2, 3:
- The heavier the images, the longer it takes to write and read from the networks.
- Longer strings do not significantly take longer to write to the networks.
Why do I break images into chunks of bytes?
- For each key:value, the maximum length of
valueis 8000. Therefore, the only way to save an image in the DHT system is to save many chunks of bytes. - Each image can have hundreds of chunks, each chunk is full of 8000 bytes. While each regular string doesn't reach the maximum of 8000 bytes. Therefore, getting an image of 100 chunks takes significantly more time and is slower than getting 100 key:value pairs.
How do I break them into chunks?
For any image, lower the quality to 85% of the original quality.
Then, it will be broken into chunks of 8000 bytes.
To save in the DHT networks:
1. Set IMAGE_NAME:NUMBER_OF_CHUNKS
2. Set all the chunks with the key IMAGE_NAME_i where i is the index of the chunk.
- The DHT system is fast in the 3 experiences above, and it can have many applications. However, consider encrypting data before sending it to the system to securely store data since everyone in the network can access your data by having the key.













