Exploring the Scala Macro System for Compile Time Model-Based - - PowerPoint PPT Presentation

exploring the scala macro system for compile time model
SMART_READER_LITE
LIVE PREVIEW

Exploring the Scala Macro System for Compile Time Model-Based - - PowerPoint PPT Presentation

Exploring the Scala Macro System for Compile Time Model-Based Generation of Statically Type-Safe REST Services Filipe R. R. Oliveira ei10038@fe.up.pt July 20, 2015 Supervisor Hugo Sereno Ferreira Co-supervisor Tiago Boldt Sousa Context


slide-1
SLIDE 1

Exploring the Scala Macro System for Compile Time Model-Based Generation of Statically Type-Safe REST Services

Filipe R. R. Oliveira

ei10038@fe.up.pt July 20, 2015

Supervisor Hugo Sereno Ferreira Co-supervisor Tiago Boldt Sousa

slide-2
SLIDE 2

Growth of web services

Context

2

slide-3
SLIDE 3

Context

How is complexity handled?

REST

3

slide-4
SLIDE 4

Context

Traditional

4

Resource A GET POST ... Resource B GET POST ...

slide-5
SLIDE 5

Model-driven

CRUD

Context

Traditional

5

Resource A GET POST ... Resource B GET POST ...

slide-6
SLIDE 6

Model-Driven Engineering

6

slide-7
SLIDE 7

Model-Driven Frameworks

  • Python
  • Highly customizable
  • Python
  • Only supports MongoDB
  • JavaScript (Node.js)
  • Model definition via terminal
  • JavaScript (Node.js)
  • Scaffolding via terminal

7

slide-8
SLIDE 8

Motivation

8

Less Performance

Runtime Reflection

slide-9
SLIDE 9

Motivation

9

Less Performance Longer Debugging

Dynamically Typed Runtime Reflection

slide-10
SLIDE 10

Research Problem

Considering a statically type-safe programming language that enables generation of REST services in compile time. Can a model-driven REST framework written in this language improve the development process and execution performance when compared to current ones?

10

slide-11
SLIDE 11

Research Process

  • 1. Review

a. REST frameworks b. Model-driven development

  • 2. Develop a proof of concept

a. In Scala b. Using macros

  • 3. Validate

a. Benchmarks b. Academic quasi-experiment

11

slide-12
SLIDE 12

Scala Macros

def macros

printf("Hello, %s!", "World") print("Hello, World!")

12

slide-13
SLIDE 13

Scala Macros

def macros

printf("Hello, %s!", "World") print("Hello, World!")

macro annotations

@json case class Category(name: String, description: String) case class Category(name: String, description: String) { def toJson: String = "{" + "name:" + name + "," + "description:" + description + "}" }

13

slide-14
SLIDE 14
  • Capture models
  • Customizable operations
  • Configurable

14

Proof of Concept

slide-15
SLIDE 15

Metamorphic Application Architecture

Either synchronous or asynchronous

15

slide-16
SLIDE 16

Metamorphic Application Architecture

Create | GetAll | Get | Replace | Delete

16

slide-17
SLIDE 17

Metamorphic Application Architecture

