|
| 1 | +# Copyright (c) "Neo4j" |
| 2 | +# Neo4j Sweden AB [http://neo4j.com] |
| 3 | +# |
| 4 | +# This file is part of Neo4j. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | + |
| 19 | +import asyncio |
| 20 | +from itertools import product |
| 21 | + |
| 22 | +from pytest import mark |
| 23 | + |
| 24 | +from neo4j import AsyncGraphDatabase |
| 25 | + |
| 26 | +from .tools import RemoteGraphDatabaseServer |
| 27 | + |
| 28 | + |
| 29 | +class AsyncReadWorkload(object): |
| 30 | + |
| 31 | + server = None |
| 32 | + driver = None |
| 33 | + loop = None |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def setup_class(cls): |
| 37 | + cls.server = server = RemoteGraphDatabaseServer() |
| 38 | + server.start() |
| 39 | + cls.loop = asyncio.new_event_loop() |
| 40 | + asyncio.set_event_loop(cls.loop) |
| 41 | + cls.driver = AsyncGraphDatabase.driver(server.server_uri, |
| 42 | + auth=server.auth_token, |
| 43 | + encrypted=server.encrypted) |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def teardown_class(cls): |
| 47 | + try: |
| 48 | + cls.loop.run_until_complete(cls.driver.close()) |
| 49 | + cls.server.stop() |
| 50 | + finally: |
| 51 | + cls.loop.stop() |
| 52 | + asyncio.set_event_loop(None) |
| 53 | + |
| 54 | + def work(self, *units_of_work): |
| 55 | + async def runner(): |
| 56 | + async with self.driver.session() as session: |
| 57 | + for unit_of_work in units_of_work: |
| 58 | + await session.read_transaction(unit_of_work) |
| 59 | + |
| 60 | + def sync_runner(): |
| 61 | + self.loop.run_until_complete(runner()) |
| 62 | + |
| 63 | + return sync_runner |
| 64 | + |
| 65 | + |
| 66 | +class TestAsyncReadWorkload(AsyncReadWorkload): |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def uow(record_count, record_width, value): |
| 70 | + |
| 71 | + async def _(tx): |
| 72 | + s = "UNWIND range(1, $record_count) AS _ RETURN {}".format( |
| 73 | + ", ".join("$x AS x{}".format(i) for i in range(record_width))) |
| 74 | + p = {"record_count": record_count, "x": value} |
| 75 | + async for record in await tx.run(s, p): |
| 76 | + assert all(x == value for x in record.values()) |
| 77 | + |
| 78 | + return _ |
| 79 | + |
| 80 | + @mark.parametrize("record_count,record_width,value", product( |
| 81 | + [1, 1000], # record count |
| 82 | + [1, 10], # record width |
| 83 | + [1, u'hello, world'], # value |
| 84 | + )) |
| 85 | + def test_1x1(self, benchmark, record_count, record_width, value): |
| 86 | + benchmark(self.work(self.uow(record_count, record_width, value))) |
0 commit comments