alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lift Framework example source code file (Serbench.scala)

This example Lift Framework source code file (Serbench.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

array, date, employee, employee, io, java, list, list, nick, no, project, string, string, team, team, util

The Lift Framework Serbench.scala source code

import net.liftweb.json._
import net.liftweb.json.Serialization.{read, write}
import java.io._
import java.util.Date

object Serbench extends Benchmark {
  val classes = List(classOf[Project], classOf[Team], classOf[Employee], classOf[Language])
  val project = Project("test", new Date, Some(Language("Scala", 2.75)), List(
    Team("QA", List(Employee("John Doe", 5), Employee("Mike", 3))),
    Team("Impl", List(Employee("Mark", 4), Employee("Mary", 5), Employee("Nick Noob", 1)))))

  def main(args: Array[String]) = {
    println("** No type hints")
    new Bench()(Serialization.formats(NoTypeHints))
    println("** Short type hints")
    new Bench()(Serialization.formats(ShortTypeHints(classes)))
    println("** Full type hints")
    new Bench()(Serialization.formats(FullTypeHints(classes)))
  }

  class Bench(implicit formats: Formats) {
    benchmark("Java serialization (full)") { deserialize(serialize(project)) }
    benchmark("lift-json (full)") { read[Project](write(project)) }
    benchmark("Java serialization (ser)") { serialize(project) }
    benchmark("lift-json (ser)") { write(project) }
    val ser1 = serialize(project)
    val ser2 = write(project)
    benchmark("Java serialization (deser)") { deserialize(ser1) }
    benchmark("lift-json (deser)") { read[Project](ser2) }

    def benchmark(name: String)(f: => Any) = run(name, 20000, 20000)(f)

    def deserialize(array: Array[Byte]) =
      new ObjectInputStream(new ByteArrayInputStream(array)).readObject.asInstanceOf[Project]

    def serialize(project: Project) = {
      val baos = new ByteArrayOutputStream()
      val oos = new ObjectOutputStream(baos)
      oos.writeObject(project)
      baos.toByteArray
    }
  }

  @serializable
  case class Project(name: String, startDate: Date, lang: Option[Language], teams: List[Team])
  @serializable
  case class Language(name: String, version: Double)
  @serializable
  case class Team(role: String, members: List[Employee])
  @serializable
  case class Employee(name: String, experience: Int)
}

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework Serbench.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.