2019-12-03 10:21:25 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
import logging
|
2019-12-03 10:21:25 +01:00
|
|
|
import warnings
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
from mock import Mock
|
|
|
|
|
|
|
|
from pyperf import perf_counter
|
|
|
|
|
|
|
|
from twisted.internet.defer import Deferred
|
|
|
|
from twisted.internet.protocol import ServerFactory
|
2020-10-29 12:27:37 +01:00
|
|
|
from twisted.logger import LogBeginner, LogPublisher
|
2019-12-03 10:21:25 +01:00
|
|
|
from twisted.protocols.basic import LineOnlyReceiver
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
from synapse.config.logger import _setup_stdlib_logging
|
|
|
|
from synapse.logging import RemoteHandler
|
|
|
|
from synapse.util import Clock
|
2019-12-03 10:21:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LineCounter(LineOnlyReceiver):
|
|
|
|
|
|
|
|
delimiter = b"\n"
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.count = 0
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def lineReceived(self, line):
|
|
|
|
self.count += 1
|
|
|
|
|
|
|
|
if self.count >= self.factory.wait_for and self.factory.on_done:
|
|
|
|
on_done = self.factory.on_done
|
|
|
|
self.factory.on_done = None
|
|
|
|
on_done.callback(True)
|
|
|
|
|
|
|
|
|
|
|
|
async def main(reactor, loops):
|
|
|
|
"""
|
|
|
|
Benchmark how long it takes to send `loops` messages.
|
|
|
|
"""
|
|
|
|
servers = []
|
|
|
|
|
|
|
|
def protocol():
|
|
|
|
p = LineCounter()
|
|
|
|
servers.append(p)
|
|
|
|
return p
|
|
|
|
|
|
|
|
logger_factory = ServerFactory.forProtocol(protocol)
|
|
|
|
logger_factory.wait_for = loops
|
|
|
|
logger_factory.on_done = Deferred()
|
|
|
|
port = reactor.listenTCP(0, logger_factory, interface="127.0.0.1")
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
# A fake homeserver config.
|
|
|
|
class Config:
|
|
|
|
server_name = "synmark-" + str(loops)
|
|
|
|
no_redirect_stdio = True
|
|
|
|
|
|
|
|
hs_config = Config()
|
|
|
|
|
|
|
|
# To be able to sleep.
|
|
|
|
clock = Clock(reactor)
|
2019-12-03 10:21:25 +01:00
|
|
|
|
|
|
|
errors = StringIO()
|
|
|
|
publisher = LogPublisher()
|
|
|
|
mock_sys = Mock()
|
|
|
|
beginner = LogBeginner(
|
|
|
|
publisher, errors, mock_sys, warnings, initialBufferSize=loops
|
|
|
|
)
|
|
|
|
|
|
|
|
log_config = {
|
2020-10-29 12:27:37 +01:00
|
|
|
"version": 1,
|
|
|
|
"loggers": {"synapse": {"level": "DEBUG", "handlers": ["tersejson"]}},
|
|
|
|
"formatters": {"tersejson": {"class": "synapse.logging.TerseJsonFormatter"}},
|
|
|
|
"handlers": {
|
2019-12-03 10:21:25 +01:00
|
|
|
"tersejson": {
|
2020-10-29 12:27:37 +01:00
|
|
|
"class": "synapse.logging.RemoteHandler",
|
2019-12-03 10:21:25 +01:00
|
|
|
"host": "127.0.0.1",
|
|
|
|
"port": port.getHost().port,
|
|
|
|
"maximum_buffer": 100,
|
2020-10-29 12:27:37 +01:00
|
|
|
"_reactor": reactor,
|
2019-12-03 10:21:25 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
logger = logging.getLogger("synapse.logging.test_terse_json")
|
|
|
|
_setup_stdlib_logging(
|
2021-02-16 23:32:34 +01:00
|
|
|
hs_config,
|
|
|
|
log_config,
|
|
|
|
logBeginner=beginner,
|
2019-12-03 10:21:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Wait for it to connect...
|
2020-10-29 12:27:37 +01:00
|
|
|
for handler in logging.getLogger("synapse").handlers:
|
|
|
|
if isinstance(handler, RemoteHandler):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Improperly configured: no RemoteHandler found.")
|
|
|
|
|
|
|
|
await handler._service.whenConnected()
|
2019-12-03 10:21:25 +01:00
|
|
|
|
|
|
|
start = perf_counter()
|
|
|
|
|
|
|
|
# Send a bunch of useful messages
|
|
|
|
for i in range(0, loops):
|
2020-10-29 12:27:37 +01:00
|
|
|
logger.info("test message %s", i)
|
|
|
|
|
|
|
|
if len(handler._buffer) == handler.maximum_buffer:
|
|
|
|
while len(handler._buffer) > handler.maximum_buffer / 2:
|
|
|
|
await clock.sleep(0.01)
|
2019-12-03 10:21:25 +01:00
|
|
|
|
|
|
|
await logger_factory.on_done
|
|
|
|
|
|
|
|
end = perf_counter() - start
|
|
|
|
|
2020-10-29 12:27:37 +01:00
|
|
|
handler.close()
|
2019-12-03 10:21:25 +01:00
|
|
|
port.stopListening()
|
|
|
|
|
|
|
|
return end
|