Wednesday, August 29, 2012

Java: Helloooooooo, World!

I was talking to my buddy, Peter von der Ahé, today about optimizing startup time. He asked me to guess how many classes the JVM had to load for a simple hello world application written in Java. He also asked me how long such a program would take to run. I didn't know the answers, so he quickly typed it out on my friend's MacBook Pro.

Here's how to see how many classes are loaded:

java -verbose:class Hello

That results in 594 classes:

[Opened /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.lang.Object from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.lang.Comparable from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.util.Collections$UnmodifiableMap from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]

It takes roughly 0.2 seconds to run hello world:

time java Hello
Hello, World!

real 0m0.212s
user 0m0.221s
sys 0m0.051s

Kind of interesting. I know that this is the world's stupidest benchmark, but I decided to write the same thing in Python. It takes 0.04 seconds.

Dart currently takes 0.02 seconds.

Ruby 1.8.7 takes 0.009 seconds.

C takes 0.006 seconds.

(Warning: bad jokes coming...)

Of course, the real reason C is so fast is because it doesn't have any class ;-)

As I said earlier, this is a totally stupid benchmark. Never pick a programming language based on how long it takes to run hello world. What happens if you have to write a program that says something other than hello world, such as "hej, verden"? ;-)

Tuesday, August 21, 2012

Books: Memoirs Found in a Bathtub

I just finished listening to Memoirs Found in a Bathtub by Stanislaw Lem. It was recommended to me by someone long ago, but I can't remember who. All I can say is WTF!?! Fortunately, Wikipedia has a better summary:

Set in the distant future, Memoirs Found in a Bathtub is the horrifying first-hand account of a bureaucratic agent trapped deep within the subterranean bowels of a vast underground military complex. In a Kafkaesque maelstrom of terrifying confusion and utter insanity, this man must attempt to follow his mission directives of conducting an "on-the-spot investigation. Verify. Search. Destroy. Incite. Inform. Over and out. On the nth day nth hour sector n subsector n rendezvous with N."

The narrator inhabits a paranoid dystopia where nothing is as it seems, chaos seems to rule all events, and everyone is deeply suspicious of everyone else. In danger of losing his mind, the protagonist starts keeping a diary, and it is this diary which details only a few days in his life that is ultimately found by a future society and given the title Notes from the Neogene. Memoirs Found in a Bathtub is this distant voice from the past, this Notes from the Neogene.

Here's a quote from the book:

"I'll tell you. You're young, but you're one of us, and I'm one of us, so I'll tell you. Everything. Now, say someone's one of us. . . but he's also—you know—you can tell, right?"

"He's not—one of us," I said.

"Right! You can tell! But sometimes—you can't tell. You think someone's one of us, but they got to him and then he wasn't any more—and then we got to him, and he was—but he still has to look like he isn't, that is, like he only looks like he is! But they get wise to him and—now he isn't again, but he has to look like he isn't—or we'll get wise—and that's a triple!"

Tuesday, August 14, 2012

Python: Party Playlist Picker

At long last, Jeffrey Posnick and I finally got around to open sourcing a project that we started as my starter project at Google. Here is the blog post we posted on the YouTube API Blog:

When Google+ first came out, we were really excited about the idea of writing some open source code to showcase how you could integrate YouTube with Google+. After some brainstorming, we came up with the idea of letting people collaboratively edit a playlist in realtime, and we started hacking!

We started working on Party Playlist Picker in early 2011. We’ve always wanted to release our experiment as an open source project, and you can now download the code to see one example of integrating Google+ functionality to enrich a YouTube application.

Party Playlist Picker is a Google App Engine application written in Python. It uses a Google+ share button so that partygoers can invite their friends to come edit a playlist in realtime. That feature is based on the Google App Engine channel API. It makes use of the gdata-python-client library to talk to YouTube and the google-api-python-client library to talk to Google+. It’s currently using version 1 of the YouTube API (because that’s what gdata-python-client uses), and we used OAuth 2 for authentication. In fact, we made good use of the Python client library to manage the OAuth 2 tokens for us automatically.

There’s also a lot of jQuery-based JavaScript goodness. For instance, you can search for videos without reloading the page, and you can simply drag them to add them to the playlist. If multiple people edit the page at the same time, the list of videos is updated in realtime whenever a change is made. You can also watch videos right on the page. Last of all, it keeps track of who’s edited the playlist, and whether they’re currently editing it.

There are a bunch of subtle HTML5 tricks thrown in as well. For instance, the application supports voice search, and we made a clever use of gradients to fade overflow text in the video descriptions.

Here’s a screenshot of the application:

If you’re interested in learning more, you can play around with a running version of it, or you can download the source code and hack away! If you have any questions, post them to our developer forum.

--Shannon -jj Behrens and Jeffrey Posnick, YouTube API Team

