Re: Question regarding custom contextFactory for twisted.web.client.Agent

Glyph <[email protected]> Fri, 12 May 2017 16:24:33 -0700
Newsgroups gmane.comp.python.twisted.web
Message-ID <[email protected]>
--===============6354211800745097348==
Content-Type: multipart/alternative;
 boundary="Apple-Mail=_7763DB70-153E-4213-BCE5-CB671F8BF8FC"


--Apple-Mail=_7763DB70-153E-4213-BCE5-CB671F8BF8FC
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii


> On May 12, 2017, at 1:56 PM, Jason Litzinger <[email protected]> =
wrote:
>=20
> Hello,
>=20
> I have an optimization question in the realm of "before I do something
> foolish..." regarding the contextFactory passed to =
twisted.web.client.Agent.
>=20
> Background:
>=20
> I have (several) twisted applications running on a Cortex-A5 system, =
one of
> which interfaces with a web service using the twisted.web.client.Agent
> API*.  The
> problem I've observed is that requests to this web service result in a =
higher
> than desired CPU load.  To understand the source of the load I =
isolated the
> server interactions and profiled.
>=20
> Investigation:
>=20
> The profile data revealed the load in question stemmed from TLS =
related
> calls, specifically optionsForClientTLS, which is called once per =
connection.
> In looking (briefly) at what optionsForClientTLS does, it seemed
> (perhaps wrongly)
> that there was an opportunity for optimization in my specific case.
>=20
> The optimization is to create a custom contextFactory that caches the =
connection
> creator since the host is not changing.  I prototyped an =
implementation and the
> load was reduced by a factor of ten.
>=20
> Question:
>=20
> Is it safe/sane to reuse a connection creator if the host to which I'm
> connecting is
> not going to change?

This sounds eminently sensible, and in fact sounds like Twisted, or Treq =
at least, really ought to do something like this on our end with some =
kind of small LRU cache, keeping the most-used N hostnames (where N =
defaults to some small number, say, 20).  I'm surprised to hear it is =
such a big optimization, but surprises like that are entirely the point =
of performance testing!

Nevertheless, thanks for asking!  I really wish people would ask =
questions about client TLS more often before doing something potentially =
insecure :).  In this case, the client connection creator is used to =
produce client connections anyway, and the connections are not shared, =
so there should be no worrisome mingling of state between connections.

We do host a performance test suite here: http://speed.twistedmatrix.com =
<http://speed.twistedmatrix.com/> and the benchmarks are here =
https://github.com/twisted-infra/twisted-benchmarks =
<https://github.com/twisted-infra/twisted-benchmarks> if your test is =
not adequately represented in an existing benchmark.

-g


--Apple-Mail=_7763DB70-153E-4213-BCE5-CB671F8BF8FC
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" =
class=3D""><br class=3D""><div><blockquote type=3D"cite" class=3D""><div =
class=3D"">On May 12, 2017, at 1:56 PM, Jason Litzinger &lt;<a =
href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div class=3D""><div =
class=3D"">Hello,<br class=3D""><br class=3D"">I have an optimization =
question in the realm of "before I do something<br class=3D"">foolish..." =
regarding the contextFactory passed to twisted.web.client.Agent.<br =
class=3D""><br class=3D"">Background:<br class=3D""><br class=3D"">I =
have (several) twisted applications running on a Cortex-A5 system, one =
of<br class=3D"">which interfaces with a web service using the =
twisted.web.client.Agent<br class=3D"">API*. &nbsp;The<br =
class=3D"">problem I've observed is that requests to this web service =
result in a higher<br class=3D"">than desired CPU load. &nbsp;To =
understand the source of the load I isolated the<br class=3D"">server =
interactions and profiled.<br class=3D""><br class=3D"">Investigation:<br =
class=3D""><br class=3D"">The profile data revealed the load in question =
stemmed from TLS related<br class=3D"">calls, specifically =
optionsForClientTLS, which is called once per connection.<br class=3D"">In=
 looking (briefly) at what optionsForClientTLS does, it seemed<br =
class=3D"">(perhaps wrongly)<br class=3D"">that there was an opportunity =
for optimization in my specific case.<br class=3D""><br class=3D"">The =
optimization is to create a custom contextFactory that caches the =
connection<br class=3D"">creator since the host is not changing. &nbsp;I =
prototyped an implementation and the<br class=3D"">load was reduced by a =
factor of ten.<br class=3D""><br class=3D"">Question:<br class=3D""><br =
class=3D"">Is it safe/sane to reuse a connection creator if the host to =
which I'm<br class=3D"">connecting is<br class=3D"">not going to =
change?</div></div></blockquote><br class=3D""></div><div>This sounds =
eminently sensible, and in fact sounds like Twisted, or Treq at least, =
really ought to do something like this on our end with some kind of =
small LRU cache, keeping the most-used N hostnames (where N defaults to =
some small number, say, 20). &nbsp;I'm surprised to hear it is such a =
big optimization, but surprises like that are entirely the point of =
performance testing!</div><div><br class=3D""></div><div>Nevertheless, =
thanks for asking! &nbsp;I really wish people would ask questions about =
client TLS more often before doing something potentially insecure :). =
&nbsp;In this case, the client connection <i =
class=3D"">creator</i>&nbsp;is used to produce client connections =
anyway, and the connections are not shared, so there should be no =
worrisome mingling of state between connections.</div><div><br =
class=3D""></div><div>We do host a performance test suite here: <a =
href=3D"http://speed.twistedmatrix.com" =
class=3D"">http://speed.twistedmatrix.com</a>&nbsp;and the benchmarks =
are here&nbsp;<a =
href=3D"https://github.com/twisted-infra/twisted-benchmarks" =
class=3D"">https://github.com/twisted-infra/twisted-benchmarks</a>&nbsp;if=
 your test is not adequately represented in an existing =
benchmark.</div><div><br class=3D""></div><div>-g</div><br =
class=3D""></body></html>=

--Apple-Mail=_7763DB70-153E-4213-BCE5-CB671F8BF8FC--


--===============6354211800745097348==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KVHdpc3RlZC13
ZWIgbWFpbGluZyBsaXN0ClR3aXN0ZWQtd2ViQHR3aXN0ZWRtYXRyaXguY29tCmh0dHA6Ly90d2lz
dGVkbWF0cml4LmNvbS9jZ2ktYmluL21haWxtYW4vbGlzdGluZm8vdHdpc3RlZC13ZWIK

--===============6354211800745097348==--