Skip to content

Commit

Permalink
bug: fix profiler json output
Browse files Browse the repository at this point in the history
  • Loading branch information
arozx committed Jan 23, 2025
1 parent e99b442 commit ddb0312
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 131 deletions.
123 changes: 61 additions & 62 deletions json-viewer/visualiser.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Import necessary libraries\n",
"import json\n",
"import plotly.graph_objects as go\n",
"import plotly.express as px\n",
"from plotly.subplots import make_subplots\n",
"import pandas as pd\n",
"\n",
"hover_label_dict =dict(\n",
Expand All @@ -31,39 +28,49 @@
")\n",
"\n",
"# Load both JSON files\n",
"with open('profile_results.json', 'r') as file:\n",
"with open('../profile_results.json', 'r') as file:\n",
" profile_data = json.load(file)\n",
"\n",
"with open('profiler_n_frames.json', 'r') as file:\n",
"with open('../profiler_n_frames.json', 'r') as file:\n",
" frame_data = json.load(file)\n",
"\n",
"# Create DataFrame for frame data\n",
"frames_df = pd.DataFrame(frame_data['frames'])\n",
"\n",
"# Create DataFrame for profile data\n",
"profiles_df = pd.DataFrame(profile_data['profiles'])\n",
"\n",
"# Visualization 1: Profile Overview\n",
"fig1 = go.Figure()\n",
"\n",
"# Add bars for min, avg, max times\n",
"for metric in ['minMs', 'averageMs', 'maxMs']:\n",
" fig1.add_trace(go.Bar(\n",
" name=metric.replace('Ms', ' Time'),\n",
" x=profiles_df['name'],\n",
" y=profiles_df[metric],\n",
" text=profiles_df[metric].round(2),\n",
" textposition='auto',\n",
" ))\n",
"\n",
"fig1.update_layout(\n",
" title='Profile Timing Overview',\n",
" barmode='group',\n",
" xaxis_title='Function',\n",
" yaxis_title='Time (ms)',\n",
" yaxis_type='log', # Use log scale for better visibility\n",
" hoverlabel=hover_label_dict\n",
")\n",
"# Check if we have profile data\n",
"has_profile_data = len(profile_data['profiles']) > 0\n",
"\n",
"if has_profile_data:\n",
" # Create DataFrame for profile data only if we have profiles\n",
" profiles_df = pd.DataFrame(profile_data['profiles'])\n",
" \n",
" # Define custom sorting order\n",
" function_order = ['BeginScene', 'Present', 'EndScene']\n",
" profiles_df['sort_order'] = profiles_df['name'].map({name: i for i, name in enumerate(function_order)})\n",
" profiles_df = profiles_df.sort_values('sort_order').drop('sort_order', axis=1)\n",
" \n",
" # Visualization 1: Profile Overview\n",
" fig1 = go.Figure()\n",
" \n",
" for metric in ['minMs', 'averageMs', 'maxMs']:\n",
" if metric in profiles_df.columns:\n",
" fig1.add_trace(go.Bar(\n",
" name=metric.replace('Ms', ' Time'),\n",
" x=profiles_df['name'] if 'name' in profiles_df.columns else [],\n",
" y=profiles_df[metric],\n",
" text=profiles_df[metric].round(2),\n",
" textposition='auto',\n",
" ))\n",
" \n",
" fig1.update_layout(\n",
" title='Profile Timing Overview',\n",
" barmode='group',\n",
" xaxis_title='Function',\n",
" yaxis_title='Time (ms)',\n",
" yaxis_type='log',\n",
" hoverlabel=hover_label_dict\n",
" )\n",
" fig1.show()\n",
"\n",
"# Visualization 2: Frame Times Distribution\n",
"fig2 = go.Figure()\n",
Expand Down Expand Up @@ -117,29 +124,36 @@
" title='FPS Statistics',\n",
" xaxis_title='Metric',\n",
" yaxis_title='FPS',\n",
" yaxis_type='log', # Use log scale since highest FPS can be very large\n",
" yaxis_type='log',\n",
" hoverlabel=hover_label_dict\n",
")\n",
"\n",
"# Visualization 4: Box Plot of Frame Times\n",
"fig4 = go.Figure()\n",
"\n",
"# Add box plot for frame times\n",
"# Calculate all quartiles including min (q0) and max (q4)\n",
"quartiles = frames_df['frame_time_ms'].quantile([0, 0.25, 0.5, 0.75, 1])\n",
"\n",
"fig4.add_trace(go.Box(\n",
" y=frames_df['frame_time_ms'],\n",
" name='Frame Times',\n",
" boxpoints='all', # show all points\n",
" jitter=0.3, # spread points horizontally\n",
" pointpos=-1.8, # offset points\n",
" boxmean=True, # show mean\n",
" boxpoints='all',\n",
" jitter=0.3,\n",
" pointpos=-1.8,\n",
" boxmean=True,\n",
" marker=dict(color='lightblue'),\n",
" line=dict(color='blue'),\n",
" hovertemplate='Time: %{y:.2f}ms<extra></extra>'\n",
"))\n",
"\n",
"# Add markers for quartiles\n",
"quartiles = frames_df['frame_time_ms'].quantile([0.25, 0.5, 0.75])\n",
"for q, (label, color) in enumerate([('25th', 'purple'), ('Median', 'red'), ('75th', 'orange')]):\n",
"# Add lines for all quartiles including min and max\n",
"for q, (label, color) in enumerate([\n",
" ('Min (Q0)', 'gray'), \n",
" ('25th', 'purple'), \n",
" ('Median', 'red'), \n",
" ('75th', 'orange'),\n",
" ('Max (Q4)', 'darkgray')\n",
"]):\n",
" fig4.add_hline(\n",
" y=quartiles.iloc[q],\n",
" line_dash=\"dash\",\n",
Expand All @@ -148,43 +162,28 @@
" annotation_position=\"right\"\n",
" )\n",
"\n",
"# Update layout with adjusted y-axis range for better distribution\n",
"fig4.update_layout(\n",
" title='Frame Time Distribution',\n",
" yaxis_title='Frame Time (ms)',\n",
" yaxis=dict(\n",
" range=[\n",
" quartiles.iloc[1] - (quartiles.iloc[4] - quartiles.iloc[1]) * 0.1, # Q1 - 10% of range from Q1 to Q4\n",
" quartiles.iloc[4] + (quartiles.iloc[4] - quartiles.iloc[1]) * 0.1 # Q4 + 10% of range from Q1 to Q4\n",
" ]\n",
" ),\n",
" showlegend=False,\n",
" hovermode='y',\n",
" hoverlabel=hover_label_dict\n",
")\n",
"\n",
"# Visualization 5: Call Count Analysis\n",
"fig5 = px.bar(\n",
" profiles_df,\n",
" x='name',\n",
" y='calls',\n",
" title='Function Call Counts',\n",
" labels={'name': 'Function', 'calls': 'Number of Calls'},\n",
" text='calls'\n",
")\n",
"\n",
"fig5.update_traces(\n",
" textposition='auto',\n",
" marker_line_width=1.5,\n",
" marker_line_color='black'\n",
")\n",
"\n",
"fig5.update_layout(\n",
" hoverlabel=hover_label_dict\n",
")\n",
"\n",
"# Display figures\n",
"# Display session info and remaining figures\n",
"print(f\"Session: {profile_data['session']}\")\n",
"print(f\"Timestamp: {profile_data['timestamp']}\\n\")\n",
"\n",
"fig1.show()\n",
"fig2.show()\n",
"fig3.show()\n",
"fig4.show()\n",
"fig5.show()\n"
"fig4.show()\n"
]
}
],
Expand Down
Loading

0 comments on commit ddb0312

Please sign in to comment.