cross-post: https://stackoverflow.com/q/44257530/46395

Consider app.psgi:

#!perl use 5.024; use strictures; use Time::HiRes qw(sleep); sub mock_connect { my $how_long_it_takes = 3 + rand; sleep $how_long_it_takes; return $how_long_it_takes; } sub main { state $db_handle = mock_connect($dsn); return sub { [200, [], ["connect took $db_handle seconds\n"]] }; } my $dsn = 'dbi:blahblah'; # from config file my $app = main($dsn);
Measuring plackup (HTTP::Server::PSGI: Accepting connections at http://0:5000/):
› perl -MBenchmark=timeit,timestr,:hireswallclock -E"say timestr timei +t 10, sub { system q(curl http://localhost:5000) }" connect took 3.0299610154043 seconds connect took 3.0299610154043 seconds connect took 3.0299610154043 seconds connect took 3.0299610154043 seconds connect took 3.0299610154043 seconds connect took 3.0299610154043 seconds connect took 3.0299610154043 seconds connect took 3.0299610154043 seconds connect took 3.0299610154043 seconds connect took 3.0299610154043 seconds 2.93921 wallclock secs ( 0.03 usr + 0.06 sys = 0.09 CPU) @ 107.53/s +(n=10)
Measuring thrall (Starting Thrall/0.0305 (MSWin32) http server listening at port 5000):
› perl -MBenchmark=timeit,timestr,:hireswallclock -E"say timestr timei +t 10, sub { system q(curl http://localhost:5000) }" connect took 3.77111188120125 seconds connect took 3.15455510265111 seconds connect took 3.77111188120125 seconds connect took 3.15455510265111 seconds connect took 3.77111188120125 seconds connect took 3.64333342488772 seconds connect took 3.15455510265111 seconds connect took 3.77111188120125 seconds connect took 3.85268922343767 seconds connect took 3.64333342488772 seconds 17.4764 wallclock secs ( 0.02 usr + 0.09 sys = 0.11 CPU) @ 90.91/s ( +n=10)
This performance is not acceptable because the initialisation happens several times, despite the state variable. How do you make it so it happens only once?

In reply to How do I make a PSGI program do costly initialisation only once per process, not per thread? by daxim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.