Skip to content

Instantly share code, notes, and snippets.

@aslakhellesoy
Created July 4, 2012 09:35
Show Gist options
  • Select an option

  • Save aslakhellesoy/3046369 to your computer and use it in GitHub Desktop.

Select an option

Save aslakhellesoy/3046369 to your computer and use it in GitHub Desktop.
Small script to illustrate the speed and simplicity of Webbit.
#!/bin/bash
# Small script to illustrate the speed and simplicity of Webbit.
# For more intersting stuff using WebSockets, see https://github.com/webbit/webbit
if [ ! -f webbit-full.jar ];
then
wget https://oss.sonatype.org/content/repositories/releases/org/webbitserver/webbit/0.4.11/webbit-0.4.11-full.jar -O webbit-full.jar
fi
cat <<-EOF > TryWebbit.java
import org.webbitserver.HttpControl;
import org.webbitserver.HttpHandler;
import org.webbitserver.HttpRequest;
import org.webbitserver.HttpResponse;
import org.webbitserver.WebServer;
import org.webbitserver.WebServers;
import java.io.IOException;
import java.net.URLConnection;
import java.util.concurrent.ExecutionException;
public class TryWebbit {
public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
int n = 100;
long s1 = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
long t1 = System.currentTimeMillis();
String resp = startGetStop();
long t2 = System.currentTimeMillis();
System.out.println(String.format("%s (%dms)", resp, t2 - t1));
}
long s2 = System.currentTimeMillis();
System.out.println(String.format("Total %dms, Avg: %fms", s2 - s1, ((double) (s2 - s1)) / n));
}
private static String startGetStop() throws InterruptedException, ExecutionException, IOException {
WebServer webServer = WebServers.createWebServer(9987).add(new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
response.content("pølsemaker").end();
}
}).start().get();
URLConnection conn = webServer.getUri().toURL().openConnection();
byte[] content = new byte[conn.getContentLength()];
conn.getInputStream().read(content);
String resp = new String(content, "UTF-8");
webServer.stop().get();
return resp;
}
}
EOF
if [ ! -f TryWebbit.class ];
then
javac -classpath webbit-full.jar TryWebbit.java
fi
time java -classpath .:webbit-full.jar TryWebbit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment