diff --git a/tests/t.generic/test.generic.010.daemon.01.py b/tests/t.generic/test.generic.010.daemon.01.py new file mode 100755 index 00000000..928a50b8 --- /dev/null +++ b/tests/t.generic/test.generic.010.daemon.01.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import time, threading, multiprocessing + +def task(): + process = multiprocessing.current_process() + print(f"Task: {process.daemon=}") + for i in range(100): + print(f"{i=}",flush=True) + time.sleep(0.1) + +process = multiprocessing.Process(target=task, daemon=True) +process.start() + +time.sleep(10) + +print(f"Main Process exit...") + + + diff --git a/tests/t.generic/test.generic.010.daemon.02 copy.py b/tests/t.generic/test.generic.010.daemon.02 copy.py new file mode 100755 index 00000000..15364ce5 --- /dev/null +++ b/tests/t.generic/test.generic.010.daemon.02 copy.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import os,sys +import time + +def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): + """ + Double fork-trick. For starting a posix daemon. + + This forks the current process into a daemon. The stdin, stdout, and stderr + arguments are file names that will be opened and be used to replace the + standard file descriptors in sys.stdin, sys.stdout, and sys.stderr. These + arguments are optional and default to /dev/null. Note that stderr is opened + unbuffered, so if it shares a file with stdout then interleaved output may + not appear in the order that you expect. + + Thanks to: + http://code.activestate.com/recipes/66012-fork-a-daemon-process-on-unix/ + """ + # Do first fork. + try: + pid = os.fork() + if pid > 0: + os.waitpid(pid, 0) + return 0 # Return 0 from first parent. + except OSError as e: + sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) + sys.exit(1) + + # Decouple from parent environment. + os.chdir("/") + os.umask(0) + os.setsid() + + # Do second fork. + try: + pid = os.fork() + if pid > 0: + sys.exit(0) # Exit second parent. + except OSError as e: + sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) + sys.exit(1) + + # Now I am a daemon! + + # Redirect standard file descriptors. + + # NOTE: For debugging, you meight want to take these instead of /dev/null. + # so = open('/tmp/log2', 'ab+') + # se = open('/tmp/log2', 'ab+', 0) + + # si = open(stdin, 'rb') + # so = open(stdout, 'ab+') + # se = open(stderr, 'ab+', 0) + # os.dup2(si.fileno(), sys.stdin.fileno()) + # os.dup2(so.fileno(), sys.stdout.fileno()) + # os.dup2(se.fileno(), sys.stderr.fileno()) + + # Return 1 from daemon. + return 1 + +if daemonize(stdout=sys.stdout): + for i in range(100): + print(f"{i=}",flush=True) + time.sleep(0.1) + +time.sleep(3) + +print(f"Main Process exit...") + + + diff --git a/tests/t.generic/test.generic.011.pickle.01.py b/tests/t.generic/test.generic.011.pickle.01.py new file mode 100755 index 00000000..d2be019a --- /dev/null +++ b/tests/t.generic/test.generic.011.pickle.01.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2021 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import sys, os +import pickle + +sys.path.append(os.path.join(sys.path[0],'../..')) +import TermTk as ttk + +ttk.TTkLog.use_default_file_logging() + +root = ttk.TTk() + +win5 = ttk.TTkWindow(parent=root, pos = (25,10), size=(120,20), title="Test Window 5", border=True) +win5.setLayout(ttk.TTkHBoxLayout()) +ttk.TTkLogViewer(parent=win5) + +def _pickle(): + picklestring = pickle.dumps(root.getCanvas()) + ttk.TTkLog.debug(f"{len(picklestring)=}") + +ttk.TTkButton(parent=root, text="Pickle").clicked.connect(_pickle) + +root.mainloop() \ No newline at end of file diff --git a/tests/t.generic/test.socket.01.client.py b/tests/t.generic/test.socket.01.client.py new file mode 100755 index 00000000..7084d2a2 --- /dev/null +++ b/tests/t.generic/test.socket.01.client.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import socket +import sys + +HOST, PORT = "localhost", 9999 +data = " ".join(sys.argv[1:]) + +# Create a socket (SOCK_STREAM means a TCP socket) +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + # Connect to server and send data + sock.connect((HOST, PORT)) + sock.sendall(bytes(data + "\n", "utf-8")) + + # Receive data from the server and shut down + received = str(sock.recv(1024), "utf-8") + +print("Sent: {}".format(data)) +print("Received: {}".format(received)) diff --git a/tests/t.generic/test.socket.01.server.py b/tests/t.generic/test.socket.01.server.py new file mode 100755 index 00000000..b1292b33 --- /dev/null +++ b/tests/t.generic/test.socket.01.server.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import socketserver + +class MyTCPHandler(socketserver.BaseRequestHandler): + """ + The request handler class for our server. + + It is instantiated once per connection to the server, and must + override the handle() method to implement communication to the + client. + """ + + def handle(self): + # self.request is the TCP socket connected to the client + self.data = self.request.recv(1024).strip() + print("{} wrote:".format(self.client_address[0])) + print(self.data) + # just send back the same data, but upper-cased + self.request.sendall(self.data.upper()) + +if __name__ == "__main__": + HOST, PORT = "localhost", 9999 + + # Create the server, binding to localhost on port 9999 + with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server: + # Activate the server; this will keep running until you + # interrupt the program with Ctrl-C + server.serve_forever() + diff --git a/tests/t.generic/test.socket.02.client.py b/tests/t.generic/test.socket.02.client.py new file mode 100755 index 00000000..48e41f29 --- /dev/null +++ b/tests/t.generic/test.socket.02.client.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import socket +import sys + +ADDR = '/tmp/pyTermTk.skt' +data = " ".join(sys.argv[1:]) + +# Create a socket (SOCK_STREAM means a TCP socket) +with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock: + # Connect to server and send data + sock.connect(ADDR) + sock.sendall(bytes(data + "\n", "utf-8")) + + # Receive data from the server and shut down + received = str(sock.recv(1024), "utf-8") + +print("Sent: {}".format(data)) +print("Received: {}".format(received)) diff --git a/tests/t.generic/test.socket.02.server.py b/tests/t.generic/test.socket.02.server.py new file mode 100755 index 00000000..24351975 --- /dev/null +++ b/tests/t.generic/test.socket.02.server.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import socketserver + +class MyTCPHandler(socketserver.BaseRequestHandler): + """ + The request handler class for our server. + + It is instantiated once per connection to the server, and must + override the handle() method to implement communication to the + client. + """ + + def handle(self): + # self.request is the TCP socket connected to the client + self.data = self.request.recv(1024).strip() + print("{} wrote:".format(self.client_address)) + print(self.data) + # just send back the same data, but upper-cased + self.request.sendall(self.data.upper()) + +if __name__ == "__main__": + ADDR = '/tmp/pyTermTk.skt' + + # Create the server, binding to localhost on port 9999 + with socketserver.UnixStreamServer(ADDR, MyTCPHandler) as server: + # Activate the server; this will keep running until you + # interrupt the program with Ctrl-C + server.serve_forever() + diff --git a/tests/timeit/23.pickle.01.py b/tests/timeit/23.pickle.01.py new file mode 100755 index 00000000..39e9369f --- /dev/null +++ b/tests/timeit/23.pickle.01.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import sys,os +import pickle + +import timeit +import random + + +sys.path.append(os.path.join(sys.path[0],'../..')) +import TermTk as ttk + +# canvas = ttk.TTkCanvas(width=500,height=200) +# cp = ttk.TTkColorDialogPicker(size=(500,200),title="Test Color Picker") +cp = ttk.TTkWidgets.TTkPickers.colorpicker._TTkColorCanvas(size=(500,200)) +canvas = cp.getCanvas() +cp.paintEvent(canvas) +cp.paintChildCanvas() +picklestring = pickle.dumps(canvas) + +def test1(): + return len(pickle.dumps(canvas.serialize())) + +def test2(): + return len(pickle.dumps(canvas)) + +def test3(): + c = pickle.loads(picklestring) + return c._width * c._height + + +loop = 100 + +a = {} + +iii = 1 +while (testName := f'test{iii}') and (testName in globals()): + result = timeit.timeit(f'{testName}(*a)', globals=globals(), number=loop) + # print(f"test{iii}) fps {loop / result :.3f} - s {result / loop:.10f} - {result / loop} {globals()[testName](*a)}") + print(f"test{iii:02}) | {result / loop:.10f} sec. | {loop / result : 15.3f} Fps ╞╡-> {globals()[testName](*a)}") + iii+=1 + diff --git a/tests/timeit/23.pickle.02.py b/tests/timeit/23.pickle.02.py new file mode 100755 index 00000000..f13ee77d --- /dev/null +++ b/tests/timeit/23.pickle.02.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import sys,os,io +import pickle + +import timeit +import random + + +sys.path.append(os.path.join(sys.path[0],'../..')) +import TermTk as ttk + +class MyPickler(pickle.Pickler): + def reducer_override(self, obj): + """Custom reducer for MyClass.""" + if issubclass(type(obj),ttk.TTkColor): + return type, (obj.__class__.__name__, obj.__class__.__bases__, + {'_buffer': obj._buffer, + '_fg':obj._fg, + '_bg':obj._bg, + '_mod':obj._mod, + '_link':obj._link, + '_clean':obj._clean, + '_colorMod':obj._colorMod }) + else: + return NotImplemented + +f = io.BytesIO() +p = MyPickler(f) + +# canvas = ttk.TTkCanvas(width=500,height=200) +# cp = ttk.TTkColorDialogPicker(size=(500,200),title="Test Color Picker") +cp = ttk.TTkWidgets.TTkPickers.colorpicker._TTkColorCanvas(size=(500,200)) +canvas = cp.getCanvas() +cp.paintEvent(canvas) +cp.paintChildCanvas() +picklestring = pickle.dumps(canvas) + +def test1(): + return len(pickle.dumps(canvas)) + +def test2(): + return len(pickle.dumps(canvas.serialize())) + +def test3(): + p.dump(canvas) + return len(f.getvalue()) + +def test4(): + c = pickle.loads(picklestring) + return c._width * c._height + + +loop = 50 + +a = {} + +iii = 1 +while (testName := f'test{iii}') and (testName in globals()): + result = timeit.timeit(f'{testName}(*a)', globals=globals(), number=loop) + # print(f"test{iii}) fps {loop / result :.3f} - s {result / loop:.10f} - {result / loop} {globals()[testName](*a)}") + print(f"test{iii:02}) | {result / loop:.10f} sec. | {loop / result : 15.3f} Fps ╞╡-> {globals()[testName](*a)}") + iii+=1 + diff --git a/tests/timeit/24.socket.01.client.py b/tests/timeit/24.socket.01.client.py new file mode 100755 index 00000000..517d6906 --- /dev/null +++ b/tests/timeit/24.socket.01.client.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import socket +import timeit + +ADDR = '/tmp/pyTermTk.skt' +data = "Eugenio Parodi" + +sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) +sock.connect(ADDR) + +loop = 1000 + +def test1(): + sock.sendall(bytes(data + "\n", "utf-8")) + received = str(sock.recv(1024), "utf-8") + return len(received) +https://github.com/ceccopierangiolieugenio/pyTermTk/issues/210 + +a = {} + +iii = 1 +while (testName := f'test{iii}') and (testName in globals()): + result = timeit.timeit(f'{testName}(*a)', globals=globals(), number=loop) + # print(f"test{iii}) fps {loop / result :.3f} - s {result / loop:.10f} - {result / loop} {globals()[testName](*a)}") + print(f"test{iii:02}) | {result / loop:.10f} sec. | {loop / result : 15.3f} Fps ╞╡-> {globals()[testName](*a)}") + iii+=1 + +sock.sendall(bytes('', "utf-8")) diff --git a/tests/timeit/24.socket.01.server.py b/tests/timeit/24.socket.01.server.py new file mode 100755 index 00000000..117c2a69 --- /dev/null +++ b/tests/timeit/24.socket.01.server.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import socketserver + +class MyTCPHandler(socketserver.BaseRequestHandler): + """ + The request handler class for our server. + + It is instantiated once per connection to the server, and must + override the handle() method to implement communication to the + client. + """ + + def handle(self): + while True: + # self.request is the TCP socket connected to the client + self.data = self.request.recv(1024).strip() + if not self.data: + return + print(len(self.data)) + # just send back the same data, but upper-cased + self.request.sendall(self.data.upper()) + +if __name__ == "__main__": + ADDR = '/tmp/pyTermTk.skt' + + # Create the server, binding to localhost on port 9999 + with socketserver.UnixStreamServer(ADDR, MyTCPHandler) as server: + # Activate the server; this will keep running until you + # interrupt the program with Ctrl-C + server.serve_forever() + diff --git a/tests/timeit/24.socket.02.client.py b/tests/timeit/24.socket.02.client.py new file mode 100755 index 00000000..90c5fc0f --- /dev/null +++ b/tests/timeit/24.socket.02.client.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import socket +import timeit + +ADDR = '/tmp/pyTermTk.skt' +data = "Eugenio Parodi" + +sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) +sock.connect(ADDR) + +loop = 1000 + +def test1(): + sock.sendall(bytes(data + "\n", "utf-8")) + received = str(sock.recv(1024), "utf-8") + return len(received) + + +a = {} + +iii = 1 +while (testName := f'test{iii}') and (testName in globals()): + result = timeit.timeit(f'{testName}(*a)', globals=globals(), number=loop) + # print(f"test{iii}) fps {loop / result :.3f} - s {result / loop:.10f} - {result / loop} {globals()[testName](*a)}") + print(f"test{iii:02}) | {result / loop:.10f} sec. | {loop / result : 15.3f} Fps ╞╡-> {globals()[testName](*a)}") + iii+=1 + +sock.sendall(bytes('', "utf-8")) diff --git a/tests/timeit/25.comparison.01.py b/tests/timeit/25.comparison.01.py new file mode 100755 index 00000000..945d6454 --- /dev/null +++ b/tests/timeit/25.comparison.01.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2023 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import timeit, pickle + +class ObjA(): + __slots__ = ('a','b','c','d') + def __init__(self,a,b,c,d) -> None: + self.a=a + self.b=b + self.c=c + self.d=d + +class ObjA1(ObjA): + def __eq__(self, other): + if other is None: return False + return ( + self.a == other.a and + self.b == other.b and + self.c == other.c and + self.d == other.d ) + +class ObjA2(ObjA): + def __eq__(self, other): + if other is None: return False + return ( + (self.a, self.b, self.c, self.d ) == + (other.a,other.b,other.c,other.d) ) + +class ObjA3(ObjA): + __slots__ = ('rec') + def __init__(self, a, b, c, d) -> None: + super().__init__(a, b, c, d) + self.rec = (self.a, self.b, self.c, self.d ) + def __eq__(self, other): + if other is None: return False + return self.rec == other.rec + +oa1_1 = ObjA1(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa1_2 = ObjA1(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa1_3 = ObjA1(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa1_4 = ObjA1(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa2_1 = ObjA2(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa2_2 = ObjA2(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa2_3 = ObjA2(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa2_4 = ObjA2(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa3_1 = ObjA3(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa3_2 = ObjA3(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa3_3 = ObjA3(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') +oa3_4 = ObjA3(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'123456789123456789123456789123456789') + +oa1_1d = ObjA1(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567891') +oa1_2d = ObjA1(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567892') +oa1_3d = ObjA1(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567893') +oa1_4d = ObjA1(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567894') +oa2_1d = ObjA2(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567895') +oa2_2d = ObjA2(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567896') +oa2_3d = ObjA2(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567897') +oa2_4d = ObjA2(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567898') +oa3_1d = ObjA3(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567895') +oa3_2d = ObjA3(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567896') +oa3_3d = ObjA3(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567897') +oa3_4d = ObjA3(1,'asd123456789abcdefghijklmnopqrstuvwxyz',3.123456,'1234567891234567891234567891234567898') + +print(f"{len(pickle.dumps(oa1_1))=}") +print(f"{len(pickle.dumps(oa2_1))=}") +print(f"{len(pickle.dumps(oa3_1))=}") +print(f"diff: {len(pickle.dumps(oa3_1))-len(pickle.dumps(oa1_1))}") + +def test1(): return oa1_1==oa1_1==oa1_1==oa1_1 +def test2(): return oa1_1==oa1_2==oa1_3==oa1_4 +def test3(): return oa2_1==oa2_1==oa2_1==oa2_1 +def test4(): return oa2_1==oa2_2==oa2_3==oa2_4 +def test5(): return oa3_1==oa3_1==oa3_1==oa3_1 +def test6(): return oa3_1==oa3_2==oa3_3==oa3_4 + +def test7(): return oa1_1==oa1_1==oa1_1==oa1_1d +def test8(): return oa1_1==oa1_2==oa1_3==oa1_4d +def test9(): return oa2_1==oa2_1==oa2_1==oa2_1d +def test10(): return oa2_1==oa2_2==oa2_3==oa2_4d +def test11(): return oa3_1==oa3_1==oa3_1==oa3_1d +def test12(): return oa3_1==oa3_2==oa3_3==oa3_4d + +def test13(): return oa1_1d==oa1_1==oa1_1==oa1_1 +def test14(): return oa1_1d==oa1_2==oa1_3==oa1_4 +def test15(): return oa2_1d==oa2_1==oa2_1==oa2_1 +def test16(): return oa2_1d==oa2_2==oa2_3==oa2_4 +def test17(): return oa3_1d==oa3_1==oa3_1==oa3_1 +def test18(): return oa3_1d==oa3_2==oa3_3==oa3_4 + + +loop = 300000 + +a = {} + +iii = 1 +while (testName := f'test{iii}') and (testName in globals()): + result = timeit.timeit(f'{testName}(*a)', globals=globals(), number=loop) + # print(f"test{iii}) fps {loop / result :.3f} - s {result / loop:.10f} - {result / loop} {globals()[testName](*a)}") + print(f"test{iii:02}) | {result / loop:.10f} sec. | {loop / result : 15.3f} Fps ╞╡-> {globals()[testName](*a)}") + iii+=1