<%@ language = "JavaScript" %>
<% /*
     $Revision$
     $Id$

    File:               perfmon.asp
    Description:        Test Oracle, Webserver and client performance and connection speeds.
    Parameters:         doSaveTime als 1 dan gaan we opslaan
                        showDetail als 1 tonen we ook de details
    Context:            Handmatig opgestart, vanuit PerformanceTester.asp of self-submit
    Note:
*/ %>

<!-- #include file="../../Shared/common.inc" -->

<%
// WEB_PERMON gaat automatisch, WEB_PRSSYS zou handmatig kunnen aanroepen
user.checkAutorisation("WEB_PERMON", true) || user.checkAutorisation("WEB_PRSSYS");

var oldLog = Session("logging");
var Autosave = getQParamInt("autosave", 0) == 1;
if (Autosave)
{
  if (oldLog&1)
  {
    __DoLog("Starten performance meting (logging tijdelijk uit)", "#808000");
    __Logging = 0; // Anders worden we gek
  }
}

niFrame = 100; // zoveel iFRAME testen

var mustSave = getQParamInt("doSaveTime", 0) == 1;
var showDetail = getQParamInt("showDetail", 0) == 1;
if (mustSave)
{ // Helemaal klaar.
  var oldLog = getQParamInt("oldLog", 0);
  __Log("==>KLaar met perfmon. Waarden worden opgeslagen.");
  var serverStart = getQParamInt("serverStart");
  var serverEnd = (new Date()).valueOf();
      bodyTxt = "Dit duurde " + (serverEnd - serverStart) + "ms "
              + " en moeten we misschien opslaan in de database"
  var sql = "INSERT INTO fac_perfmon_sample("
          + " fac_perfmon_sample_datum,"
          + " fac_perfmon_sample_waarde1, "
          + " fac_perfmon_sample_waarde2, "
          + " fac_perfmon_sample_waarde3, "
          + " fac_perfmon_sample_waarde4, "
          + " fac_perfmon_sample_waarde5, "
          + " fac_perfmon_sample_waarde6, "
          + " fac_perfmon_sample_waarde7, "
          + " fac_perfmon_sample_waarde8, "
          + " fac_perfmon_sample_waarde9, "
          + " prs_perslid_key, fac_perfmon_sample_ip, "
          + " fac_perfmon_sample_opmerking)"
          + " VALUES(SYSDATE,"
          + getQParamInt("t1") + ","
          + getQParamInt("t2") + ","
          + getQParamInt("t3") + ","
          + getQParamInt("t4") + ","
          + getQParamInt("t5") + ","
          + getQParamInt("t6") + ","
          + getQParamInt("t7") + ","
          + getQParamInt("t8") + ","
          + (serverEnd - serverStart) + ","
          +           Session("user_key") + "," + safe.quoted_sql(String(Request.ServerVariables("REMOTE_ADDR"))) + ","
          + "'perfmon.asp;$Revision$')"
  Oracle.Execute(sql);
%>
  <emp>Waarden zijn opgeslagen</emp>
  <script>
    if (parent && parent.perfmon_done)
      parent.perfmon_done();
  </script>
<%
  Response.End;
}

var serverStart = (new Date()).valueOf();
var t1; // A Database
var t2; // B Database response 1
var t3; // B Database speed 2
var t4; // C Webserver speed
var t5; // D Webserver->client speed
var t6; // D Webserver->client response
var t7; // E Client speed
var t8; // Hele pad

