[[Glasgow Notes]]
## adding an endpoint argument to the applet
```python
def add_interact_arguments(cls, parser):
ServerEndpoint.add_argument(parser, "endpoint")
```
## getting data from the endpoint to use inside the applet
(example adapted from `video-ws1812-output` applet)
call the endpoint inside applet interact function, define a number for `buffer_size`:
```python
buffer_size = 16384
async def interact(self, device, args)`
endpoint = await ServerEndpoint("socket", self.logger, args.endpoint, queue_size=buffer_size)
while True:
try:
data = await asyncio.shield(endpoint.recv(buffer_size))
await iface.write(data)
except asyncio.CancelledError:
pass
```
see [[Using Glasgow FIFO]] for how to get the data into Glasgow next
## starting applet from the command line
```
glasgow run video-ws2812-output --pins-out 1,2,3 -c 100 4 -V 3.3 tcp:localhost:123
```
the ENDPOINT argument is in the format tcp:HOST:PORT, or something else for a unix server.
## testing communication over TCP
```zsh
nc localhost 1234
```
([[netcat]], how does it work?)
now you can send data to the applet, and the logs show it as received. Neat!
![[CleanShot 2023-08-28 at
[email protected]]]
![[CleanShot 2023-08-28 at
[email protected]]]
## Benchmarking the socket
```
import sys
import asyncio
import logging
import time
logger = logging.getLogger(__name__)
sys.path.append("/Users/isabelburgos/Scan-Gen-Glasgow-Testing/software")
from glasgow.support.endpoint import *
async def get_endpoint():
endpoint = await ServerEndpoint("socket", logger, ("tcp","localhost","1234"), queue_size=16384*8)
data = "FFFF"*4096
bytes_data = data.encode("UTF-8")
start_time = time.perf_counter()
await endpoint.send(bytes_data)
end_time = time.perf_counter()
print(end_time-start_time)
asyncio.run(get_endpoint())
```
Result - 3x10-6 seconds on average
(16384 Bytes/(3 * 10^(-6)s))/(10^6 B/MB) = 5400 MB/s???
## trying to do this bidirectionally
```
## socketbenchmark.py
import sys
import asyncio
import logging
import time
logger = logging.getLogger(__name__)
from test_socket import get_recieve_socket, r_benchmark
sys.path.append("/Users/isabelburgos/Scan-Gen-Glasgow-Testing/software")
from glasgow.support.endpoint import *
data = "FFFF"*4096
bytes_data = data.encode("UTF-8")
async def get_endpoint():
endpoint = await ServerEndpoint("socket", logger, ("tcp","localhost","1234"), queue_size=16384*8)
return endpoint
async def s_benchmark(endpoint):
for n in range(256):
start_time = time.perf_counter()
await endpoint.send(bytes_data)
end_time = time.perf_counter()
print(end_time-start_time)
async def run():
endpoint = await get_endpoint()
reader, writer = await get_recieve_socket()
await asyncio.gather(
s_benchmark(endpoint),
r_benchmark(reader, writer)
)
asyncio.run(run())
```
```
## test_socket.py
import socket
import asyncio
async def get_recieve_socket():
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 1234 # Port to listen on (non-privileged ports are > 1023)
reader, writer = await asyncio.open_connection(HOST, PORT)
return reader, writer
async def r_benchmark(reader,writer):
while True:
try:
data = await reader.read(16384)
#print(data)
#print("read", len(data))
except:
break
```
Result: Prints times averaging 1.5e-5 seconds.
-> ~1000 MB/s
## Running with my glasgow applet
In interact:
```
async def interact(self, device, args, scan_iface):
print("start interact")
await scan_iface.set_frame_resolution(2048,2048)
await scan_iface.set_raster_mode()
endpoint = await ServerEndpoint("socket", None, ("tcp","localhost","1234"), queue_size=16384*8)
for n in range(256):
print(n)
start_time = time.perf_counter()
data = await scan_iface.iface.read(16384)
await endpoint.send(data)
end_time = time.perf_counter()
print(end_time-start_time)
```
Timing iface.read() and endpoint.send() separately:
![[CleanShot 2023-12-03 at
[email protected]]]
## Reading more data
In the benchmark applet, the size of the read is 8388608 bytes
With this size,
![[CleanShot 2023-12-03 at
[email protected]]]
```math
data = 8388608 B
time = 0.5 s
speed = (data/time)
```
In benchmark mode source:
Time: 0.1885221004486084 s
Data: 8388608 B
```math
data = 8388608 B
time = 0.1885 s
speed = (data/time)
```
Tried adding iface.reset() in my interact(), since that's what the benchmark applet does, but it didn't change my times
Put everything into run() instead of interact, did the throughput calculation in the code, and still getting:
15.75556742720986 MB/s
![[CleanShot 2023-12-03 at
[email protected]]]
Adding throttle = "none" as an argument to this part of my applet.... did not change the throughput
```math
bytes_per_dwell = 12
dwell_time = 250ns
bytes_per_dwell/dwell_time
```