graph/test/test.py

111 lines
3.1 KiB
Python
Raw Normal View History

2024-05-15 20:52:13 +00:00
#!/bin/env python3
import requests
2024-05-19 14:57:15 +00:00
from math import sin, cos
2024-05-15 20:52:13 +00:00
from time import sleep
2024-05-16 23:43:31 +00:00
import numpy as np
from argparse import ArgumentParser
# import pyaudio
2024-05-15 20:52:13 +00:00
def sine(args):
2024-05-15 20:52:13 +00:00
x = 0.0
while True:
try:
requests.get(f"http://{args.address}:{args.port}/add?chart={args.chart}&series={args.series}&x={x}&y={sin(x)}")
x += 0.01
sleep(0.01)
except KeyboardInterrupt:
break
2024-05-16 23:43:31 +00:00
2024-05-19 14:57:15 +00:00
def cosine(args):
x = 0.0
while True:
try:
requests.get(f"http://{args.address}:{args.port}/add?chart={args.chart}&series={args.series}&x={x}&y={cos(x)}")
x += 0.01
sleep(0.01)
except KeyboardInterrupt:
break
def sine_array(args):
2024-05-17 14:24:47 +00:00
start = 0
while True:
try:
2024-05-19 13:31:54 +00:00
x = np.linspace(start, start+0.5, args.num)
y = np.sin(x)
2024-05-17 14:24:47 +00:00
start += 0.5
2024-05-16 23:43:31 +00:00
data = {
"chart": args.chart,
"series": args.series,
"points": [ {"x": i[0], "y":i[1]} for i in zip(x,y) ]
}
2024-05-16 23:43:31 +00:00
r = requests.post(f"http://{args.address}:{args.port}/add", json=data)
2024-05-19 13:31:54 +00:00
sleep(args.delay)
except KeyboardInterrupt:
break
2024-05-16 23:43:31 +00:00
def star(args):
2024-05-16 23:43:31 +00:00
points = [
2024-05-19 13:31:54 +00:00
(2/4,2/2),
(3/4,0),
(0,1/2),
(4/4,1/2),
(1/4,0),
(2/4,2/2)
2024-05-16 23:43:31 +00:00
]
for point in points:
requests.get(f"http://{args.address}:{args.port}/add?chart={args.chart}&series={args.series}&x={point[0]}&y={point[1]}")
2024-05-16 23:43:31 +00:00
# def audio():
# p = pyaudio.PyAudio()
# stream = p.open(
# input=True,
# input_device_index=0,
# format=pyaudio.paInt16,
# channels=1,
# rate=44100,
# frames_per_buffer=1024
# )
# stream.start_stream()
# while True:
# try:
# data = stream.read(1024, False)
# requests.post(
# "http://0.0.0.0:8080/add",
# json={
# "chart": 0,
# "series": "audio",
# "points": [ {"x": i, "y": v} for (i, v) in enumerate(data) ]
# },
# headers={"Connection":"close"})
# except KeyboardInterrupt:
# break
2024-05-16 23:43:31 +00:00
# stream.stop_stream()
# stream.close()
# p.terminate()
2024-05-15 20:52:13 +00:00
if __name__ == "__main__":
parser = ArgumentParser()
2024-05-19 14:57:15 +00:00
parser.add_argument("-t", action="store", dest="test", help="test type", type=str, choices=["sine", "cosine", "sine_array", "star"], required=False, default="sine")
parser.add_argument("-n", action="store", dest="num", help="sin_array size/iteration", type=int, required=False, default=100)
parser.add_argument("-d", action="store", dest="delay", help="delay between iteration", type=float, required=False, default=0.25)
parser.add_argument("-c", action="store", dest="chart", help="chart id", type=int, required=True)
parser.add_argument("-s", action="store", dest="series", help="series name", type=str, required=True)
parser.add_argument("-p", action="store", dest="port", help="server port", type=int, required=False, default=8080)
parser.add_argument("-a", action="store", dest="address", help="server address", type=str, required=False, default="localhost")
args = parser.parse_args()
if args.test == "sine":
sine(args)
2024-05-19 14:57:15 +00:00
if args.test == "cosine":
cosine(args)
elif args.test == "sine_array":
sine_array(args)
elif args.test == "star":
star(args)