Commit b0f79b3a authored by neverlord's avatar neverlord

benchmark update

parent fcb7d04f
import scala.actors.Actor
import scala.actors.Actor._
import akka.actor.Actor.actorOf
import scala.annotation.tailrec
case object Msg
object global {
val latch = new java.util.concurrent.CountDownLatch(1)
}
class ThreadedReceiver(n: Long) extends Actor {
def act() {
var i: Long = 0
while (i < n)
receive {
case Msg => i += 1
}
Console println "received " + i + " messages"
}
}
class ThreadlessReceiver(n: Long) extends Actor {
def rcv(rest: Long) {
react {
case Msg => if (rest > 0) rcv(rest-1);
}
}
def act() {
rcv(n);
}
}
class AkkaReceiver(n: Long) extends akka.actor.Actor {
var received: Long = 0
def receive = {
case Msg =>
received += 1
if (received == n) {
global.latch.countDown
self.exit
}
}
}
object MailboxPerformance {
def usage() {
Console println "usage: (threaded|threadless|akka) (num_threads) (msgs_per_thread)"
}
def cr_threads(receiver: { def !(msg:Any): Unit }, threads: Int, msgs: Int) {
for (i <- 0 until threads)
(new java.lang.Thread {
override def run() {
for (j <- 0 until msgs)
receiver ! Msg
}
}).start
}
def main(args: Array[String]) = {
if (args.size != 3) {
usage
throw new IllegalArgumentException("")
}
val threads = args(1).toInt
val msgs = args(2).toInt
if (args(0) == "threaded") {
cr_threads((new ThreadedReceiver(threads*msgs)).start, threads, msgs)
}
else if (args(0) == "threadless") {
cr_threads((new ThreadlessReceiver(threads*msgs)).start, threads, msgs)
}
else if (args(0) == "akka") {
val a = actorOf(new AkkaReceiver(threads*msgs)).start
// akka actors define operator! with implicit argument
cr_threads(new AnyRef { def !(what: Any) { a ! what } }, threads, msgs)
global.latch.await
}
else usage
}
}
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011, 2012 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "cppa/cppa.hpp"
#include "cppa/fsm_actor.hpp"
using std::cout;
using std::cerr;
using std::endl;
using std::int64_t;
using namespace cppa;
struct fsm_receiver : fsm_actor<fsm_receiver>
{
int64_t m_value;
behavior init_state;
fsm_receiver(int64_t max) : m_value(0)
{
init_state =
(
on(atom("msg")) >> [=]()
{
++m_value;
if (m_value == max)
{
quit(exit_reason::normal);
}
}
);
}
};
void receiver(int64_t max)
{
int64_t value;
receive_loop
(
on(atom("msg")) >> [&]()
{
++value;
if (value == max)
{
std::cout << "received " << value << " messages" << endl;
quit(exit_reason::normal);
}
}
);
}
void sender(actor_ptr whom, int64_t count)
{
any_tuple msg = make_tuple(atom("msg"));
for (int64_t i = 0; i < count; ++i)
{
whom->enqueue(nullptr, msg);
}
}
void usage()
{
cout << "usage: mailbox_performance "
"(stacked|event-based) (sending threads) (msg per thread)" << endl
<< endl;
}
int main(int argc, char** argv)
{
if (argc == 4)
{
char* endptr = nullptr;
int64_t num_sender = static_cast<int64_t>(strtol(argv[2], &endptr, 10));
if (endptr == nullptr || *endptr != '\0')
{
cerr << "\"" << argv[2] << "\" is not an integer" << endl;
usage();
return 1;
}
int64_t num_msgs = static_cast<int64_t>(strtol(argv[3], &endptr, 10));
if (endptr == nullptr || *endptr != '\0')
{
cerr << "\"" << argv[3] << "\" is not an integer" << endl;
usage();
return 1;
}
actor_ptr testee;
if (strcmp(argv[1], "stacked") == 0)
{
testee = spawn(receiver, num_sender * num_msgs);
}
else if (strcmp(argv[1], "event-based") == 0)
{
testee = spawn(new fsm_receiver(num_sender * num_msgs));
}
else
{
usage();
return 1;
}
for (int64_t i = 0; i < num_sender; ++i)
{
spawn<detached>(sender, testee, num_msgs);
}
await_all_others_done();
}
else
{
usage();
return 1;
}
return 0;
}
-module(mailbox_performance).
-export([start/1,sender/2]).
sender(_, 0) ->
0;
sender(Pid, Num) ->
Pid ! {msg},
sender(Pid, Num-1).
start_senders(_, _, 0) ->
0;
start_senders(Pid, Arg, Num) ->
spawn(mailbox_performance, sender, [Pid, Arg]),
start_senders(Pid, Arg, Num-1).
receive_messages(0) ->
0;
receive_messages(Num) ->
receive
{msg} -> receive_messages(Num-1)
end.
start(Args) ->
[H1|T] = Args,
NumSender = list_to_integer(atom_to_list(H1)),
[H2|_] = T,
NumMsgs = list_to_integer(atom_to_list(H2)),
start_senders(self(), NumMsgs, NumSender),
receive_messages(NumMsgs * NumSender).
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment