<div dir="ltr">On Thu, Apr 11, 2013 at 10:10 AM, Lonni J Friedman <span dir="ltr">&lt;<a href="mailto:netllama@gmail.com" target="_blank">netllama@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Wed, Apr 10, 2013 at 5:15 PM, Bill Campbell &lt;<a href="mailto:linux-sxs@celestial.com">linux-sxs@celestial.com</a>&gt; wrote:<br>

&gt; On Wed, Apr 10, 2013, Lonni J Friedman wrote:<br>
&gt;&gt;I&#39;ve got a perl script that is used to parse data from one format into<br>
&gt;&gt;another format.  It works fairly well 99% of the time, however when<br>
&gt;&gt;the data that its parsing is large, the performance of the script gets<br>
&gt;&gt;awful.  Unfortunately, my perl skills are marginal at best, so I&#39;m<br>
&gt;&gt;lost on how to debug this problem.<br>
&gt;&gt;<br>
&gt;&gt;For example, for 99% of the cases, there are less than 1k rows of data<br>
&gt;&gt;to parse, and it completes in less than 10 seconds.  However, for the<br>
&gt;&gt;remaining 1%, there are over 150k rows, and the script takes hours<br>
&gt;&gt;(3+) to finish.  I&#39;m hoping that this is due to something inefficient<br>
&gt;&gt;in my perl, that can be fixed easily, but I&#39;m not sure what that might<br>
&gt;&gt;be.<br>
&gt;&gt;<br>
&gt;&gt;The slow part of the script is this subroutine:<br>
&gt;&gt;######<br>
&gt;&gt;sub sqlInsert {<br>
&gt;&gt;    my ($fh, $app, $status, $entry, $table_testlist_csv_path,%hash_values) = @_;<br>
&gt;&gt;    my $now=strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, localtime) ;<br>
&gt;&gt;    my $entryVals = join(&#39;,&#39;, map { &quot;\&quot;$$entry{$_}\&quot;&quot;} qw(suiteid<br>
&gt;&gt;regressionCL cl os arch build_type branch gpu subtest osversion));<br>
&gt;&gt;    my $testid = $hash_values{$app} ;<br>
&gt;&gt;<br>
&gt;&gt;    # we need to add an escape character in front of all double quotes<br>
&gt;&gt;in a testname, or the dquotes will be stripped out when the SQL COPY<br>
&gt;&gt;occurs<br>
&gt;&gt;    $app =~ s/&quot;/~&quot;/g ;<br>
&gt;&gt;    print $fh &lt;&lt;END;<br>
&gt;&gt;&quot;$now&quot;,&quot;$app&quot;,&quot;$status&quot;,&quot;$testid&quot;,$entryVals<br>
&gt;&gt;END<br>
&gt;&gt;}<br>
&gt;<br>
&gt; Somebody has already pointed out the use of the strftime/localtime<br>
&gt; for every iteration.  This reminds me of my first programming<br>
&gt; experience in FORTRAN almost 50 years ago where the &quot;I&#39;m an<br>
&gt; Engineer, not a Programmer and Proud of It&quot; person who wrote the<br>
&gt; program computed the square root of PI/2.0 every time in a<br>
&gt; subroutine that was called over 20,000 times per run.  I<br>
&gt; calculated it once, put it in COMMON, and cut the run time from<br>
&gt; 30 minutes to 5 minutes.<br>
&gt;<br>
&gt; There are a few things you might do to improve this.<br>
&gt;<br>
&gt;    + Use one of the database interfaces (DBI) available in Perl to connect<br>
&gt;      to the database.  It&#39;s been quite a while since I did this as I&#39;m<br>
&gt;      primarily doing Python these days so I don&#39;t remember the details.<br>
&gt;      The DBI libraries typically have facilities to properly quote as<br>
&gt;      necessary.<br>
<br>
</div></div>Yea, I&#39;m aware of that, but unfortunately this script has to run on a<br>
large number of environments (some of which are not Linux), and<br>
getting those modules installed is a huge PITA.  Also, the bottleneck<br>
in the script isn&#39;t the database queries, its writing out a file<br>
locally, so making this change wouldn&#39;t help regardless.<br>
<div class="im"><br>
&gt;<br>
&gt;    + I think that most SQL databases have a now() function that will get<br>
&gt;      the current time, and that would probably be much more efficient than<br>
&gt;      doing it externally.  I have a link to the PostgreSQL page on this<br>
&gt;      here.<br>
&gt;                 <a href="http://www.postgresql.org/docs/8.2/static/functions-datetime.html" target="_blank">http://www.postgresql.org/docs/8.2/static/functions-datetime.html</a><br>
<br>
</div>Yup, and we actually have now() as the default for the column in<br>
question.  Unfortunately (or perhaps fortunately), I&#39;m not the<br>
original author of this script, I&#39;ve inherited the mess, and need to<br>
maintain it on top of 3829483 other responsibilities.  At some point I<br>
should determine what the added overhead is to letting the database<br>
figure out now() for the millions of rows we insert each day, rather<br>
than pre-calculating it on the clients.<br>
<div class="im"><br>
&gt;<br>
&gt;    + If the SQL back end has stored procedures, it might be most efficient<br>
&gt;      to have one handle the time automatically on insert.<br>
<br>
</div>That&#39;s what setting a now() default does for a column (at least in Postgresql).<br>
<br>
Now that I&#39;ve determined that the client side timestamp calculation<br>
isn&#39;t the bottleneck, what else can I look at next?<br>
<br>
thanks!<br>
<br>
<br>
--<br>
<div class="im HOEnZb">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
L. Friedman                                    <a href="mailto:netllama@gmail.com">netllama@gmail.com</a><br>
LlamaLand                       <a href="https://netllama.linux-sxs.org" target="_blank">https://netllama.linux-sxs.org</a><br>
</div><div class="HOEnZb"><div class="h5">_______________________________________________<br></div></div></blockquote><div><br></div><div style>Does the script read in an entire data source file and parse each line?  Or does is read one line at a time and parse/write it prior to reading the next line?  If the entire source file is being read into memory, could it be causing a bottleneck?</div>
<div style><br></div><div style>Andrew</div><div> </div></div></div></div>