Skip to content

Slow response when recording and replaying... #12

@johntemyra

Description

@johntemyra

I investigated slow behaviour when I recorded or replayed (pyvisa module) traffic. I cloned the library and asked Chatgpt for help.

It suggested a couple of things like file I/O should be buffered and also to use sys._getframe() instead of inspect.stack(). These two changes made a huge difference for the performance for my tests.

I did this change in recordfilehandler.py

class RecordFileHandler(object):
    def __init__(self, file):
        self.file = file
        self.writeFile = None
        self.lastTruncationPoint = None
        self.recordedSinceTruncationPoint = []
        if self.file:
            self.writeFile = open(self.file, "a", buffering=8192)

    def record(self, text, truncationPoint=False):
        if self.writeFile:
            if truncationPoint:
                self.lastTruncationPoint = os.path.getsize(self.file)
                self.recordedSinceTruncationPoint = []
            if self.lastTruncationPoint is not None:
                self.recordedSinceTruncationPoint.append(text)
            self.writeFile.write(text)
            # Only flush on truncation points or periodically
            if truncationPoint:
                self.writeFile.flush()

    def close(self):
        if self.writeFile:
            self.writeFile.close()

And this in capturepython.py:

        frame = sys._getframe(stackDistance)
        fileName = frame.f_code.co_filename

instead of

        framerecord = inspect.stack()[stackDistance]
        fileName = framerecord[1]

I think the first one (the file I/O) is a bit problematic since it would be best to have instances of it context managed (using with...) but that would affect a whole lot of code.
I don't know what you think would be best to do there?
I've tried adding them separately but both two together is when it really makes a difference for my tests.
As I mentioned, there were a couple of other suggestions besides these two but I haven't investigated any more since they made so much difference.

Br
John

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions