]> AND Private Git Repository - Cipher_code.git/blob - measure_energy_iot/client_tcp4.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Merge branch 'master' of ssh://info.iut-bm.univ-fcomte.fr/Cipher_code
[Cipher_code.git] / measure_energy_iot / client_tcp4.py
1 import socket
2 import threading
3 import os
4 import time
5 import buffer
6 from PIL import Image
7 import sys
8 from io import BytesIO
9 from pathlib import Path
10 import numpy as np
11
12 HOST = "192.168.0.106"
13 PORT = 2345
14
15 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
16 s.connect((HOST, PORT))
17
18 nb_times=100
19
20 quality=int(sys.argv[1])
21 print("quality ",quality)
22
23
24
25
26 im=Image.open("lena.jpg")
27 im.save("lena.bmp",quality=quality)
28 elapsed_time=0
29 t = time.time()
30 for i in range(nb_times):
31     buf = BytesIO()
32     im.save(buf, "BMP", quality=quality)
33
34 elapsed_time += time.time() - t
35
36 print("compute bmp",elapsed_time/nb_times," / size of file ",Path('lena.bmp').stat().st_size)
37
38
39 """
40 im=Image.open("lena.jpg")
41
42 elapsed_time=0
43 t = time.time()
44 for i in range(nb_times):
45     im.save("lena.bmp",quality=quality)
46
47 elapsed_time += time.time() - t
48
49 print("time bmp",elapsed_time/nb_times)
50 """
51
52
53
54 im=Image.open("lena.jpg")
55 elapsed_time=0
56 im.save("lena2.jpg",quality=quality)
57 t = time.time()
58 for i in range(nb_times):
59     buf = BytesIO()
60     im.save(buf, "JPEG", quality=quality)
61     
62 elapsed_time += time.time() - t
63
64 print("compute noscale jpg",elapsed_time/nb_times, " / size of file ",Path('lena2.jpg').stat().st_size)
65
66
67
68 elapsed_time=0
69 im2=im.resize((256, 256))
70 im2.save("lena3.jpg",quality=quality)
71 t = time.time()
72 for i in range(nb_times):
73     im2=im.resize((256, 256))
74     buf = BytesIO()
75     im2.save(buf, "JPEG", quality=quality)
76     
77 elapsed_time += time.time() - t
78 print("compute scale 4 jpg ",elapsed_time/nb_times, " / size of file ",Path('lena3.jpg').stat().st_size)
79
80
81
82
83
84
85 with s:
86     sbuf = buffer.Buffer(s)
87
88
89     elapsed_time=[]
90     file_name="lena.bmp"
91     for i in range(nb_times):
92         t=time.time()
93         sbuf.put_utf8(file_name)
94
95         file_size = os.path.getsize(file_name)
96         sbuf.put_utf8(str(file_size))
97
98         with open(file_name, 'rb') as f:
99             sbuf.put_bytes(f.read())
100         elapsed_time.append(time.time() - t)
101
102     print("send bmp mean",np.mean(elapsed_time), " std ",np.std(elapsed_time))
103
104     elapsed_time=[]
105     file_name="lena2.jpg"
106     for i in range(nb_times):
107         t=time.time()
108         sbuf.put_utf8(file_name)
109
110         file_size = os.path.getsize(file_name)
111         sbuf.put_utf8(str(file_size))
112
113         with open(file_name, 'rb') as f:
114             sbuf.put_bytes(f.read())
115         elapsed_time.append(time.time() - t)
116
117     print("send noscale jpg   mean",np.mean(elapsed_time), " std ",np.std(elapsed_time))
118
119     elapsed_time=[]
120
121     file_name="lena3.jpg"
122     for i in range(nb_times):
123         t=time.time()
124         sbuf.put_utf8(file_name)
125
126         file_size = os.path.getsize(file_name)
127         sbuf.put_utf8(str(file_size))
128
129         with open(file_name, 'rb') as f:
130             sbuf.put_bytes(f.read())
131         elapsed_time.append(time.time() - t)
132
133     print("send scale 4 jpg  mean",np.mean(elapsed_time), " std ",np.std(elapsed_time))
134