function toMillisec(sTime)
{
    return(Math.floor(sTime * 1000)); // Geeft dus een integer terug
}
%>
<html>
  <head>
    <script>
      var iframeStart=-1;
      var onloadCnt=0;
      var mode=-1; // zie StartIframeTiming
      function StartListboxTiming()
      {
          var lbStart = new Date();
          var lbEnd;
          var cnt = 0;
          var sel = document.getElementById("frmSelect");
          do
          {
              sel.options[sel.options.length] = new Option("Nummer "+cnt);
              cnt ++;
              lbEnd = new Date();
          } while ((lbEnd - lbStart) < 250)
          t7 = (1000*(lbEnd - lbStart) / cnt).toFixed(0);
          document.getElementById("cntListbox").innerHTML = ""+t7+"ms";
          document.getElementById("cntListboxTot").innerHTML = ""+t7;
      }
      // mode==1: gebruik perfmon.gif (25K, cache-baar) en ga na afloop door met mode==2
      // mode==2: gebruik photo.asp en na afloop ben je klaar
      function StartIframeTiming(pMode)
      {
          mode=pMode;
          iframeStart = new Date();
          onloadCnt = 0;
          for (i=0; i < <%=niFrame%>; i++)
          if (mode==1)
              document.getElementById("imgfr"+i).src = "../../Pictures/perfmon.gif";
          else
           //   document.getElementById("ifr"+i).src = "../../res/photo.asp?room=-"+Math.floor(Math.random()*10000)+"&dt=" + (new Date()).valueOf();
              document.getElementById("ifr"+i).src = "../../res/get_res_info_ajax.asp?perfmontest=1&req_info=res_ruimte&res_ruimte_key=-"+Math.floor(Math.random()*10000)+"&dt=" + (new Date()).valueOf();
      }
      function SetPicture(vimage,vremark) // Photo.asp callback
      {
      }
      function IframeLoaded()
      {
          if (mode>0)
          {
              onloadCnt++;
              if (onloadCnt==<%=niFrame%>)
              {
                  var iframeEnd = new Date();
                  if (mode==1)
                  {
                      t6 = (10*(iframeEnd - iframeStart) / <%=niFrame%>).toFixed(0);
                      document.getElementById("cntFrames").innerHTML = ""+t6+"ms";
                      document.getElementById("cntFramesTot").innerHTML = ""+t6;
                      StartIframeTiming(2); // Ga door met empty.asp
                  }
                  else  // We zijn echt helemaal klaar met alles.
                  {
                      t8 = (10*(iframeEnd - iframeStart) / <%=niFrame%>).toFixed(0);
                      document.getElementById("cntFrames2").innerHTML = ""+t8+"ms";
                      document.getElementById("cntFramesTot2").innerHTML = ""+t8;

                      document.getElementById("txtMeting").innerHTML = "Meting is klaar.";
                      <%
                      if (Autosave)
                          Response.Write("doSubmit();");
                      %>
                  }
              }
          }
      }
      function bodyOnload()
      {
          StartListboxTiming(); // Deze eerst zodat hij de andere, a-synchrone niet beinvloedt
          StartIframeTiming(1);
          document.getElementById("cntSpeedTot").innerHTML = ""+t5; // Nu is het form er pas
      }
    </script>
    <style>
      *, body {font-family: Arial; font-size: 9pt;}
      table {width:800px;}
      geenhekjeaanbeginregel,#perfresult {border:2px solid blue;}
      geenhekjeaanbeginregel,#perfresult th {  border-bottom: 1px solid blue; padding: 1px 4px 3px 3px; text-align: left; }
      geenhekjeaanbeginregel,#perfresult td {  border: 1px solid red; padding: 4px; width: 60px; text-align: center;}
      geenhekjeaanbeginregel,#perfresult td.info {  border: 0px; padding: 4px; text-align: left; width: 120px;}
    </style>
  </head>
  <body onload="bodyOnload()">
    <div id=txtMeting>Measuring....</div>
    <div <%=showDetail? "" : "style=display:none"%>>
<%    // Hoe vaak kan ik <sql> uitvoeren in <maxtime> milliseconde seconde?
      function CountSQL(descr, sql, maxtime)
      {
        var cnt = 0;
        var sDate = new Date();
        do
        {
            var oRs = Oracle.RealConnection.Execute(sql); // RealExecution voorkomt overhead van logging
            oRs.Close();
            cnt ++;
            var endDate = new Date();
        } while ((endDate - sDate) < maxtime)

        t2 = toMillisec(maxtime/cnt);
        Response.Write("<tr><td>"+ descr + "</td><td align=right>" + t2 +"ms</td>");
        return cnt;
      }

      var startDate = new Date();
%>
<%    /* =========================================================================================== */ %>
      <table>
      <tr><td><b>Facilitor environment performance indication</b><br><%= startDate%></td></tr>
      <tr><td><img alt="<%=L("lcl_WEB_PERMON")%>" src=../../Pictures/perfmon.png></td></tr>
      </table>
<%    /* =========================================================================================== */ %>
      <table>
      <tr>
        <td colspan=3><b>A) Database activitijd 1 (waarde1)</b>
          <br>Hier wordt getest hoe snel de databaseserver records aan een tabel kan toevoegen.
        </td>
      </tr>
