Skip to content

Commit

Permalink
MP experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
aivarannamaa committed Sep 25, 2020
1 parent 9f35337 commit bf566a3
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 33 deletions.
33 changes: 0 additions & 33 deletions misc/mmm.py

This file was deleted.

49 changes: 49 additions & 0 deletions misc/mp_paste_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import serial
import time

PORT = "/dev/ttyACM0"

# The output of following code tells me whether the code was received intact or not
code = """
print("*" * 1000)
"""

s = serial.Serial(PORT, 115200)

def forward_until(marker):
total = b""
while not total.endswith(marker):
b = s.read(1)
total += b
print(b.decode("UTF-8"), end="")

# prepare
print("Interrupting...")
s.write(b"\x03")
s.write(b"\x03")
print("Cleaning...", s.read_all())
s.write(b"\x02")
s.read_until(b">>> ")
print("Got normal prompt")


start_time = time.time()
for i in range(100):
# goto paste mode
s.write(b"\x05")
forward_until(b"=== ")

data = b"print('*' * 1000)\n"
s.write(data)
s.flush()

# read echo
forward_until(data)

# submit the command
s.write(b"\x04")

# wait until response
forward_until(b">>> ")

print("Processing took %.1f seconds", time.time() - start_time)
88 changes: 88 additions & 0 deletions misc/mp_repl_experiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import serial
import time

PORT = "/dev/ttyACM0"

MODE = "paste" # "paste" or "raw"
BLOCK_SIZE = 256
LENGTH_OF_STRING_LITERAL = 1000 # affects the size of the script sent to the device
NUM_ITERATIONS = 100 # how many times to send the script to the device

# The output of following code tells me whether the code was received intact or not
code = """
s = "%s"
print()
print(len(s), set(s))
""" % ("*" * LENGTH_OF_STRING_LITERAL)

s = serial.Serial(PORT, 115200)

def forward_until(marker):
total = b""
while not total.endswith(marker):
b = s.read(1)
total += b
print(b.decode("UTF-8"), end="")

def prepare():
print("Interrupting...")
s.write(b"\x03")
s.write(b"\x03")
print("Cleaning...", s.read_all())
s.write(b"\x02")
s.read_until(b">>> ")
print("Got normal prompt")


def start_raw_mode():
s.write(b"\x01")
forward_until(b"exit\r\n>")
print("Got raw prompt. Executing code...")


def run_in_raw_mode():
data = code.encode("UTF-8")
while data:
s.write(data[:BLOCK_SIZE])
s.flush()
data = data[BLOCK_SIZE:]
if data:
time.sleep(0.01)

s.write(b"\x04")
forward_until(b"OK")
# wait until completion
forward_until(b">")

def run_in_paste_mode():
# goto paste mode
s.write(b"\x05")
forward_until(b"=== ")

for line in code.splitlines(keepends=True):
data = line.encode("UTF-8")
while data:
block = data[:BLOCK_SIZE]
s.write(block)
s.flush()

forward_until(block)
#time.sleep(0.01)
data = data[BLOCK_SIZE:]

s.write(b"\x04")
forward_until(b">>> ")

prepare()
start_time = time.time()

if MODE == "raw":
start_raw_mode()

for i in range(NUM_ITERATIONS):
if MODE == "raw":
run_in_raw_mode()
else:
run_in_paste_mode()

print("Processing took %.1f seconds" % (time.time() - start_time))

0 comments on commit bf566a3

Please sign in to comment.