File: logwriter.py

package info (click to toggle)
python-eliot 1.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: python: 8,641; makefile: 151
file content (49 lines) | stat: -rw-r--r-- 1,047 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
A benchmark for eliot.logwriter.
"""

import tempfile
import time

from twisted.internet.task import react
from twisted.python.filepath import FilePath

from eliot.logwriter import ThreadedFileWriter


LENGTH = 100
MESSAGES = 100000


def main(reactor):
    print("Message size: %d bytes   Num messages: %d" % (LENGTH, MESSAGES))
    message = b"a" * LENGTH
    fp = FilePath(tempfile.mktemp())
    writer = ThreadedFileWriter(fp.open("ab"), reactor)
    writer.startService()

    start = time.time()
    for i in range(MESSAGES):
        writer(message)
    d = writer.stopService()

    def done(_):
        elapsed = time.time() - start
        kbSec = (LENGTH * MESSAGES) / (elapsed * 1024)
        messagesSec = MESSAGES / elapsed
        print("messages/sec: %s   KB/sec: %s" % (messagesSec, kbSec))

    d.addCallback(done)

    def cleanup(result):
        fp.restat()
        print()
        print("File size: ", fp.getsize())
        fp.remove()

    d.addBoth(cleanup)
    return d


if __name__ == "__main__":
    react(main, [])