[Erlang 0094] Erlang 杂记 VI

  最近一直忙Storm相关的东西,今天抽时间整理一下Erlang笔记,这一次的内容主要是一些开源项目:Mock ,worker pool......
  
 

安装Erlan/OTP

  
 之前写过一个关于 Centos安装Erlang的文章,还是有人把Erlang编译安装这件事情做到了极致: Kerl
 
  Easy building and installing of Erlang/OTP instances;Kerl aims to be shell agnostic and its only dependencies, excluding what's required to actually build Erlang/OTP, are curl and git.
 
$ kerl list releases
Getting the available releases from erlang.org...
R10B-0 R10B-2 R10B-3 R10B-4 R10B-5 R10B-6 R10B-7 R10B-8 R10B-9 R11B-0 R11B-1
R11B-2 R11B-3 R11B-4 R11B-5 R12B-0 R12B-1 R12B-2 R12B-3 R12B-4 R12B-5 R13A
R13B R13B01 R13B02 R13B03 R13B04 R14A R14B R14B01 R14B02
Run "./kerl update releases" to update this list from erlang.org

 

  
 

Erlang 多语言混搭

  
   Erlang和其它语言混搭一下,效果如何?有不少这样的项目,有些纯属玩票,有的还是发展的不错;
 
Reia 
     Reia就属于玩票的那种,no realease ! no direct download links!
 
 Reia is a Ruby-like scripting language for the Erlang virtual machine. Reia brings you the best of both worlds between Ruby's friendly syntax, reflection, metaprogramming, and the amazing power of blocks, and Erlang's immense abilities for concurrency, distribution, hot code swapping, and fault tolerance.
Reia's source code is available on Github. There are no releases and thus no direct download links yet, sorry! See the README on Github for instructions on how to build Reia from source.
 
 项目地址:  http://reia-lang.org/
 
 
Erlang Meet Javascript
  
   Node.js着实让Javascript又火了一把 ,Erlang与Javascript又会擦出什么火花? 看看

 
 
Elixir
  发展比较不错的就属Elixir了,周边的项目也越来越多;
 
   Elixir is a functional meta-programming aware language built on top of the Erlang VM. It is a dynamic language with flexible syntax with macros support that leverages Erlang's abilities to build concurrent, distributed, fault-tolerant applications with hot code upgrades.
 
复制代码
defmodule Hello do
  IO.puts "Defining the function world"

  def world do
    IO.puts "Hello World"
  end

  IO.puts "Function world defined"end

Hello.world
复制代码

项目地址: http://elixir-lang.org/

相关链接:  http://expm.co/
 
 
Lua
 
Luerl - an implementation of Lua in Erlang   https://github.com/rvirding/luerl
 
Joxa
 
A Modern Lisp for the Erlang VM  https://github.com/ericbmerritt/joxa
 

Meck

 
    做测试Mock是利器,Erlang对应的解决方案是meck:
    With meck you can easily mock modules in Erlang. You can also perform some basic validations on the mocked modules, such as making sure no unexpected exceptions occurred or looking at the call history.
 
看看下面的例子是不是都是熟悉的东西:
   
复制代码
Eshell V5.8.4  (abort with ^G)
1> meck:new(dog).
ok
2> meck:expect(dog, bark, fun() -> "Woof!" end).
ok
3> dog:bark().
"Woof!"
4> meck:validate(dog).
true
5> meck:unload(dog).
ok
6> dog:bark().
** exception error: undefined function dog:bark/0

7> meck:expect(dog, meow, fun() -> meck:exception(error, not_a_cat) end).
ok
8> catch dog:meow().
{'EXIT',{not_a_cat,[{meck,exception,2},
                    {meck,exec,4},
                    {dog,meow,[]},
                    {erl_eval,do_apply,5},
                    {erl_eval,expr,5},
                    {shell,exprs,6},
                    {shell,eval_exprs,6},
                    {shell,eval_loop,3}]}}
9> meck:validate(dog).
true
复制代码

 

 项目地址: https://github.com/eproxus/meck

 还有一个gen_server_mock.erl 也可以看一下: https://github.com/jashmenn/gen_server_mock

 

 Poolboy 

 
    正如这个项目的描述 "A hunky Erlang worker pool factory"