<%    Oracle.Execute("DELETE FROM fac_rapport WHERE fac_rapport_node = " + safe.quoted_sql(Session.SessionID))
      sql = "DECLARE "
          + "   tstart     INTEGER; "
          + "   teind      INTEGER; "
          + "   cnt        INTEGER; "
          + "BEGIN "
          + "   cnt := 0; "
          + "   SELECT dbms_utility.get_time() " // Niet iedereen heeft rechten op v$timer
          + "     INTO tstart "
          + "     FROM dual; "
          + "   teind := tstart; "
          + "   WHILE (teind - tstart < 25) " // 25/100 seconde lang testen
          + "   LOOP "
          + "      INSERT INTO fac_rapport "
          + "                  (fac_rapport_node, fac_rapport_volgnr, "
          + "                   fac_rapport_regel, "
          + "                   fac_rapport_soort "
          + "                  ) "
          + "           VALUES ("+safe.quoted_sql(Session.SessionID)+", cnt, "
          + "                      'Performance test' "
          + "                   || TO_CHAR (SYSDATE, 'YYYY-MM-DD HH24:MI'), "
          + "                   2 "
          + "                  ); "
          + "      cnt := cnt + 1; "
          + "      SELECT dbms_utility.get_time() "
          + "        INTO teind "
          + "        FROM dual; "
          + "      COMMIT;" // Zodat we zo kunnen gaan tellen
          + "   END LOOP; "
          + "END; "

      oRs = Oracle.Execute(sql);
      oRs = Oracle.Execute("SELECT COUNT(*) FROM fac_rapport WHERE fac_rapport_node = " + safe.quoted_sql(Session.SessionID));
      t1 = toMillisec(250 / oRs(0).value);
      oRs.Close();
      Response.Write("<tr><td>1000 records toevoegen</td><td align=right>" + t1 + "ms</td></tr>");
%>
      </table>
<%    /* =========================================================================================== */ %>
      <table>
      <tr>
        <td colspan=3><b>B) Database responsetijd (waarde2)</b>
          <br>Hier wordt getest hoe snel vanuit de webserver een verbinding met
          <br>de database kan worden opgezet.
        </td>
      </tr>
<%    CountSQL("1000 verbindingen", "SELECT SYSDATE FROM DUAL", 250)
%>
      </table>
<%    /* =========================================================================================== */ %>
      <table>
      <tr>
        <td colspan=3><b>B) Database activitijd 2 (waarde3)</b>
          <br>Hier wordt getest hoe snel de webserver records uit de database kan halen.
        </td>
      </tr>
<%
      oRs = Oracle.Execute("SELECT * FROM FAC_RAPPORT WHERE fac_rapport_node = " + safe.quoted_sql(Session.SessionID));
      var sDate = new Date();
      var endDate = new Date();
      var cnt =0;
      while (endDate - sDate < 250 && !oRs.eof)
      {
        var endDate = new Date();
        oRs.MoveNext();
        cnt ++;
      }
      oRs.Close();
      t3 = toMillisec(10 * (endDate - sDate) / cnt);
      Response.Write("<tr><td>10000 records ophalen:</td><td align=right>" + t3 + "ms</td></tr>");
      Oracle.Execute("DELETE FROM FAC_RAPPORT WHERE fac_rapport_node = " + safe.quoted_sql(Session.SessionID)) // Opruimen
%>
      </table>
<%    /* =========================================================================================== */ %>
      <table>
      <tr>
        <td colspan=3><b>C) Webserver snelheid (waarde4)</b>
          <br>Hier wordt getest hoe snel de webserver strings kan opbouwen.
        </td>
      </tr>
<%
      var sDate = new Date();
      var endDate = new Date();
      var cnt =0;
      while (endDate - sDate < 250)
      {
        var endDate = new Date();
        var s="";
        while (s.length < 1000)
            s += "X";
        cnt ++;
      }
      t4=toMillisec((endDate - sDate) / cnt / 4);
      Response.Write("<tr><td>250 grote strings opbouwen:</td><td align=right>" + t4 + "ms</td></tr>");
%>
      </table>
<%    /* =========================================================================================== */ %>
      <table>
      <tr>
        <td colspan=3><b>D) Netwerksnelheid (waarde5)</b>
          <br>Hier wordt getest hoe snel data over het (inter/intra)netwerk wordt getransporteerd (testen zonder Fiddler!).
        </td>
      </tr>
      <tr><td>Overhalen 1MB kilobyte aan (verborgen) data:</td><td><div id=cntSpeed>Bezig..</div></td></tr>
      <script>var sDate = new Date();</script>
<%    var bigdata = "<div style='display:none'>"
      while (bigdata.length < 1024) bigdata+=String(Math.random()*10);
      bigdata+="</div>"
      for (i=0; i<=1024; i++)
      {
        Response.Write(bigdata);
      }
%>    <script>
        var eDate = new Date();
        t5 = (eDate - sDate);
        document.getElementById("cntSpeed").innerHTML = ""+t5+"ms (~"+(Math.round(1024*80/t5)/10)+"Mbit/s)";
        // die is er nog niet document.getElementById("cntSpeedTot").innerHTML = ""+t5;
      </script>
      </table>