Friday, August 03, 2012

Dart: My Quest to Program Dart on a Chromebook

They gave me a cool new Chromebook at work. Now, the sensible thing to do is to use it as a convenient web browser and nothing more. However, I'm not a very sensible person, and for some reason, I dislike having more than one computer at a time. Since I'm a member of the Dart team, I figured I should be prepared to use Dart Editor at any time of the day or night. Hence, I embarked on a multi-day journey, driven by my own obsessive compulsive disorder, to somehow figure out a way to run Dart Editor on a Chromebook.

By far, the easiest approach is probably to install Ubuntu on it. I didn't want to take that approach since I really wanted to use ChromeOS as well, and I hate dual booting.

The next approach is to put the machine in developer mode and install various things within the existing Linux distro. There are some pretty good instructions on that here. They involve setting up a build server, etc., which I balked at--I want fewer computers, not more computers!

My next idea was to install a full Linux distro in a directory and chroot into it. I thought I was the first one to come up with this idea, but apparently, several other people have had this idea as well (which is the way ideas usually work). There are pretty good instructions on how to do that here. By the way, that page covers the other approaches as well. I spent a couple days with this approach, and I made a lot of progress. However, I never got things to work just right. I faced a few major hurdles:

  • The Chromebook is already running a window manager, but it doesn't work as a normal X11 window manager. Dart Editor doesn't behave very well without a normal X11 window manager. Furthermore, getting another window manager installed and running is non-trivial, and it makes ChromeOS look ugly.
  • There's a lot of confusion about 32-bit vs. 64-bit binaries. If I understand correctly, the Chromebook is a 64-bit device running 32-bit binaries. There's also some confusion about 32-bit vs. 64-bit in the Dart Editor world which threw me off.
  • I was having a hard time getting all the right dependencies to get Dart Editor and Dartium running.

Eventually, I discovered that some companies...ahem...don't like it if you put a Chromebook into developer mode and then try to connect to the corporate network. That kind of makes sense since putting a Chromebook in developer mode circumvents many of the nice security features the Chromebook provides. Hence, even if I did manage to get any of the above approaches working, I wouldn't be able to use it at work.

My next approach was to look at Cloud9 IDE. It looks interesting, but it doesn't support Dart, and it definitely doesn't support Dart Editor.

My next approach was to try "Chromoting" into a Linux box in the cloud using the "Chrome Remote Desktop" extension. I figured that would be easy to get working. Best of all, this doesn't require any hacking of the Chromebook. However, it has a few major drawbacks:

  • It doesn't work when I'm on a train (which is one day a week).
  • It doesn't work when I'm on a plane. It may sound strange, but almost all of the time I've spent learning Dart was while on planes flying around to give various YouTube API talks.
  • If I use chromoting to give a talk on Dart, and my network connection goes down (which tends to happen every time you give a talk), I wouldn't be able to use Dart Editor.
  • Chromebooks don't currently have a VPN solution.
  • Chromebooks do support SPDY Proxy, but that doesn't currently work with Chromoting.

Even with all those drawbacks, I knew Chromoting was the only solution likely to work for my particular set of constraints, so that's what I got to work. I had to install a 64-bit JDK and the 64-bit version of Dart Editor, but I eventually got things working. Hence, the picture above is a picture of me Chromoting into my Linux box and running Dart Editor.

Ok, now that I've spent way too much time on this, and it's still not something I'd want to use on a daily basis, maybe it's time I get back to work ;)

Update September 11, 2012: I tried installing ChrUbuntu 12.04 on it using these instructions. I got Dart Editor working, but my wireless wouldn't work reliably.

I've heard that there are occasional driver issues when running 64-bit Linux on this device, so next I tried following these instructions. They walk you through installing ChrUbuntu 11.04 and upgrading twice to 12.04. Sure enough, this fixed my wireless problems. It temporarily broke my trackpad, but plugging in a mouse and rebooting fixed that problem. However, after following those instructions, I couldn't get Dart Editor to work. Whenever I tried to run it (from the command line), it would say, "Cannot find DartEditor". This is despite the fact that I was specifying the path to the executable. I think this is yet another weird 32-bit vs. 64-bit problem in the JRE.

The fact that Chromebooks have a funky EFF BIOS makes them really great for running ChromeOS securely but not so convenient when you're trying to run a normal version of Linux. Having been defeated twice more, I got a USB drive and installed ChromeOS from scratch and exited developer mode.

Update February 26, 2013: I was hoping that "emerge chromeos-dev" was going to be the path to success, but it turns out that it probably won't be, at least in the short term. See this forum post. Right now, Crouton looks to be the most viable approach. Also see this blog post.

Update February 27, 2013: Success! I got it to work, thanks to Crouton, which is based on using a chroot. See my newer blog post.