annotate 4-Scala/sizer/src/main/scala/example/sizer.scala @ 70:4198fa4e0df4

Add Scala code and notes
author IBBoard <dev@ibboard.co.uk>
date Sat, 14 Oct 2017 13:39:41 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 package example
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 import scala.io._
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 import akka.actor.{Actor,ActorSystem,Props}
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 object PageLoader {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 def getPageSize(url : String) : Int = {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 try {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 return Source.fromURL(url).mkString.length
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 } catch {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 case e:Exception =>
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 return -1
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 class ConcurrentActor extends Actor {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 def receive = {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 case (url, size) => println("Size for " + url + ": " + size)
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 object Main {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 val urls = List(
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 "https://www.amazon.com/",
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 "https://twitter.com/",
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 "https://www.google.co.uk/",
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 "https://www.bbc.co.uk/news/"
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 )
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 val system = ActorSystem("MySystem")
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 val actor = system.actorOf(Props[ConcurrentActor], name="concurrent-actor")
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 def timeMethod(method: () => Unit) = {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 val start = System.nanoTime
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 method()
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 val end = System.nanoTime
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 println("+++ Method took " + (end - start) / 1000000000.0 + " seconds.")
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 def getPageSizeSequentially() = {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 for (url <- urls) {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 println("Size for " + url +": " + PageLoader.getPageSize(url))
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 def getPageSizeConcurrently() = {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 for (url <- urls) {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 // Send a message to the actor (which won't arrive until the PageLoader call completes)
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 println(s"Sending message for $url")
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 actor ! (url, PageLoader.getPageSize(url))
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 def main(args: Array[String]) {
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 println("Sequential:")
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 timeMethod { getPageSizeSequentially }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 println("Concurrent:")
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 timeMethod { getPageSizeConcurrently }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 system.stop(actor)
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 system.terminate()
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 }
4198fa4e0df4 Add Scala code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 }