KarrLab/de_sim

View on GitHub
de_sim/examples/jupyter_examples/4. DE-Sim performance test.ipynb

Summary

Maintainability
Test Coverage
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- :Author: Arthur Goldberg <Arthur.Goldberg@mssm.edu> -->\n",
    "<!-- :Date: 2020-07-13 -->\n",
    "<!-- :Copyright: 2020, Karr Lab -->\n",
    "<!-- :License: MIT -->\n",
    "\n",
    "# DE-Sim performance test\n",
    "\n",
    "This notebook conducts a performance of DE-Sim simulating a model of a cyclic messaging network over a range of network sizes.\n",
    "\n",
    "The cyclic messaging network model consists of a ring of simulation objects. Each simulation object executes an event at every time unit and schedules an event for the next object in the ring 1 time unit in the future.\n",
    "\n",
    "## Installation\n",
    "Use `pip` to install `de_sim`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Performance test of cyclical messaging network: end simulation time: 100\n",
      "\n",
      "#sim. obs     # events      run-time (s)  events/s\n",
      "        4         400             0.027      14797\n",
      "       16        1600             0.137      11673\n",
      "       64        6400             0.431      14837\n",
      "      256       25600             2.226      11499\n",
      "     1024      102400             8.927      11471\n",
      "     4096      409600            34.674      11813\n"
     ]
    }
   ],
   "source": [
    "# DE-Sim performance test\n",
    "import time\n",
    "\n",
    "import de_sim\n",
    "\n",
    "\n",
    "class InitMsg(de_sim.EventMessage):\n",
    "    'An InitMsg message'\n",
    "\n",
    "def obj_name(i):\n",
    "    return f'sim_obj_{i}'\n",
    "\n",
    "\n",
    "class CyclicalMessagesSimulationObject(de_sim.SimulationObject):\n",
    "    \"\"\" Send events around a cycle of objects \"\"\"\n",
    "\n",
    "    def __init__(self, name, obj_num, cycle_size):\n",
    "        super().__init__(name)\n",
    "        self.obj_num = obj_num\n",
    "        self.cycle_size = cycle_size\n",
    "\n",
    "    def next_obj(self):\n",
    "        next = (self.obj_num + 1) % self.cycle_size\n",
    "        return self.simulator.simulation_objects[obj_name(next)]\n",
    "\n",
    "    def send_next_event(self):\n",
    "        # send event to next CyclicalMessagesSimulationObject\n",
    "        self.send_event(1, self.next_obj(), InitMsg())\n",
    "\n",
    "    def init_before_run(self):\n",
    "        self.send_next_event()\n",
    "\n",
    "    def handle_event(self, event):\n",
    "        self.send_next_event()\n",
    "\n",
    "    event_handlers = [(InitMsg, 'handle_event')]\n",
    "\n",
    "    # register the message types sent\n",
    "    messages_sent = (InitMsg, )\n",
    "\n",
    "\n",
    "def make_cyclical_messaging_network_sim(simulator, num_objs):\n",
    "    # make a simulation with cyclical messaging network\n",
    "    sim_objects = [CyclicalMessagesSimulationObject(obj_name(i), i, num_objs)\n",
    "                   for i in range(num_objs)]\n",
    "    simulator.add_objects(sim_objects)\n",
    "\n",
    "def prep_simulation(simulator, num_sim_objs):\n",
    "    simulator.reset()\n",
    "    make_cyclical_messaging_network_sim(simulator, num_sim_objs)\n",
    "    simulator.initialize()\n",
    "\n",
    "def test_performance():\n",
    "    simulator = de_sim.Simulator()\n",
    "    end_sim_time = 100\n",
    "    num_sim_objs = 4\n",
    "    max_num_profile_objects = 300\n",
    "    max_num_sim_objs = 5000\n",
    "    print()\n",
    "    print(f\"Performance test of cyclical messaging network: \"\n",
    "          f\"end simulation time: {end_sim_time}\")\n",
    "    unprofiled_perf = [\"\\n#sim. obs\\t# events\\trun-time (s)\\tevents/s\".expandtabs(14)]\n",
    "\n",
    "    while num_sim_objs < max_num_sim_objs:\n",
    "\n",
    "        # measure execution time\n",
    "        prep_simulation(simulator, num_sim_objs)\n",
    "        start_time = time.process_time()\n",
    "        num_events = simulator.simulate(end_sim_time).num_events\n",
    "        run_time = time.process_time() - start_time\n",
    "        unprofiled_perf.append(f\"{num_sim_objs:>9}\\t{num_events:>7}\\t\"\n",
    "                               f\"{run_time:11.3f}\\t{num_events / run_time:8.0f}\".expandtabs(14))\n",
    "        num_sim_objs *= 4\n",
    "\n",
    "    print(\"\\n\".join(unprofiled_perf))\n",
    "\n",
    "test_performance()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each simulation run executes for 100 time units.\n",
    "The number of simulation objects in the ring is given by `#sim. obs`, and the number of simulation events executed by `# events`."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}