<%    /* =========================================================================================== */ %>
<%    for (i = 0; i < niFrame; i++)
      { %>
        <IFRAME src="../../shared/empty.html" id=ifr<%=i%> onload="IframeLoaded()" style="display:none"></IFRAME>
        <IMG id=imgfr<%=i%> onload="IframeLoaded()" style="display:none">
<%    } %>
      <table>
      <tr><td colspan=3><b>D) Webserver responsetijd (waarde6)</b><br>Hier wordt getest hoe snel vanuit de client een verbinding met de webserver kan worden opgezet.</td></tr>
      <tr><td>10 cachebare pagina's ophalen:</td><td><div id=cntFrames></div></td></tr>
      </table>
<%    /* =========================================================================================== */ %>
      <FORM><SELECT id=frmSelect style='display:none'></SELECT></FORM>
      <table>
      <tr><td colspan=3><b>E) Client snelheid (waarde7)</b><br>Hier wordt getest hoe snel de client een listbox kan vullen.</td></tr>
      <tr><td>1000 items aan listbox toevoegen:</td><td><div id=cntListbox></div></td></tr>
      </table>
<%    /* =========================================================================================== */ %>
      <table>
      <tr><td colspan=3><b>A-E) Gehele pad (waarde8)</b><br>Hier wordt vanuit de client aan de database wat gevraagd.</td></tr>
      <tr><td>10 niet cachebare pagina's ophalen incl. database:</td><td><div id=cntFrames2></div></td></tr>
      </table>
<%    /* =========================================================================================== */ %>
    </div>

    <b>Systeeminformatie:</b>
    <table>
    <tr><td>Database:</td><td><%=Oracle.RealConnection.Properties("User Name")%></td></tr>
    <tr><td>DBMS Name:</td><td><%=Oracle.RealConnection.Properties("DBMS Name")%></td></tr>
    <tr><td>DBMS Version:</td><td><%=Oracle.RealConnection.Properties("DBMS Version")%></td></tr>
    <tr><td>ADO Version:</td><td><%=Oracle.RealConnection.Version%></td></tr>
    <tr><td>Provider Version:</td><td><%=Oracle.RealConnection.Properties("Provider Version")%></td></tr>
    <tr><td>Provider Name:</td><td><%=Oracle.RealConnection.Properties("Provider Name")%></td></tr>
    <tr><td>OLE DB Version:</td><td><%=Oracle.RealConnection.Properties("OLE DB Version")%></td></tr>
    <tr><td>Web server:</td><td><%=Request.ServerVariables("SERVER_SOFTWARE")%></td></tr>
    <tr><td>Client browser:</td><td><%=Request.ServerVariables("HTTP_USER_AGENT")%></td></tr>
    </table>
    <b>Meetresultaten:</b>
    <table id="perfresult">
    <tr>
      <th>date</th>
      <th>id</th>
      <th>IP</th>
      <th>t1</th>
      <th>t2</th>
      <th>t3</th>
      <th>t4</th>
      <th>t5</th>
      <th>t6</th>
      <th>t7</th>
      <th>t8</th>
    </tr>
    <tr>
      <td class="info"><%=toDateTimeString(new Date(serverStart), 1)%></td>
      <td class="info"><%=Session("user_key")%></td>
      <td class="info"><%=Request.ServerVariables("REMOTE_ADDR")%></td>
      <td><%=t1%></td>
      <td><%=t2%></td>
      <td><%=t3%></td>
      <td><%=t4%></td>
      <td id=cntSpeedTot>...</td>
      <td id=cntFramesTot>...</td>
      <td id=cntListboxTot>...</td>
      <td id=cntFramesTot2>...</td>
    </tr>
    </table>
    <table>
<%  Response.Write("<tr><td colspan=3><small>Facilitor 5i perfmon $Revision$</small></td></tr>") %>
    </table>
    <script>
      function doSubmit()
      {
        frm = document.getElementById("Performform");
        frm.t5.value = t5;
        frm.t6.value = t6;
        frm.t7.value = t7;
        frm.t8.value = t8;
        frm.submit();
      }
    </script>
    <form name="Performform" id="Performform">
      <input type=hidden name=doSaveTime value=1>
      <input type=hidden name=serverStart value=<%=serverStart%>>
      <input type=hidden name=t1 value=<%=t1%>>
      <input type=hidden name=t2 value=<%=t2%>>
      <input type=hidden name=t3 value=<%=t3%>>
      <input type=hidden name=t4 value=<%=t4%>>
      <input type=hidden name=t5>
      <input type=hidden name=t6>
      <input type=hidden name=t7>
      <input type=hidden name=t8>
      <input type=hidden name=oldLog value=<%=oldLog%>>
      <xxinput type=button onClick="doSubmit()" value="Nu opslaan">
    </form>
  </body>
</html>
<% ASPPAGE_END(); %>
