-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
70 lines (55 loc) · 1.72 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
import grpc
import atm_pb2
import atm_pb2_grpc
import pymongo
import random
import json
import time
import logging
import datetime
def run(data):
# 建立與gRPC服務器的通道
channel = grpc.insecure_channel('localhost:50051')
# 建立gRPC客戶端
stub = atm_pb2_grpc.BankServiceStub(channel)
count = 0
start = datetime.datetime.now()
for i in range(0, len(data), 500):
batch = transactions[i:i+500]
requests = []
print(type(requests))
for item in batch:
# print(type(item))
request = atm_pb2.TransferRequest(
from_account_id=item['from_account_id'],
to_account_id=item['to_account_id'],
amount=item['amount']
)
requests.append(request)
futures = []
for request in requests:
future = stub.Transfer.future(request)
futures.append(future)
for future in futures:
response = future.result()
if response.success:
count += 1
else:
print('Transaction failed: ', response.error)
end = datetime.datetime.now()
if (end - start).total_seconds() >= 1:
print('Transactions processed in 1 second: ', count)
break
channel.close()
if __name__ == '__main__':
logging.basicConfig()
# 讀取交易紀錄 JSON 文件
with open("transactions.json", "r") as f:
transactions = json.load(f)
# print(transactions)
# 把交易记录 JSON 文件传到 run function
for json_data in transactions:
print(type(json_data))
run(json_data)
# count+=1
# break