tests/test-batching.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 09 Apr 2024 22:37:15 +0200
changeset 51595 3a6fae3bef35
parent 48946 642e31cb55f0
permissions -rw-r--r--
outgoing: add a simple fastpath when there is no common This further speed up case like `hg bundle --all` for larger repository. ### data-env-vars.name = mozilla-try-2023-03-22-zstd-sparse-revlog # benchmark.name = hg.command.bundle # bin-env-vars.hg.flavor = default # bin-env-vars.hg.py-re2-module = default # benchmark.variants.revs = all # benchmark.variants.type = none-streamv2 before: 316.749699 after: 311.165461 (-1.76%, -5.58) There is further work to be done in this area like not doing any outgoing computation in the stream case for example. however the recent changes already gives use a large win for a small amount of local work. ### benchmark.name = hg.command.bundle # bin-env-vars.hg.flavor = default # bin-env-vars.hg.py-re2-module = default # benchmark.variants.revs = all # benchmark.variants.type = none-streamv2 ## data-env-vars.name = mercurial-public-2024-03-22-zstd-sparse-revlog pre-%ln-change: 1.263859 the-%ln-change: 0.700229 (-44.60%, -0.56) prev-changeset: 0.496050 (-60.75%, -0.77) this-changeset: 0.495243 (-60.81%, -0.77) ## data-env-vars.name = tryton-public-2024-03-22-zstd-sparse-revlog pre-%ln-change: 2.975765 the-%ln-change: 1.870798 (-37.13%, -1.10) prev-changeset: 1.461583 (-50.88%, -1.51) this-changeset: 1.469185 (-50.63%, -1.51) ## data-env-vars.name = pypy-2024-03-22-zstd-sparse-revlog pre-%ln-change: 4.540080 the-%ln-change: 3.401700 (-25.07%, -1.14) prev-changeset: 2.915810 (-35.78%, -1.62) this-changeset: 2.911643 (-35.87%, -1.63) ## data-env-vars.name = heptapod-public-2024-03-25-zstd-sparse-revlog pre-%ln-change: 10.138396 the-%ln-change: 7.750458 (-23.55%, -2.39) prev-changeset: 6.665565 (-34.25%, -3.47) this-changeset: 6.672078 (-34.19%, -3.47) ## data-env-vars.name = mozilla-try-2023-03-22-zstd-sparse-revlog pre-%ln-change: 399.484481 the-%ln-change: 346.508952 (-13.26%, -52.98) prev-changeset: 316.749699 (-20.71%, -82.73) this-changeset: 311.165461 (-22.11%, -88.32)

# test-batching.py - tests for transparent command batching
#
# Copyright 2011 Peter Arrenbrecht <peter@arrenbrecht.ch>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import contextlib

from mercurial import (
    localrepo,
    pycompat,
    wireprotov1peer,
)


def bprint(*bs):
    print(*[pycompat.sysstr(b) for b in bs])


# equivalent of repo.repository
class thing:
    def hello(self):
        return b"Ready."


# equivalent of localrepo.localrepository
class localthing(thing):
    def foo(self, one, two=None):
        if one:
            return b"%s and %s" % (
                one,
                two,
            )
        return b"Nope"

    def bar(self, b, a):
        return b"%s und %s" % (
            b,
            a,
        )

    def greet(self, name=None):
        return b"Hello, %s" % name

    @contextlib.contextmanager
    def commandexecutor(self):
        e = localrepo.localcommandexecutor(self)
        try:
            yield e
        finally:
            e.close()


# usage of "thing" interface
def use(it):

    # Direct call to base method shared between client and server.
    bprint(it.hello())

    # Direct calls to proxied methods. They cause individual roundtrips.
    bprint(it.foo(b"Un", two=b"Deux"))
    bprint(it.bar(b"Eins", b"Zwei"))

    # Batched call to a couple of proxied methods.

    with it.commandexecutor() as e:
        ffoo = e.callcommand(b'foo', {b'one': b'One', b'two': b'Two'})
        fbar = e.callcommand(b'bar', {b'b': b'Eins', b'a': b'Zwei'})
        fbar2 = e.callcommand(b'bar', {b'b': b'Uno', b'a': b'Due'})

    bprint(ffoo.result())
    bprint(fbar.result())
    bprint(fbar2.result())


