Author Archives: Scott Cytacki

Safari Javascript bug

Recently we found that our Lab framework caused the JavascriptCore of Safari 5.1 to crash. Safari 5.1 is the latest version available for OS X 10.6. If you have OS X 10.7 or 10.8, then you have Safari 6 which doesn’t have this problem.

Too long; didn’t read solution: do not name your getters the same as your closure variables.

After at lot of detective work I figured out what the problem was. A block of code like this was causing it:

function testObject(){
  var myProperty = "hello";
  return {
    get myProperty() {
      return myProperty;
    }
  }
}
var myObject = testObject();
console.log(myObject.myProperty);

Here is a fiddle of the same code. If you run that fiddle in Safari 5.1 You will see an reference error to myProperty. Note: you need to make sure script debugging is disabled. If script debugging is enabled then this problem doesn’t occur. That block of code is using a Javascript getter. If you are not familiar with Javascript getters and setters, this post by John Resig is a great place to start.

If the closure variable name is changed to _myProperty like this:

function testObject(){
  var _myProperty = "hello";
  return {
    get myProperty() {
      return _myProperty;
    }
  }
}
var myObject = testObject();
console.log(myObject.myProperty);

Then it works! So there is an issue with Safari when a getter has the same name as the variable in the outer closure. This didn’t make sense though. We use this pattern of a getter accessing a variable of the same name throughout much of our code. Those parts of the code worked fine. After more exploration I found the following block of code works fine in Safari 5.1:

function testObject(){
  var myProperty = "hello";
  return {
    get myProperty() {
      return myProperty;
    },
    methodUsingMyProperty: function () {
      return myProperty;
    }
  };
}
var myObject = testObject();
console.log(myObject.myProperty);
Here is a fiddle of the same code.

This code is using the closure variable in another method that is part of the return object. Based on this, I believe the problem is that Safari is failing to keep the closure variable available when it has the same name as the getter and is only referenced by that getter. I assume it is common for browsers to try to optimize closures by discarding variables that aren’t used. So it seems the code analysis done by Safari to see if a variable is used, is failing to mark a variable as being used when it is in a getter with the same name.

I have submitted a bug to Apple about this. Their bug tracker is private though, so there is no way for you to see that bug.

So to summarize, you should not name your getters the same as your closure variables.

Server-side upload time tracking

I wanted to see if we could roughly log how long users are spending waiting for learner data uploads. The more accurate way to do this is on the client side. However I wanted to try it on the server side so it could be applied in many cases without needing instrumented clients that send back data to the server.

I looked around for a while to see if this has been documented anywhere, but I didn’t find anything. So I decided to try something and test it to see if it would work.

The Conclusion

Yes it is possible. At least the ‘%t’ option when added to the request headers is the time which the request is started. This time is before most of the POST data is uploaded, so it can be used to get an estimate of upload times. This estimate seems very good with my testing, but it should be verified in the real world of computers in schools before relying on it for something important.

The Test

The idea for this test came from Aaron Unger.

In summary it was tested with a simple Rack app running on a EC2 server that was identical to the servers we use to run our portals. Then on the client side I used curl and Charles (the personal proxy) to send it a large chunk of data and record the timing.

The server was running Apache 2.2.22 and it was configured with a Passenger web app. I won’t go into that setup here. Additionally I added this to the Apache configuration:

RequestHeader set X-Queue-Start "%t"

Then in the web app folder I added this config.ru file:

run lambda { |env| 
  start_time = Time.now
  if env['rack.input']
    env['rack.input'].read
  end
  sleep 5
  [200, {"Content-Type" => "text/plain"}, 
    ["Apache Start Time: #{env['HTTP_X_QUEUE_START']}\n" +
     "Start Time: #{start_time}\n" +
     "End Time: #{Time.now}\n"]]
}

Then on my local machine. I ran Charles the personal proxy. This starts a proxy on port 8888.

I made a large random data file with:

head -c 2000000 random_data

Then I sent that off to the server with curl:

% time curl -x localhost:8888 --data-urlencode something@random_data http://testserver-on-aws
Apache Start Time: t=1359399773413862
Start Time: 2013-01-28 19:02:55 +0000
End Time: 2013-01-28 19:03:00 +0000
.
real    0m8.229s
...

Converting the time stamp shows the apache start time is 3 seconds before the start time. The simple server always waits for 5 seconds so together this makes up the 8 seconds reported. Bingo!

I wasn’t convinced that the 3 seconds was actually the upload time. I thought perhaps it was some apache processing time that happened after the upload. So I used the throttle option in Charles to slow down the upload. Doing this gave the expected result: the apache start time was even earlier than before. And subtracting the end time from the apache start time was very close to the total request time reported on the command line.

