VM Performance: Calling javascript functions from Applet

Discussion in 'Javascript' started by SPG, Dec 24, 2004.

  1. SPG

    SPG Guest

    Hi,

    We have an applet that has to support the SUN VMs as well as the MS VM.
    The applet receives updates from a server (via tcp or http) and wraps them
    up as objects and passes them using the JSObject scripting context to a
    javascript function.

    This function takes the object and reads the properties and updates a
    screen.

    All seems simple stuff. The problem is that the MS JVM runs like a rocket,
    but the sun vm seems to max out our processor and also takes ages to process
    anything.

    I have tried logging from the console using debug level 5, and it shows a
    lot of back and forth between the applet and the javascript when accessing
    methods on the object passed.

    Seeing that as a possible problem, I now pass a delimited string to the
    front end, and then convert that into an update message purely in javascript
    so there is only one hit to the applet per message.

    I am still seeing a massive difference between the sun and ms VMs.

    Has anyone ever come across this, and is there anything I can do to help
    flatten out this performance?

    Steve
    SPG, Dec 24, 2004
    #1
    1. Advertising

  2. SPG

    SPG Guest

    Hi,

    OK, Below is a quick sample.. A bit nasty code-wise but sort of highlights
    what we are doing in a very rough way.
    I have two message pumps to simulate multiple threads updating prices...

    We find the MS VM to be far more performant than the SUN version...
    It would be really great if we could at least find out why the sun vm is so
    poor at handling this, so we can start rectifying our mistakes!

    Steve

    <snip code="applet code">
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import netscape.javascript.*;
    import java.util.*;

    public class LoadTest
    extends Applet {

    private String jsFunction = "handleUpdate";
    private JSObject win;
    MessagePump pump1;
    MessagePump pump2;

    //Construct the applet
    public LoadTest() {
    }

    //Initialize the applet
    public void init() {
    try {
    win = JSObject.getWindow(this);
    pump1 = new MessagePump((int)(Math.random()*500)+300);
    pump1.start();
    pump2 = new MessagePump((int)(Math.random()*500)+300);
    pump2.start();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    public void destroy(){
    pump1.interrupt();
    pump2.interrupt();
    super.destroy();

    }

    private void sendUpdate(final Message m){
    try{
    final Object[] params = new Object[]{m};
    win.call(jsFunction, params);
    }catch(Exception err){}
    }

    private class MessagePump extends Thread{
    private long _wait;
    public MessagePump(long wait){
    _wait = wait;
    }
    public void run(){
    while(! interrupted()){
    for(int i=0; i < 20; i++){
    Message m = new Message("" + (i+1), "" +
    (Math.random()*100),"" + (Math.random()*100),new Date());
    sendUpdate(m);
    }
    try {
    Thread.sleep(_wait);
    }
    catch (InterruptedException ex) {
    }
    }
    }
    }

    }
    </snip>
    <html>
    <head>
    <title>
    HTML Test Page
    </title>
    <SCRIPT language="JavaScript">
    function handleUpdate(msg)
    {
    try{
    var id = msg.getID();
    var date = msg.getDate();
    var bid = msg.getBid();
    var ask = msg.getAsk();

    var table = document.getElementById("Prices");
    var row = table.rows[id];

    row.cells[0].firstChild.nodeValue = id;
    row.cells[1].firstChild.nodeValue = date;
    row.cells[2].firstChild.nodeValue = bid;
    row.cells[3].firstChild.nodeValue = ask;
    }catch(e)
    {
    alert(e);
    }

    }
    </SCRIPT>
    </head>
    <body>
    <h1>Using Direct Access</h1>
    <table border=1 id="Prices">
    <thead id="Title">
    <tr>
    <th>ID</th>
    <th>Date</th>
    <th>Bid</th>
    <th>Ask</th>
    </tr>
    </thead>
    <tr id="1">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="2">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="3">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="4">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="5">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="6">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="7">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="8">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="9">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="10">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="11">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="12">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="13">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="14">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="15">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="16">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="17">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="18">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="19">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    <tr id="20">
    <td id="ID">&nbsp;</td>
    <td id="DATE">&nbsp;</td>
    <td id="BID">&nbsp;</td>
    <td id="ASK">&nbsp;</td>
    </tr>
    </table>
    <applet
    codebase = "."
    code = "LoadTest.class"
    name = "TestApplet"
    width = "0"
    height = "0"
    hspace = "0"
    vspace = "0"
    align = "middle"
    MAYSCRIPT
    >

    </applet>
    </body>
    </html>


    <snip code="html">


    </snip>
    SPG, Dec 27, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur
    Replies:
    6
    Views:
    1,051
    Alex Vinokur
    Aug 30, 2003
  2. crazytazo
    Replies:
    3
    Views:
    1,253
    Daniel Pitts
    Oct 7, 2008
  3. vamsi
    Replies:
    21
    Views:
    2,073
    Keith Thompson
    Mar 9, 2009
  4. SPG
    Replies:
    2
    Views:
    102
  5. SPG
    Replies:
    0
    Views:
    72
Loading...

Share This Page