{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Experiment Data Analysis\n",
        "\n",
        "This notebook analyzes the experiment data collected from AWS and IBM cloud environments comparing CAS and Karpenter autoscaling."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "source": [
        "import json\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "from pathlib import Path\n",
        "import numpy as np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "source": [
        "# Set up paths\n",
        "BASE_PATH = Path('.')\n",
        "AWS_PATH = BASE_PATH / 'aws'\n",
        "IBM_PATH = BASE_PATH / 'ibm'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "source": [
        "def load_json_file(path):\n",
        "    \"\"\"Load and parse a JSON file\"\"\"\n",
        "    with open(path) as f:\n",
        "        return json.load(f)\n",
        "\n",
        "def parse_latency_data(path):\n",
        "    \"\"\"Parse node latency measurement data\"\"\"\n",
        "    data = load_json_file(path)\n",
        "    return pd.DataFrame(data)\n",
        "\n",
        "def parse_billing_data(path):\n",
        "    \"\"\"Parse AWS billing metrics data\"\"\"\n",
        "    data = load_json_file(path)\n",
        "    return pd.DataFrame(data)\n",
        "\n",
        "def parse_log_file(path):\n",
        "    \"\"\"Parse kube-burner log file\"\"\"\n",
        "    with open(path) as f:\n",
        "        return [line.strip() for line in f if line.strip()]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "source": [
        "# Load AWS homogeneous CAS data\n",
        "aws_homo_cas_path = AWS_PATH / 'homogenous/cas/3c29adda-1a6f-4548-a3a6-0faad0bf8013-eks-cas'\n",
        "aws_cas_latency = parse_latency_data(aws_homo_cas_path / 'nodeLatencyMeasurement-api-intensive.json')\n",
        "aws_cas_latency_quantiles = parse_latency_data(aws_homo_cas_path / 'nodeLatencyQuantilesMeasurement-api-intensive.json')\n",
        "aws_cas_billing = parse_billing_data(aws_homo_cas_path / 'aws_billing_metrics_cas.json')\n",
        "aws_cas_logs = parse_log_file(aws_homo_cas_path / 'kube-burner-3c29adda-1a6f-4548-a3a6-0faad0bf8013-iks-cas.log')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "source": [
        "# Load AWS homogeneous Karpenter data\n",
        "aws_homo_karp_path = AWS_PATH / 'homogenous/karpenter/3c29adda-1a6f-4548-a3a6-0faad0bf8013-eks-karpenter'\n",
        "aws_karp_latency = parse_latency_data(aws_homo_karp_path / 'nodeLatencyMeasurement-api-intensive.json')\n",
        "aws_karp_latency_quantiles = parse_latency_data(aws_homo_karp_path / 'nodeLatencyQuantilesMeasurement-api-intensive.json')\n",
        "aws_karp_billing = parse_billing_data(aws_homo_karp_path / 'aws_billing_metrics_karpenter.json')\n",
        "aws_karp_logs = parse_log_file(aws_homo_karp_path / 'kube-burner-3c29adda-1a6f-4548-a3a6-0faad0bf8013-iks-karpenter.log')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Data Analysis\n",
        "\n",
        "### 1. Node Provisioning Latency Comparison"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "source": [
        "# Compare latency distributions\n",
        "plt.figure(figsize=(12, 6))\n",
        "sns.boxplot(data=[aws_cas_latency['latency'], aws_karp_latency['latency']], \n",
        "           labels=['CAS', 'Karpenter'])\n",
        "plt.title('Node Provisioning Latency Distribution')\n",
        "plt.ylabel('Latency (seconds)')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 2. Cost Analysis"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "source": [
        "# Compare billing metrics\n",
        "def plot_billing_comparison():\n",
        "    plt.figure(figsize=(12, 6))\n",
        "    \n",
        "    cas_costs = aws_cas_billing['cost'].values\n",
        "    karp_costs = aws_karp_billing['cost'].values\n",
        "    \n",
        "    plt.bar(['CAS', 'Karpenter'], [cas_costs.mean(), karp_costs.mean()])\n",
        "    plt.title('Average Cost Comparison')\n",
        "    plt.ylabel('Cost (USD)')\n",
        "    plt.show()\n",
        "\n",
        "plot_billing_comparison()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 3. Performance Quantiles Analysis"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "source": [
        "# Compare latency quantiles\n",
        "def plot_quantile_comparison():\n",
        "    plt.figure(figsize=(12, 6))\n",
        "    \n",
        "    quantiles = ['p50', 'p90', 'p95', 'p99']\n",
        "    cas_values = [aws_cas_latency_quantiles[q].mean() for q in quantiles]\n",
        "    karp_values = [aws_karp_latency_quantiles[q].mean() for q in quantiles]\n",
        "    \n",
        "    x = np.arange(len(quantiles))\n",
        "    width = 0.35\n",
        "    \n",
        "    plt.bar(x - width/2, cas_values, width, label='CAS')\n",
        "    plt.bar(x + width/2, karp_values, width, label='Karpenter')\n",
        "    \n",
        "    plt.xlabel('Percentile')\n",
        "    plt.ylabel('Latency (seconds)')\n",
        "    plt.title('Latency Quantiles Comparison')\n",
        "    plt.xticks(x, quantiles)\n",
        "    plt.legend()\n",
        "    plt.show()\n",
        "\n",
        "plot_quantile_comparison()"
      ]
    }
  ],
  "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.8.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}
