lib/test1.ex

defmodule Test1 do
#  import ExProf.Macro
  import Xpeg

  def time(what, f) do
    #Task.start(fn -> 
    {t1, _} = :erlang.statistics(:runtime)
    r = f.()
    {t2, _} = :erlang.statistics(:runtime)
    IO.puts("#{what} #{(t2-t1)/1000}")
    r
    #end)
  end

  def do_prof(lines, pid) do
    i = :erlang.process_info(pid, :current_location)
    {_, {_, _, _, line}} = i
    lines = Map.update(lines, line, 1, &(&1+1))
    receive do
      :stop -> 
        Enum.sort(lines, fn {_, a}, {_, b} -> a > b end)
        |> Enum.take(20)
        |> IO.inspect()
    after
      0 -> do_prof(lines, pid)
    end
  end

  def prof(f) do
    pid = self()
    {:ok, tracer_pid} = Task.start(fn -> do_prof(%{}, pid) end)
    f.()
    send(tracer_pid, :stop)
  end


  #_ = """
  def performance do
    p =
      Xpeg.peg :json, dump_code: true, trace: false, dump_ir: true do
        # White space
        :s <- star({' ', '\t', '\r', '\n'})

        # Basic atoms
        true <- "true" * fn cs -> [true | cs] end
        false <- "false" * fn cs -> [false | cs] end
        :null <- "null" * fn cs -> [nil | cs] end

        # Parse strings - needs proper escaping for the capture
        :xdigit <- {'0'..'9', 'a'..'f', 'A'..'F'}
        :unicode_escape <- 'u' * :xdigit[4]
        :escape <- '\\' * ({'"', '\\', '/', 'b', 'f', 'n', 'r', 't'} | :unicode_escape)
        :string_body <- star(:escape) * star(+({'\x20'..'\x7f'} - {'"'} - {'\\'}) * star(:escape))
        :string <- '"' * str(:string_body) * '"'

        # Numbers are converted to Elixir float
        :minus <- '-'
        :int_part <- '0' | {'1'..'9'} * star({'0'..'9'})
        :fract_part <- "." * +{'0'..'9'}
        :exp_part <- {'e', 'E'} * opt({'+', '-'}) * +{'0'..'9'}

        :number <- float(opt(:minus) * :int_part * opt(:fract_part) * opt(:exp_part)) 

        # Objects are represented by an Elixir map
        :obj_pair <-
          :s * :string * :s * ":" * :value *
            fn [v, k, obj | cs] -> [Map.put(obj, k, v) | cs] end

        :object <- '{' * fn cs -> [%{} | cs] end * (:obj_pair * star("," * :obj_pair) | :s) * "}"

        # Arrays are represented by an Elixir list
        :array_elem <- :value * fn [v, a | cs] -> [[v | a] | cs] end

        :array <- "[" * fn cs -> [[] | cs] end * (:array_elem * star("," * :array_elem) | :s) * "]" *
            fn [a | cs] -> [Enum.reverse(a) | cs] end

        # All possible JSON values
        :value <- :s * (:number | :string | :object | :array | true | false | :null) * :s

        # The toplevel json document is a value with no other trailing characters
        :json <- :value * !1
      end
      
    #Module.create(Koe, p, Macro.Env.location(__ENV__))

    s = File.read!("/tmp/json-32M.bzip2.out")
    s2 = to_charlist(s)

    Poison.decode!(s)
    Poison.decode!(s)
    
    time("poison", fn ->
      Poison.decode!(s)
    end)
    
    time("jason", fn ->
      Jason.decode!(s)
    end)

    time("xpeg", fn ->
      #prof("xpeg", fn ->
        Xpeg.match(p, s2)
      #end)
    end)

    :nop


    #nil

  end
  #"""

  def run() do
    p =
      peg :flop, dump_code: true, trace: false, dump_ir: true, userdata: false do
        :p <- "37.791"
        :flop <- str(star(1 - :p) * :p)
      end

    s = File.read!("/tmp/json-32M.bzip2.out")
    s2 = to_charlist(s)

    time("search", fn ->
      prof(fn ->
        r = Xpeg.match(p, s2)
      end)
    end)
    
  end

end