How to setup a JSON webservice with Play Framework 1.2.4 (for beginners)

Step 1: Installation

1. Download play framework 1.2.4 http://download.playframework.org/releases/play-1.2.4.zip and extract it to a folder of your preference.

2. Download Java http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html if you don’t have it.

3. Extract it and add the play framework and java/bin to your “PATH” environment variable (Click here for a demonstration)

4. Start -> Run -> cmd <press enter>, and type “java -version”  to see if it works properly, and also test  “play” as well.

5. Download Eclipse IDE for Java Developers and extract it to a folder of your choosing.

Step 2: Setting up application base

1. Via command line, go type “cd Desktop” (optional)

2. Type “play new jsonplay” to create a new play project named jsonplay in the folder jsonplay (if this did not work, your path is not setup correctly, repeat installation step 3)

3. jsonplay project should show up on your desktop, and you can now also start it up, so  “cd jsonplay”

4. Type “play run” and point your browser to http://localhost:9000 and you should see a nice welcome screen.

5. Press CTRL+C to stop the server in your command prompt.

6. While in the same directory as your project, type “play eclipsify” and open eclipse.

7. In eclipse, press File -> Import -> Existing Projects into Workspace, and place the root directory to your Desktop/jsonplay folder and press finish (leave everything else untouched)

Step 3: Lets write our application

1. Expand the project jsonplay then app and right click models -> new -> class, name it Category (leave everything else default)

2. Create one more class right click models -> new -> class, name it Product (leave everything else default)

3. In Product class, add     “public String name;” and  “public Integer price;”

4. In Category add three members to the class,  “public String name;” and “public Integer type;” and “public ArrayList<Product> products;” and press CTRL+SHIFT+O  to import any missing classes.

should look like so

Category.java

package models;

import java.util.ArrayList;

public class Category {
   public String name;
   public Integer type;
   public ArrayList<Product> products;
}

Product.java

package models;

public class Product {
    public String name;
    public Integer price;
}

5. Right click controllers, and add a new class named Manager, that has the superclass of  “play.mvc.Controller” leave everything else default, and click Finish.

Replace contents with this

package controllers;

import java.util.ArrayList;

import models.Category;
import models.Product;
import play.mvc.Controller;

public class Manager extends Controller {

    public static void productList() {
        Product product1 = new Product();
        product1.name = “Advil”;
        product1.price = 5;

        Product product2 = new Product();
        product2.name = “Tylenol”;
        product2.price = 6;

        Category category1 = new Category();
        category1.name = “Pharmacy”;
        category1.type = 1;
        category1.products = new ArrayList<Product>();
        category1.products.add(product1);
        category1.products.add(product2);

        Category category2 = new Category();
        category2.name = “Medicine”;
        category2.type = 1;
        category2.products = new ArrayList<Product>();
        category2.products.add(product1);
        category2.products.add(product2);

        ArrayList<Category> categories = new ArrayList<Category>();
        categories.add(category1);
        categories.add(category2);

        renderJSON(categories);
    }

    public static void echoVariables(String var1, String var2) {
        String[] words = {var1, var2};  
        renderJSON(words);
    }
}

6. Go back to your command prompt, and type  “play run

7. Point your browser to http://localhost:9000/manager/productList  it should display a json array. (where manager => Manager.java, and productList => your productList function)

and http://localhost:9000/manager/echoVariables?var1=myVariable&var2=mySecondVariable

 

Video of Step 2 & Step 3: http://www.youtube.com/watch?v=0rXAXtmGDvs&hd=1

Download project:  jsonplay.zip

VPS Performance comparison

I wanted to benchmark the VPS servers I currently have, to see if it is worth to upgrade to a dedicated host.

Short story..it is not worth it.

OpenVZ has very good performance for its guest hosts, according to wikipedia, it runs them at native speed. And after these tests I ran, you can really see that.

 

For the benchmark software, I used unixbench.

wget http://members.dslextreme.com/users/...1.0-wht.tar.gz

gunzip -dvc unixbench-4.1.0-wht.tar.gz | tar xvf -
cd unixbench-4.1.0-wht
make
./Run

For the seperate hard disk test I used the command

#dd if=/dev/zero of=test bs=64k count=16k conv=fdatasync

For the internet bandwidth test I used

wget -O /dev/null http://cachefly.cachefly.net/100mb.test

Chicago VPS OpenVZ Node #19

Intel(R) Xeon(R) CPU E5620 @ 2.40GHz

                     INDEX VALUES            
TEST                                        BASELINE     RESULT      INDEX