metamorphic { host = "111.111.111.111" // default "localhost" port = 9000 // default 8080 databases.default.name = "file.db" }

17

slide-18
SLIDE 18

Metamorphic Process

18

Representation

UML

slide-19
SLIDE 19

@entity class Category { def name = StringField() def description = StringField() }

Metamorphic Process

19

Specification

Internal DSL

Representation

UML

slide-20
SLIDE 20

@entity class Category { def name = StringField() def description = StringField() }

Metamorphic Process

20

Specification

Internal DSL

slide-21
SLIDE 21

@entity class Category { def name = StringField() def description = StringField() } Entity("Category", List( Property("name", String, …), Property("description", String, …) )

Metamorphic Process

21

Matching

Macro expansion

Specification

Internal DSL

slide-22
SLIDE 22

@entity class Category { def name = StringField() def description = StringField() } Entity("Category", List( Property("name", String, …), Property("description", String, …) )

Metamorphic Process

22

Matching

Macro expansion

Specification

Internal DSL

slide-23
SLIDE 23

@entity class Category { def name = StringField() def description = StringField() } Entity("Category", List( Property("name", String, …), Property("description", String, …) )

Metamorphic Process

23

Matching

Macro expansion

Specification

Internal DSL

slide-24
SLIDE 24

Entity("Category", List( Property("name", String, …), Property("description", String, …) )

Metamorphic Process

24

Matching

Macro expansion

slide-25
SLIDE 25

Entity("Category", List( Property("name", String, …), Property("description", String, …) ) Application( Service("CategoryService", List( Operation(Get, Path("categories", …), …), Operation(Post, Path("categories", …), …), … ) )

Metamorphic Process

25

Transformation

Macro expansion

Matching

Macro expansion

slide-26
SLIDE 26

Entity("Category", List( Property("name", String, …), Property("description", String, …) ) Application( Service("CategoryService", List( Operation(Get, Path("categories", …), …), Operation(Post, Path("categories", …), …), … ) )

Metamorphic Process

26

Transformation

Macro expansion

Matching

Macro expansion

slide-27
SLIDE 27

Entity("Category", List( Property("name", String, …), Property("description", String, …) ) Application( Service("CategoryService", List( Operation(Get, Path("categories", …), …), Operation(Post, Path("categories", …), …), … ) )

Metamorphic Process

27

Transformation

Macro expansion

Matching

Macro expansion

slide-28
SLIDE 28

Application( Service("CategoryService", List( Operation(Get, Path("categories", …), …), Operation(Post, Path("categories", …), …), … ) )

Metamorphic Process

28

Transformation

Macro expansion

slide-29
SLIDE 29

Application( Service("CategoryService", List( Operation(Get, Path("categories", …), …), Operation(Post, Path("categories", …), …), … ) )

  • bject MyApp extends App {

case class Category( id: Option[Int], name: String, description: String ) // Repository[Category] // CategoryService }

Metamorphic Process

29

Generation

Macro result

Transformation

Macro expansion

slide-30
SLIDE 30
  • bject MyApp extends App {

case class Category( id: Option[Int], name: String, description: String ) // Repository[Category] // CategoryService }

Metamorphic Process

30

Generation

Macro result

slide-31
SLIDE 31
  • bject MyApp extends App {

case class Category( id: Option[Int], name: String, description: String ) // Repository[Category] // CategoryService } POST http://localhost:8080/categories/ GET http://localhost:8080/categories/ PUT http://localhost:8080/categories/1/ DELETE http://localhost:8080/categories/1/ GET http://localhost:8080/categories/1/ { "id": 1, "name": "Cameras", "description": "High quality cameras." }

Metamorphic Process

31

Client App Generation

Macro result

slide-32
SLIDE 32

Metamorphic Internal DSL

import metamorphic.dsl._ @app object RESTApp { @entity class Category { def name = StringField() def description = StringField() } class CategoryService extends EntityService[Category] { def create(category: Category) = { if (category.name.length < 5) Response("Name is too short.", BadRequest) else super.create(category) } } }

32

slide-33
SLIDE 33

Benchmarks

Does Metamorphic has better execution performance?

33

slide-34
SLIDE 34

Benchmarks Implementations

  • 1. Metamorphic sync
  • 2. Metamorphic async
  • 3. LoopBack
  • 4. Sails
  • 5. Django REST Python 2.7
  • 6. Django REST Python 3.4
  • 7. Eve*

34

PostgreSQL 9.3.8 *MongoDB Production environments

slide-35
SLIDE 35

Benchmarks Execution

35

Entity type

Simple | Has Object | Has List

Operation type

Create | GetAll | Get | Replace | Delete

slide-36
SLIDE 36

Discarded the first

1000 requests

Benchmarks Execution

Concurrency

up to 10

36

Executed

5000 requests Operation type

Create | GetAll | Get | Replace | Delete

Entity type

Simple | Has Object | Has List

slide-37
SLIDE 37

Benchmarks Results

37

GetAll

slide-38
SLIDE 38

Benchmarks Results

38

Framework Create GetAll Get Replace Delete Sum LoopBack 1 | 1 | 1 3 | 2 | 1 1 | 1 | 1 2 | 2 | 2 1 | 1 | 1 21 Metamorphic Async 2 | 2 | 2 1 | 1 | 2 3 | 2 | 2 . 1 | 1 | 1 . 2 | 2 | 2 26 Sails 3 | 3 | 6 2 | 6 | 4 2 | 3 | 3 3 | 3 | 6 3 | 3 | 5 55 Django REST 3.4 5 | 5 | 4 5 | 4 | 5 6 | 5 | 5 5 | 4 | 3 4 | 4 | 4 68 Django REST 4 | 4 | 3 6 | 5 | 6 5 | 6 | 6 4 | 5 | 4 5 | 5 | 3 71 Metamorphic 6 | 6 | 5 4 | 3 | 3 4 | 4 | 4 6 | 6 | 5 6 | 6 | 6 74 Eve* 1 | 1 | 1 4 | 3 | 2 4 | 3 | 3 1 | 1 | 2 3 | 3 | 3 35

slide-39
SLIDE 39

Academic Quasi-Experiment

Is Metamorphic quick and easy to use, and error preventive?

39

slide-40
SLIDE 40

Experiment Design

Baseline treatment

40

sync

Experimental treatment 8 students (5th year)

slide-41
SLIDE 41

Experiment Description

Online shop application 5 entities

41

Tasks

1. Modeling 2. Creation of services 3. Customization

slide-42
SLIDE 42

Experiment Questionnaires

The goal

  • Statistical validity
  • Features evaluation

42

Analysis

  • Wilcoxon-Mann-Whitney test
  • 5% significance level
slide-43
SLIDE 43

Experiment Questionnaires Results

Are functionalities easier and more intuitive to use?

43

Functionality

ρ - Round 1 ρ - Round 2

Modeling 0.043 0.200 Creation of services 0.014 0.014 Customization 0.014 0.100

slide-44
SLIDE 44

Experiment Questionnaires Results

Rapid prototyping Production-level

44

slide-45
SLIDE 45

Experiment Time Measurement

45

Measurement Round

x̅E σE x̅B σB x̅B − x̅E (x̅B − x̅E) / x̅B

Task 1: Modeling 1 11.44 03.11 20.13 09.18 08.69 43.2% 2 07.94 01.48 08.81 03.06 00.87 09.9% Task 2: Services 1 08.31 06.46 22.94 13.73 14.63 63.8% 2 10.69 05.76 17.25 01.49 06.56 38.0% Task 3: Customization 1 05.06 02.12 03.92 02.67

  • 1.14
  • 29.1%

2 05.31 01.91 03.44 00.97

  • 1.87
  • 54.4%

Total 1 45.81 08.19 70.56 12.14 24.75 35.1% 2 31.50 01.86 51.38 08.29 19.88 38.7%

slide-46
SLIDE 46

Experiment Error Measurement

46

Measurement Round

x̅E σE x̅B σB

# Compile-time errors 1 02.8 2.06 01.5 1.29 2 04.3 1.71 00.3 0.50 # Runtime errors 1 00.0 . 0.00 05.8 4.99 2 00.3 . 0.50 05.5 1.73

slide-47
SLIDE 47

Contributions

  • 1. Research on model-driven REST frameworks
  • 2. Meta-architecture
  • 3. Verified implementation
  • 4. Benchmark results
  • 5. Academic quasi-experiment results

47

slide-48
SLIDE 48

Future Work

  • 1. Extension of models
  • 2. Test models
  • 3. Profiling
  • 4. Experiments
  • 5. Swagger integration

48

slide-49
SLIDE 49

Paper submitted to ModComp’15 Special thanks to Eugene Burmako

EPFL, Team Member of Scala Macros

16 upvotes (100%) reddit.com/user/frroliveira/ 31 stars github.com/frroliveira/metamorphic

“The project looks awesome!”

github.com/pathikrit

“looks extremely promising..”

github.com/Daxten

slide-50
SLIDE 50

Extra def macros

Implementation

def printf(format: String, params: Any*): Unit = macro impl def impl(c: Context)(format: c.Expr[String], params: c.Expr[Any]*): c.Expr[Unit] = { //... c.Expr[Unit](q"print($result)") }

Usage

printf("Hello, %s!", "World")

50

slide-51
SLIDE 51

Extra Macro Annotations

Implementation

class serializable extends StaticAnnotation { def macroTransform(annottees: Any*): Any = macro Implementation.json } class Implementation(val context: Context) { def json(annottees: c.Expr[Any]*): c.Expr[Any] = ??? }

Usage

@json case class Category(name: String, description: String)

51

slide-52
SLIDE 52

Extra Application Model

52

slide-53
SLIDE 53

Extra Metametamodel

53

slide-54
SLIDE 54

Extra Dependency Injection

for services for repositories

54