Notes

This server side approach does not cover all the time that user is waiting for an upload to complete. I would guess there will be cases when it isn’t accurate. For example some proxy or other network device might delay POST requests in someway and in that case this approach would not record that time.

Open source spin-off projects

Developers at the Concord Consortium work on a wide variety of grants, and in the process we create reusable pieces of code. With a little work some of these reusable bits of code can be turned into spin-off projects that have a life of their own. In my opinion these spin-off projects have the best potential for broad long-term impact.

Recently I was reminded about these types of spin-off projects when Richard Klancer relayed a conversation he had with Jeremy Ashkenas. Jeremy has been very successful in this area during his work on DocumentCloud.

We strive to make our individual projects successful, but often their technology is complex and not easy to re-use. The impact of the individual project is the research enabled by the technology, or demonstrating the usefulness of a new concept. However, the collection of technologies used in the project normally becomes a one-off: it is no longer used once the project reaches its 2-5 year end.

Alternatively, within these complex projects are reusable pieces of code that are simple, easy to maintain, and solve a common need. Because of this they have potential to be popular outside of our organization. We do have some partial successes with spin-offs like this.

  • MozSwing – mostly abandoned, though it was used in at least one commercial product
  • Java Sensor Library – collection of JAR files for communicating with a variety of sensors available in schools
  • RaphaelViews – SproutCore 1.x library for creating fully fledged SproutCore 1.x views with Raphael
  • SproutCore TestDriver – ruby gem for running SproutCore Jasmine and QUnit tests on a CI server

None of these has become a successful open source spin-off project. To be successful, such a project needs an active community that includes both developers and users. And the amount of work required to maintain it by Concord Consortium developers needs to be small enough that it doesn’t prevent us from reaching the goals of individual grant projects.

The MozSwing project would require too much maintenance. The Java sensor project is too intertwined in our other Java code. RaphaelViews and CapybaraTestrunner don’t have the above problems, but they have not been polished and announced to the right audience. I don’t think the polishing would take a lot of effort, but making the time and finding the support to do so is hard. We are always working on the next big thing, so it takes discipline to really finish up what is already working internally.

There are more potential open source spin-off projects within the technology at the Concord Consortium that have wider audiences than the ones above. With luck, we can change our culture to encourage this work more and make more of this great stuff accessible.

Do you agree that we should be spinning off more projects? Do you have experience with spinning off projects like these? Any tips?

Idea for a concise syntax of nested models in cucumber

I started thinking about how to more easily specify some of the deeply nested structures we need during testing.

First we already have a step for doing this.  An example which looks like this:

And the following investigations with multiple choices exist:
| investigation         | activity    | section     | page       | multiple_choices  | image_questions   |
| first investigation   | act 1     | section 1  | page 1   | a, b              |                   |
| first investigation   | act 2     | section 2  | page 2   | c, d              |                   |
| second investigation  | act 3     | section 3  | page 3   | b_a, b_b          | image_q           |
| second investigation  | act 4     | section 4  | page 4   | b_c, b_d          |                   |

That is using some previously defined multiple_choice and image_question objects. I was trying to see if I could write it more like this:

And the following investigations with multiple choices exist:
      investigation "first investigation"
        activity "act 1"
          section "section 1"
            page "page 1"
              multiple_choice :prompt => "Prompt 1", :choices => "a,b,c,d", :correct_answer => "a"
              multiple_choice :prompt => "Prompt 2", :choices => "a,b,c,d", :correct_answer => "a"
            page "page 2"
              image_question :prompt => "Image Prompt"

I didn’t see an easy way to do that but with a little method_missing, haml, and using cucumber’s “”” notation, the following looks pretty straight forward:

And the following investigations with multiple choices exist:
      """
      - investigation "first investigation" do
        - activity "act 1" do
          - section "section 1" do
            - page "page 1" do
              - multiple_choice :prompt => "Prompt 1", :choices => "a,b,c,d", :correct_answer => "a" do
              - multiple_choice :prompt => "Prompt 2", :choices => "a,b,c,d", :correct_answer => "a" do
            - page "page 2" do
              - image_question :prompt => "Image Prompt" do
      """

In a simple implementation a map could be used to define the code to add a child object. So for investigation it would be {activities<<child}, and for page it is {add_element(child)}. The creation of of the objects could use factory girl so the names could be any of the existing factories.

I’m curious if anyone has seen something like this that is already written? And I’m wondering if there is something like haml that uses indentation for blocks but defaults to “ruby mode” so there wouldn’t be a need for the “- ” before each line.