Dhrystone 2 using register variables        376783.7 25368226.7      673.3
Double-Precision Whetstone                      83.1     1322.2      159.1
Execl Throughput                               188.3     6096.3      323.8
File Copy 1024 bufsize 2000 maxblocks         2672.0   237206.0      887.7
File Copy 256 bufsize 500 maxblocks           1077.0    64525.0      599.1
File Read 4096 bufsize 8000 maxblocks        15382.0  1996205.0     1297.8
Pipe-based Context Switching                 15448.6   829385.4      536.9
Pipe Throughput                             111814.6  2832987.9      253.4
Process Creation                               569.3    27903.2      490.1
Shell Scripts (8 concurrent)                    44.8     1993.0      444.9
System Call Overhead                        114433.5  2333548.2      203.9
                                                                 =========
     FINAL SCORE                                                     447.7

HDD Test

1073741824 bytes (1.1 GB) copied, 16.5213 s, 65.0 MB/s

Internet Download Test

9.27M/s   in 11s

Quality Servers VPS OpenVZ Node #1

Intel(R) Xeon(R) CPU X3220 @ 2.40GHz

                     INDEX VALUES            
TEST                                        BASELINE     RESULT      INDEX

Dhrystone 2 using register variables        376783.7 24227145.5      643.0
Double-Precision Whetstone                      83.1     1668.3      200.8
Execl Throughput                               188.3     7277.6      386.5
File Copy 1024 bufsize 2000 maxblocks         2672.0   170035.0      636.4
File Copy 256 bufsize 500 maxblocks           1077.0    60890.0      565.4
File Read 4096 bufsize 8000 maxblocks        15382.0  1342551.0      872.8
Pipe-based Context Switching                 15448.6   839616.6      543.5
Pipe Throughput                             111814.6  2840492.1      254.0
Process Creation                               569.3    22624.9      397.4
Shell Scripts (8 concurrent)                    44.8     2033.7      454.0
System Call Overhead                        114433.5  2497916.0      218.3
                                                                 =========
     FINAL SCORE                                                     426.6

HDD Test

1073741824 bytes (1.1 GB) copied, 21.2502 s, 50.5 MB/s

Internet Download Test

10.5M/s   in 10s

Quality Servers VPS OpenVZ Node #2 

Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz

                     INDEX VALUES            
TEST                                        BASELINE     RESULT      INDEX

Dhrystone 2 using register variables        376783.7 31367351.7      832.5
Double-Precision Whetstone                      83.1     2516.8      302.9
Execl Throughput                               188.3    17305.0      919.0
File Copy 1024 bufsize 2000 maxblocks         2672.0   520265.0     1947.1
File Copy 256 bufsize 500 maxblocks           1077.0   154825.0     1437.6
File Read 4096 bufsize 8000 maxblocks        15382.0  3415200.0     2220.3
Pipe-based Context Switching                 15448.6  1387143.3      897.9
Pipe Throughput                             111814.6  4977139.8      445.1
Process Creation                               569.3    52963.1      930.3
Shell Scripts (8 concurrent)                    44.8     3626.4      809.5
System Call Overhead                        114433.5  4583721.6      400.6
                                                                 =========
     FINAL SCORE                                                     852.8

HDD Test

1073741824 bytes (1.1 GB) copied, 11.7998 s, 91.0 MB/s

Internet Download Test

7.11M/s   in 10s

Dedicated i7 Sandybridge with 6GB/s SSD

Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz

                     INDEX VALUES
TEST                                        BASELINE     RESULT      INDEX

Dhrystone 2 using register variables        376783.7 28551553.9      757.8
Double-Precision Whetstone                      83.1     2766.7      332.9
Execl Throughput                               188.3     4414.4      234.4
File Copy 1024 bufsize 2000 maxblocks         2672.0   505748.0     1892.8
File Copy 256 bufsize 500 maxblocks           1077.0   161333.0     1498.0
File Read 4096 bufsize 8000 maxblocks        15382.0  2353748.0     1530.2
Pipe Throughput                             111814.6  3177889.9      284.2
Pipe-based Context Switching                 15448.6   221905.0      143.6
Process Creation                               569.3    16984.6      298.3
Shell Scripts (8 concurrent)                    44.8     2389.0      533.3
System Call Overhead                        114433.5  3466751.0      302.9
                                                                 =========
     FINAL SCORE                                                     501.5

HDD Test

1073741824 bytes (1.1 GB) copied, 11.7998 s, 489.7 MB/s

Internet Download Test

9.50M/s   in 11s