{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "wound-burns",
   "metadata": {},
   "outputs": [],
   "source": [
    "from plotnine import *\n",
    "import plotnine as plotnine\n",
    "import pandas as pd\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "id": "separated-foundation",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.concat([\n",
    "    pd.read_csv('block_linux_x86_64.csv'),\n",
    "    pd.read_csv('block_bufchan_linux_x86_64.csv'),\n",
    "])\n",
    "df['workload'] = [\n",
    "    r['workload'] if r['workload'] != 'chan' else 'chan(cap={})'.format(r['bufsize'])\n",
    "    for _, r in df.iterrows()\n",
    "]\n",
    "df['blockprofilerate'] = [('10$^{'+str(int(math.log10(x)))+'}$') if x > 1 else str(x) for x in df['blockprofilerate']]\n",
    "\n",
    "compare = df.groupby(['blockprofilerate', 'workload']).median()\n",
    "# calculate overhead compared to blockprofilerate=0 for same workload\n",
    "compare['overhead'] = [\n",
    "    (x['ms'] / compare.loc['0'].loc[x['workload']]['ms'] - 1) * 100\n",
    "    for _, x in compare.reset_index().iterrows()\n",
    "]\n",
    "compare = compare.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "id": "viral-baking",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 138176 characters]\n",
      "text/plain": [
       "<Figure size 1200x1000 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<ggplot: (335455735)>"
      ]
     },
     "execution_count": 201,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plotnine.options.figure_size = (12, 10)\n",
    "\n",
    "def plot_title(df):\n",
    "    return (\n",
    "        'median runtime.SetBlockProfileRate() overhead on artificial pathological workloads\\n\\n' +\n",
    "        'os={} cpu={} go={} goroutines={} stackdepth={} ops={}'.format(\n",
    "            # TODO(fg) don't hard code environment\n",
    "            'linux (docker for mac)',\n",
    "            'i7-9750H',\n",
    "            '1.15.7',\n",
    "            val_range(df, 'goroutines'),\n",
    "            val_range(df, 'depth'),\n",
    "#             val_range(df, 'bufsize'),\n",
    "            val_range(df, 'ops'),\n",
    "        )\n",
    "    )\n",
    "\n",
    "def val_range(df, col):\n",
    "    min = int(df[col].min())\n",
    "    max = int(df[col].max())\n",
    "    return '{}-{}'.format(min, max) if min != max else min\n",
    "\n",
    "(\n",
    "ggplot(df)\n",
    "+ aes(x='blockprofilerate',y='ms',ymin=0)\n",
    "+ ggtitle(plot_title(compare))\n",
    "+ ylab('time (ms)')\n",
    "+ xlab('blockprofilerate (ns)')\n",
    "+ geom_label(\n",
    "    aes(label='overhead',color='overhead'),\n",
    "    data=compare,\n",
    "    y=0,\n",
    "    size=9,\n",
    "    format_string='{:.1f}%'\n",
    ")\n",
    "+ scale_color_gradient(low=\"green\",high=\"red\",name='overhead (%)')\n",
    "+ geom_boxplot(outlier_shape='')\n",
    "+ facet_wrap('workload', scales='free', ncol=2)\n",
    "+ theme(subplots_adjust={'wspace': 0.05, 'hspace': 0.2})\n",
    "+ geom_point(color='blue',shape='o',alpha=0.3, size=1)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "orange-degree",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
