Skip to content

Commit

Permalink
🧐 (analysis) add energy efficiency by service by user stage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias-Pe committed Jul 13, 2024
1 parent cbd5071 commit ab551bf
Showing 1 changed file with 110 additions and 20 deletions.
130 changes: 110 additions & 20 deletions loadtesting/notebooks/energy_efficiency.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
"service_metrics_data_file = basedir+\"service_metrics.csv\"\n",
"\n",
"stages = [\n",
" {\"users\": 100, \"spawn_rate\": 2},\n",
" {\"users\": 200, \"spawn_rate\": 20},\n",
" {\"users\": 300, \"spawn_rate\": 20},\n",
" {\"users\": 500, \"spawn_rate\": 20},\n",
" {\"users\": 800, \"spawn_rate\": 20},\n",
" {\"users\": 1300, \"spawn_rate\": 50},\n",
" {\"users\": 2100, \"spawn_rate\": 50}\n",
" {\"users\": 100},\n",
" {\"users\": 200},\n",
" {\"users\": 300},\n",
" {\"users\": 500},\n",
" {\"users\": 800},\n",
" {\"users\": 1300}\n",
"]\n",
"\n",
"valid_users = set()\n",
Expand Down Expand Up @@ -521,28 +520,21 @@
" total_energy = calculate_service_energy_consumption(kepler_df, service, start_time, end_time)\n",
" \n",
" # Calculate successful request count from the service_work_metrics_df\n",
" if service != \"gateway\":\n",
" service_work_metrics_filtered = service_work_metrics_df[(service_work_metrics_df.index >= start_time) & (service_work_metrics_df.index <= end_time)]\n",
" if service in set(map(str.lower,service_work_metrics_filtered.columns)):\n",
" total_requests = service_work_metrics_filtered[service.upper()].max() - service_work_metrics_filtered[service.upper()].min()\n",
" else:\n",
" total_requests = 0\n",
" service_work_metrics_filtered = service_work_metrics_df[(service_work_metrics_df.index >= start_time) & (service_work_metrics_df.index <= end_time)]\n",
" if service in set(map(str.lower,service_work_metrics_filtered.columns)):\n",
" total_requests = service_work_metrics_filtered[service.upper()].max() - service_work_metrics_filtered[service.upper()].min()\n",
" else:\n",
" total_requests = locust_requests_count_df['Request Count'].sum()\n",
" total_requests = 0\n",
" \n",
" failed_requests = 0\n",
" if service == \"gateway\":\n",
" failed_requests = locust_requests_count_df['Failure Count'].sum()\n",
" \n",
" successful_requests = total_requests - failed_requests\n",
" efficiency = successful_requests / total_energy if total_energy != 0 else 0\n",
" \n",
" metrics_results[service] = {\n",
" 'efficiency': efficiency,\n",
" 'total_energy': total_energy,\n",
" 'successful_requests': successful_requests,\n",
" 'failed_requests': failed_requests,\n",
" 'total_requests': total_requests,\n",
" 'successful_requests': successful_requests\n",
" }\n",
" \n",
" return pd.DataFrame(metrics_results).transpose()\n",
Expand Down Expand Up @@ -616,10 +608,108 @@
{
"metadata": {},
"cell_type": "code",
"source": "",
"source": [
"# Function to calculate energy consumption for each service during stages\n",
"def calculate_combined_service_metrics_by_stage(service_list, stages, locust_df, kepler_df, service_work_metrics_df):\n",
" metrics_results_by_stage = {stage['users']: {} for stage in stages}\n",
"\n",
" for stage in stages:\n",
" stage_users = stage['users']\n",
" stage_df = locust_df[locust_df['User Count'] == stage_users]\n",
"\n",
" # Get the min and max index (time) for the current stage\n",
" if stage_df.empty:\n",
" print(\"No data available for stage \" + stage)\n",
" for service in service_list:\n",
" metrics_results_by_stage[stage_users][service] = {\n",
" 'efficiency': 0,\n",
" 'total_energy': 0,\n",
" 'successful_requests': 0,\n",
" }\n",
" continue\n",
"\n",
" stage_start_time, stage_end_time = stage_df.index.min(), stage_df.index.max()\n",
" stage_start_time = stage_start_time.tz_convert(\"Europe/Berlin\").tz_localize(None)\n",
" stage_end_time = stage_end_time.tz_convert(\"Europe/Berlin\").tz_localize(None)\n",
"\n",
" stage_work_metrics_df = service_work_metrics_df[\n",
" (service_work_metrics_df.index >= stage_start_time) & (service_work_metrics_df.index <= stage_end_time)]\n",
"\n",
" for service in service_list:\n",
" if service.endswith('-db'):\n",
" continue # Skip DB services\n",
"\n",
" total_energy_service = calculate_service_energy_consumption(kepler_df, service, stage_start_time,\n",
" stage_end_time)\n",
" if f\"{service}-db\" in service_list:\n",
" total_energy_service += calculate_service_energy_consumption(kepler_df, f\"{service}-db\", stage_start_time, stage_end_time)\n",
"\n",
" if service.upper() in set(map(str.upper, stage_work_metrics_df.columns)):\n",
" total_requests = stage_work_metrics_df[service.upper()].max() - stage_work_metrics_df[\n",
" service.upper()].min()\n",
" else:\n",
" total_requests = 0\n",
"\n",
" successful_requests = total_requests\n",
" efficiency = successful_requests / total_energy_service if total_energy_service != 0 else 0\n",
"\n",
" metrics_results_by_stage[stage_users][service] = {\n",
" 'efficiency': efficiency,\n",
" 'total_energy': total_energy_service,\n",
" 'successful_requests': successful_requests,\n",
" }\n",
"\n",
" return metrics_results_by_stage\n",
"\n",
"\n",
"service_metrics_by_stage = calculate_combined_service_metrics_by_stage(service_names, stages, locust_df, kepler_df,\n",
" service_work_metrics_df)\n",
"service_metrics_by_stage\n",
"\n",
"# Convert dictionary to DataFrame for easier manipulation\n",
"metrics_df_list = []\n",
"\n",
"for user_stage, service_metrics in service_metrics_by_stage.items():\n",
" df = pd.DataFrame(service_metrics).transpose()\n",
" df['user_stage'] = user_stage\n",
" metrics_df_list.append(df)\n",
"\n",
"staged_service_metrics_df = pd.concat(metrics_df_list)\n",
"\n",
"# Filter out services with zero efficiency\n",
"staged_service_metrics_df = staged_service_metrics_df[staged_service_metrics_df['efficiency'] > 0]\n",
"\n",
"# Plot Efficiency of each combined service over stages\n",
"\n",
"fig, ax = plt.subplots(figsize=(16, 8))\n",
"\n",
"for service in staged_service_metrics_df.index.unique():\n",
" # Filter out rows for this service\n",
" filtered_df = staged_service_metrics_df.loc[staged_service_metrics_df.index == service]\n",
" ax.plot(filtered_df['user_stage'], filtered_df['efficiency'], label=service, marker='o', markersize=4)\n",
"\n",
"ax.set_xlabel('User Stage (User Count)')\n",
"ax.set_ylabel('Energy Efficiency (Success Requests / Joules)')\n",
"ax.set_title('Energy Efficiency per Combined Service Over User Stages (Including DB Consumption)')\n",
"ax.set_xticks([stage['users'] for stage in stages])\n",
"ax.set_xticklabels([stage['users'] for stage in stages])\n",
"ax.legend(loc='upper left')\n",
"ax.grid(True)\n",
"\n",
"plt.xticks(rotation=45)\n",
"plt.show()"
],
"id": "f3d9ace13938a4a1",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"id": "8f731eaa04a6176b",
"outputs": [],
"execution_count": null
}
],
"metadata": {
Expand Down

0 comments on commit ab551bf

Please sign in to comment.