-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
136 lines (101 loc) · 3.47 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from time import sleep
import requests
import json
class Server:
def __init__(self, url):
self.url = url
pass
def size(self):
pass
def put(self, key, value):
url = f"{self.url}/save/{key}"
payload = {"value": value}
headers = {"Content-Type": "application/json"}
# Make the POST request
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
# Check the response status code
if response.status_code == 200:
print("POST request successful")
print("Response:", response.text)
else:
print("POST request failed with status code:", response.status_code)
def get(self, key):
url = f"{self.url}/retrieve/{key}"
headers = {"Accept": "application/json"}
# Make the GET request
response = requests.get(url, headers=headers, verify=False)
# Check the response status code
if response.status_code == 200:
print("GET request successful")
# print("Response:", response.json())
return response.json()
else:
print("GET request failed with status code:", response.status_code)
def has(self, key: str):
url = f"{self.url}/contains/{key}"
headers = {"Accept": "application/json"}
# Make the GET request
response = requests.get(url, headers=headers, verify=False)
# Check the response status code
return response.status_code == 200
def remove(self, key):
url = f"{self.url}/remove/{key}"
headers = {"Accept": "application/json"}
# Make the GET request
response = requests.post(url, headers=headers, verify=False)
# Check the response status code
return response.status_code == 200
pass
if __name__ == "__main__":
import random
import string
URL = "http://127.0.0.1:57840"
def strGen(N):
return ''.join(random.sample(string.ascii_uppercase + string.digits, N))
sc = Server(URL)
# sc.balance()
# print(sc.clusterSize(), sc.servePoints)
# stream(list(sc.servers.values())) \
# .map(lambda s: list(s.cache.keys())) \
# .for_each(print)
keys = []
while True:
action = input("Enter action(I/C/G/R): ")
if action == "I":
# keys.clear()
for i in range(10):
key = strGen(3)
print("Inserting key : ", key)
sc.put(key, i)
keys.append(key)
pass
elif action == "C":
for key in keys:
print("has key : ", key, sc.has(key))
pass
elif action == "G":
for key in keys:
print("has key : ", key, sc.get(key))
pass
elif action == "R":
for key in keys:
print("has key : ", key, sc.remove(key))
pass
# sleep(1)
# for i in range(500):
# random.shuffle(keys)
# if keys.__len__() <= 0:
# break
# key = keys[0]
# if sc.has(key):
# sc.get(key)
# sc.remove(key)
# if random.random() > 0.3:
# keys.pop(0)
# pass
# else:
# keys.pop(0)
# sleep(1)
# print(sc.clusterSize(), sc.servePoints)
# sc.printClusterDistribution()
# sleep(5)