复制代码
init([]) ->
    {ok, Pools} = application:get_env(example, pools),
    PoolSpecs = lists:map(fun({Name, SizeArgs, WorkerArgs}) ->
        PoolArgs = [{name, {local, Name}},
                    {worker_module, example_worker}] ++ SizeArgs,
        poolboy:child_spec(Name, PoolArgs, WorkerArgs)
    end, Pools),
    {ok, {{one_for_one, 10, 10}, PoolSpecs}}.

squery(PoolName, Sql) ->
    poolboy:transaction(PoolName, fun(Worker) ->
        gen_server:call(Worker, {squery, Sql})
    end).

equery(PoolName, Stmt, Params) ->
    poolboy:transaction(PoolName, fun(Worker) ->
        gen_server:call(Worker, {equery, Stmt, Params})
    end).
复制代码
 
 
 
 
常识一则:获取gen_server gen_fsm的运行时Status
 
   这应该算是常识不算技巧了,使用sys:get_status/1,可以获取gen_server等运行时的值.下面是process_info和sys:get_status的结果对比;这里使用的fsm_demo.erl是一个gen_fsm的程序.记录一下,我总是忘记它所在的模块;
 
复制代码
Eshell V5.9  (abort with ^G)
1> fsm_demo:start(abc).
{ok,<0.34.0>}
2> {_,Pid}=v(1).
{ok,<0.34.0>}
3> erlang:process_info(Pid).
[{registered_name,fsm_demo},
{current_function,{gen_fsm,loop,7}},
{initial_call,{proc_lib,init_p,5}},
{status,waiting},
{message_queue_len,0},
{messages,[]},
{links,[]},
{dictionary,[{'$ancestors',[<0.32.0>]},
              {'$initial_call',{fsm_demo,init,1}}]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.25.0>},
{total_heap_size,233},
{heap_size,233},
{stack_size,10},
{reductions,20},
{garbage_collection,[{min_bin_vheap_size,46368},
                      {min_heap_size,233},
                      {fullsweep_after,65535},
                      {minor_gcs,0}]},
{suspending,[]}]
4> sys:get_status(fsm_demo).
{status,<0.34.0>,
        {module,gen_fsm},
        [[{'$ancestors',[<0.32.0>]},
          {'$initial_call',{fsm_demo,init,1}}],
         running,<0.34.0>,[],
         [{header,"Status for state machine fsm_demo"},
          {data,[{"Status",running},
                 {"Parent",<0.34.0>},
                 {"Logged events",[]},
                 {"StateName",locked}]},
          {data,[{"StateData",{[],abc}}]}]]}
5> 
复制代码

 

2012年12月18日 14:10:24 补记

复制代码
Eshell V5.9  (abort with ^G)
1>  T={a,12,"good_man",<<2,3,4>>}.
{a,12,"good_man",<<2,3,4>>}
2> io:format("Data Dump:~p",[T]).
Data Dump:{a,12,"good_man",<<2,3,4>>}ok
3> rd(a,{age,name,desc}).
a
4> io:format("Data Dump:~p",[T]).
Data Dump:{a,12,"good_man",<<2,3,4>>}ok
5> T2=T.
#a{age = 12,name = "good_man",desc = <<2,3,4>>}
6> io:format("Data Dump:~p",[T2]).
Data Dump:{a,12,"good_man",<<2,3,4>>}ok
7> io:format(io_lib_pretty:print(T, fun(a, 3) -> [age,name,desc] end)).
#a{age = 12,name = "good_man",desc = <<2,3,4>>}ok
8> 
复制代码

 

2012-12-19 21:55 补记

 今天群里面有人问到:一个进程被压了很多消息,处理速度变慢,开启了SMP更慢,何解?

 这个和timer的情况很类似:

Creating timers using erlang:send_after/3 and erlang:start_timer/3 is much more efficient than using the timers provided by the timer module. The timer module uses a separate process to manage the timers, and that process can easily become overloaded if many processes create and cancel timers frequently (especially when using the SMP emulator).

http://www.erlang.org/doc/efficiency_guide/commoncaveats.html#id62547

 

 

 

最后小图一张:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值