File size: 6,098 Bytes
cdfc363
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bef5db
cdfc363
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from concurrent.futures import ThreadPoolExecutor, as_completed
import json
import os
import time

import numpy as np
import requests
import torch

from clip_app_client import ClipAppClient

test_image_url = "https://static.wixstatic.com/media/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg/v1/fill/w_454,h_333,fp_0.50_0.50,q_90/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg"
english_text = (
    "It was the best of times, it was the worst of times, it was the age "
    "of wisdom, it was the age of foolishness, it was the epoch of belief"
)

app_client = ClipAppClient()
preprocessed_image = app_client.preprocess_image(test_image_url)

def _send_text_request(number):
    embeddings = app_client.text_to_embedding(english_text)
    return number, embeddings

def _send_image_url_request(number):
    embeddings = app_client.image_url_to_embedding(test_image_url)
    return number, embeddings

def _send_preprocessed_image_request(number):
    embeddings = app_client.preprocessed_image_to_embedding(preprocessed_image)
    return number, embeddings

def process(numbers, send_func, max_workers=10):
    with ThreadPoolExecutor(max_workers=max_workers) as executor:
        futures = [executor.submit(send_func, number) for number in numbers]
        for future in as_completed(futures):
            n_result, result = future.result()
            print (f"{n_result} : {result.shape}")

if __name__ == "__main__":
    n_calls = 300

    # test text
    numbers = list(range(n_calls))
    start_time = time.monotonic()
    process(numbers, _send_text_request)
    end_time = time.monotonic()
    total_time = end_time - start_time
    avg_time_ms = total_time / n_calls * 1000
    calls_per_sec = n_calls / total_time
    print(f"Text...")
    print(f" Average time taken: {avg_time_ms:.2f} ms")
    print(f" Number of calls per second: {calls_per_sec:.2f}")    

    # test image url
    numbers = list(range(n_calls))
    start_time = time.monotonic()
    process(numbers, _send_image_url_request)
    end_time = time.monotonic()
    total_time = end_time - start_time
    avg_time_ms = total_time / n_calls * 1000
    calls_per_sec = n_calls / total_time
    print(f"Image passing url...")
    print(f" Average time taken: {avg_time_ms:.2f} ms")
    print(f" Number of calls per second: {calls_per_sec:.2f}")    

    # test image as vector
    numbers = list(range(n_calls))
    start_time = time.monotonic()
    process(numbers, _send_preprocessed_image_request)
    end_time = time.monotonic()
    total_time = end_time - start_time
    avg_time_ms = total_time / n_calls * 1000
    calls_per_sec = n_calls / total_time
    print(f"Preprocessed image...")
    print(f" Average time taken: {avg_time_ms:.2f} ms")
    print(f" Number of calls per second: {calls_per_sec:.2f}") 


# from concurrent.futures import ThreadPoolExecutor
# import json
# import os
# import numpy as np
# import requests
# from concurrent.futures import ThreadPoolExecutor, as_completed
# import time

# import torch

# # hack for debugging, set HTTP_ADDRESS to "http://127.0.0.1:8000/"
# # os.environ["HTTP_ADDRESS"] = "http://192.168.7.79:8000"

# test_image_url = "https://static.wixstatic.com/media/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg/v1/fill/w_454,h_333,fp_0.50_0.50,q_90/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg"
# english_text = (
#     "It was the best of times, it was the worst of times, it was the age "
#     "of wisdom, it was the age of foolishness, it was the epoch of belief"
# )

# preprocessed_image = preprocess_image(test_image_url)

# def _send_text_request(number):
#     embeddings = text_to_embedding(english_text)
#     return number, embeddings

# def _send_image_url_request(number):
#     embeddings = image_url_to_embedding(test_image_url)
#     return number, embeddings

# def _send_preprocessed_image_request(number):
#     embeddings = preprocessed_image_to_embedding(preprocessed_image)
#     return number, embeddings

# def process(numbers, send_func, max_workers=10):
#     with ThreadPoolExecutor(max_workers=max_workers) as executor:
#         futures = [executor.submit(send_func, number) for number in numbers]
#         for future in as_completed(futures):
#             n_result, result = future.result()
#             result = json.loads(result)
#             print (f"{n_result} : {len(result[0])}")

# # def process_text(numbers, max_workers=10):
# #     for n in numbers:
# #         n_result, result = send_text_request(n)
# #         result = json.loads(result)
# #         print (f"{n_result} : {len(result[0])}")

# if __name__ == "__main__":
#     n_calls = 300

#     # test text
#     # n_calls = 1
#     numbers = list(range(n_calls))
#     start_time = time.monotonic()
#     process(numbers, _send_text_request)
#     end_time = time.monotonic()
#     total_time = end_time - start_time
#     avg_time_ms = total_time / n_calls * 1000
#     calls_per_sec = n_calls / total_time
#     print(f"Text...")
#     print(f" Average time taken: {avg_time_ms:.2f} ms")
#     print(f" Number of calls per second: {calls_per_sec:.2f}")    

#     # test image url
#     # n_calls = 1
#     numbers = list(range(n_calls))
#     start_time = time.monotonic()
#     process(numbers, _send_image_url_request)
#     end_time = time.monotonic()
#     total_time = end_time - start_time
#     avg_time_ms = total_time / n_calls * 1000
#     calls_per_sec = n_calls / total_time
#     print(f"Image passing url...")
#     print(f" Average time taken: {avg_time_ms:.2f} ms")
#     print(f" Number of calls per second: {calls_per_sec:.2f}")    

#     # test image as vector
#     # n_calls = 1
#     numbers = list(range(n_calls))
#     start_time = time.monotonic()
#     process(numbers, _send_preprocessed_image_request)
#     end_time = time.monotonic()
#     total_time = end_time - start_time
#     avg_time_ms = total_time / n_calls * 1000
#     calls_per_sec = n_calls / total_time
#     print(f"Preprocessed image...")
#     print(f" Average time taken: {avg_time_ms:.2f} ms")
#     print(f" Number of calls per second: {calls_per_sec:.2f}")