“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey
Senior Software Engineer, UK

Playing with Phoenix Server

Upgrade Phoenix server.

Robs-iMac:repos roblacey$ mix archive.install hex phx_new 1.4.0
Resolving Hex dependencies...
Dependency resolution completed:
New:
  phx_new 1.4.0
* Getting phx_new (Hex package)
All dependencies up to date
Compiling 10 files (.ex)
Generated phx_new app
Generated archive "phx_new-1.4.0.ez" with MIX_ENV=prod
Are you sure you want to install "phx_new-1.4.0.ez"? [Yn] Y
* creating /Users/roblacey/.mix/archives/phx_new-1.4.0

Generate the application.

Robs-iMac:repos roblacey$ mix phoenix.new hello
* creating hello/config/config.exs
* creating hello/config/dev.exs
* creating hello/config/prod.exs
* creating hello/config/prod.secret.exs
* creating hello/config/test.exs
* creating hello/lib/hello.ex
* creating hello/lib/hello/endpoint.ex
* creating hello/test/views/error_view_test.exs
* creating hello/test/support/conn_case.ex
* creating hello/test/support/channel_case.ex
* creating hello/test/test_helper.exs
* creating hello/web/channels/user_socket.ex
* creating hello/web/router.ex
* creating hello/web/views/error_view.ex
* creating hello/web/web.ex
* creating hello/mix.exs
* creating hello/README.md
* creating hello/web/gettext.ex
* creating hello/priv/gettext/errors.pot
* creating hello/priv/gettext/en/LC_MESSAGES/errors.po
* creating hello/web/views/error_helpers.ex
* creating hello/lib/hello/repo.ex
* creating hello/test/support/model_case.ex
* creating hello/priv/repo/seeds.exs
* creating hello/.gitignore
* creating hello/brunch-config.js
* creating hello/package.json
* creating hello/web/static/css/app.css
* creating hello/web/static/css/phoenix.css
* creating hello/web/static/js/app.js
* creating hello/web/static/js/socket.js
* creating hello/web/static/assets/robots.txt
* creating hello/web/static/assets/images/phoenix.png
* creating hello/web/static/assets/favicon.ico
* creating hello/test/controllers/page_controller_test.exs
* creating hello/test/views/layout_view_test.exs
* creating hello/test/views/page_view_test.exs
* creating hello/web/controllers/page_controller.ex
* creating hello/web/templates/layout/app.html.eex
* creating hello/web/templates/page/index.html.eex
* creating hello/web/views/layout_view.ex
* creating hello/web/views/page_view.ex

Fetch and install dependencies? [Yn] Y
* running mix deps.get
* running npm install && node node_modules/brunch/bin/brunch build

We are all set! Run your Phoenix application:

    $ cd hello
    $ mix phoenix.server

You can also run your app inside IEx (Interactive Elixir) as:

    $ iex -S mix phoenix.server

Before moving on, configure your database in config/dev.exs and run:

    $ mix ecto.create

Looks like I had already created a database.

Robs-iMac:hello roblacey$ mix ecto.create
warning: found quoted keyword "test" but the quotes are not required. Note that keywords are always atoms, even when quoted, and quotes should only be used to introduce keywords with foreign characters in them
  mix.exs:52

Compiling 12 files (.ex)
Generated hello app
The database for Hello.Repo has already been created

So apparently Phoenix is super fast, scary fast. Well let’s see with ab 10 concurrent requests (longest request is 144ms)

Robs-iMac:hello roblacey$ ab -n 10 -c 10 http://phoenix.dv/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking phoenix.dv (be patient).....done


Server Software:        Cowboy
Server Hostname:        phoenix.dv
Server Port:            80

Document Path:          /
Document Length:        1931 bytes

Concurrency Level:      10
Time taken for tests:   0.059 seconds
Complete requests:      10
Failed requests:        0
Total transferred:      22600 bytes
HTML transferred:       19310 bytes
Requests per second:    169.19 [#/sec] (mean)
Time per request:       59.105 [ms] (mean)
Time per request:       5.910 [ms] (mean, across all concurrent requests)
Transfer rate:          373.41 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     8   16  10.7     13      46
Waiting:        7   16  10.6     13      45
Total:          8   16  10.6     13      46

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     13
  75%     13
  80%     14
  90%     46
  95%     46
  98%     46
  99%     46
 100%     46 (longest request)

1000 concurrent requests, 144ms

Robs-iMac:hello roblacey$ ab -n 1000 -c 1000 http://localhost:4000/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Cowboy
Server Hostname:        localhost
Server Port:            4000

Document Path:          /
Document Length:        1931 bytes

Concurrency Level:      1000
Time taken for tests:   0.152 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      2260000 bytes
HTML transferred:       1931000 bytes
Requests per second:    6569.65 [#/sec] (mean)
Time per request:       152.215 [ms] (mean)
Time per request:       0.152 [ms] (mean, across all concurrent requests)
Transfer rate:          14499.43 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   36  11.8     35      55
Processing:    25   64  16.7     69      90
Waiting:        6   64  16.8     69      90
Total:         46  100  24.2    103     144

Percentage of the requests served within a certain time (ms)
  50%    103
  66%    115
  75%    121
  80%    124
  90%    131
  95%    136
  98%    140
  99%    142
 100%    144 (longest request)

10,000 requests, Ok so I get a socket error. Too many open files (24) That’s ok, I’m not at that stage yet anyway.

Robs-iMac:hello roblacey$ ab -n 10000 -c 10000 http://localhost:4000/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
socket: Too many open files (24)

So that’s pretty fast, but that’s just a static page. This would be better tested with a page that does a little more, or an API request that reads from the database.

Lastly just adding a new route, controller and view and template for a /hello

diff --git a/web/controllers/hello_controller.ex b/web/controllers/hello_controller.ex
new file mode 100644
index 0000000..f3e97b4
--- /dev/null
+++ b/web/controllers/hello_controller.ex
@@ -0,0 +1,7 @@
+defmodule Hello.HelloController do
+  use Hello.Web, :controller
+
+  def index(conn, params) do
+    render(conn, "world.html")
+  end
+end
diff --git a/web/router.ex b/web/router.ex
index e7a505b..6df752f 100644
--- a/web/router.ex
+++ b/web/router.ex
@@ -15,7 +15,7 @@ defmodule Hello.Router do
 
   scope "/", Hello do
     pipe_through :browser # Use the default browser stack
-
+    get "/hello", HelloController, :index
     get "/", PageController, :index
   end
 
diff --git a/web/templates/hello/world.html.eex b/web/templates/hello/world.html.eex
new file mode 100644
index 0000000..e69de29
diff --git a/web/views/hello_view.ex b/web/views/hello_view.ex
new file mode 100644
index 0000000..00f1881
--- /dev/null
+++ b/web/views/hello_view.ex
@@ -0,0 +1,3 @@
+defmodule Hello.HelloView do
+  use Hello.Web, :view
+end

Intriguing.

GPK of the Day Dressy JESSE