# local usage
mylocal = localthing()
print()
bprint(b"== Local")
use(mylocal)

# demo remoting; mimicks what wireproto and HTTP/SSH do

# shared


def escapearg(plain):
    return (
        plain.replace(b':', b'::')
        .replace(b',', b':,')
        .replace(b';', b':;')
        .replace(b'=', b':=')
    )


def unescapearg(escaped):
    return (
        escaped.replace(b':=', b'=')
        .replace(b':;', b';')
        .replace(b':,', b',')
        .replace(b'::', b':')
    )


# server side

# equivalent of wireproto's global functions
class server:
    def __init__(self, local):
        self.local = local

    def _call(self, name, args):
        args = dict(arg.split(b'=', 1) for arg in args)
        return getattr(self, name)(**args)

    def perform(self, req):
        bprint(b"REQ:", req)
        name, args = req.split(b'?', 1)
        args = args.split(b'&')
        vals = dict(arg.split(b'=', 1) for arg in args)
        res = getattr(self, pycompat.sysstr(name))(**pycompat.strkwargs(vals))
        bprint(b"  ->", res)
        return res

    def batch(self, cmds):
        res = []
        for pair in cmds.split(b';'):
            name, args = pair.split(b':', 1)
            vals = {}
            for a in args.split(b','):
                if a:
                    n, v = a.split(b'=')
                    vals[n] = unescapearg(v)
            res.append(
                escapearg(
                    getattr(self, pycompat.sysstr(name))(
                        **pycompat.strkwargs(vals)
                    )
                )
            )
        return b';'.join(res)

    def foo(self, one, two):
        return mangle(self.local.foo(unmangle(one), unmangle(two)))

    def bar(self, b, a):
        return mangle(self.local.bar(unmangle(b), unmangle(a)))

    def greet(self, name):
        return mangle(self.local.greet(unmangle(name)))


myserver = server(mylocal)

# local side

# equivalent of wireproto.encode/decodelist, that is, type-specific marshalling
# here we just transform the strings a bit to check we're properly en-/decoding
def mangle(s):
    return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s))


def unmangle(s):
    return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))


# equivalent of wireproto.wirerepository and something like http's wire format
class remotething(thing):
    def __init__(self, server):
        self.server = server

    def _submitone(self, name, args):
        req = name + b'?' + b'&'.join([b'%s=%s' % (n, v) for n, v in args])
        return self.server.perform(req)

    def _submitbatch(self, cmds):
        req = []
        for name, args in cmds:
            args = b','.join(n + b'=' + escapearg(v) for n, v in args)
            req.append(name + b':' + args)
        req = b';'.join(req)
        res = self._submitone(
            b'batch',
            [
                (
                    b'cmds',
                    req,
                )
            ],
        )
        for r in res.split(b';'):
            yield r

    @contextlib.contextmanager
    def commandexecutor(self):
        e = wireprotov1peer.peerexecutor(self)
        try:
            yield e
        finally:
            e.close()

    @wireprotov1peer.batchable
    def foo(self, one, two=None):
        encoded_args = [
            (
                b'one',
                mangle(one),
            ),
            (
                b'two',
                mangle(two),
            ),
        ]
        return encoded_args, unmangle

    @wireprotov1peer.batchable
    def bar(self, b, a):
        return [
            (
                b'b',
                mangle(b),
            ),
            (
                b'a',
                mangle(a),
            ),
        ], unmangle

    # greet is coded directly. It therefore does not support batching. If it
    # does appear in a batch, the batch is split around greet, and the call to
    # greet is done in its own roundtrip.
    def greet(self, name=None):
        return unmangle(
            self._submitone(
                b'greet',
                [
                    (
                        b'name',
                        mangle(name),
                    )
                ],
            )
        )


# demo remote usage

myproxy = remotething(myserver)
print()
bprint(b"== Remote")
use(myproxy)