Skip to content

Commit

Permalink
feat: add database connections graph
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent a09ba8f commit e1dbb19
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 23 deletions.
4 changes: 3 additions & 1 deletion test/performance/charts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import 'chartjs-to-image';
import {exportLatencyGraph, exportTPSGraph} from "./src/graphs";
import {exportDatabaseStats, exportLatencyGraph, exportTPSGraph} from "./src/graphs";

const main = async () => {
let buffer = fs.readFileSync('../report/report.json', 'utf-8');
Expand All @@ -9,6 +9,8 @@ const main = async () => {
output: 'tps.png',
}, reports);

await exportDatabaseStats('database_connections.png', reports);

const ps: (keyof MetricsTime)[] = ['P99', 'P95', 'P75', 'Avg']
for (let p of ps) {
await exportLatencyGraph({
Expand Down
52 changes: 35 additions & 17 deletions test/performance/charts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/performance/charts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"@types/node": "^22.7.4",
"@types/promise-fs": "^2.1.5",
"chart.js": "^4.4.4",
"chart.js-image": "^6.1.3",
"chartjs-plugin-annotation": "^3.0.1",
"chartjs-to-image": "^1.2.2",
"install": "^0.13.0",
"npm": "^10.9.0"
Expand Down
98 changes: 93 additions & 5 deletions test/performance/charts/src/graphs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {NAMED_COLORS} from "./colors";
import ChartJsImage from "chartjs-to-image";
import {ChartConfiguration, ChartDataset, Chart} from "chart.js";
import annotationPlugin from 'chartjs-plugin-annotation';

Chart.register(annotationPlugin);

export const exportTPSGraph = async (configuration: {output: string}, result: BenchmarkResult) => {

Expand All @@ -13,15 +17,15 @@ export const exportTPSGraph = async (configuration: {output: string}, result: Be
throw new Error("no data");
}

const datasets = scripts.map(((script, index) => {
const datasets = scripts.map(((script, index): ChartDataset => {
return {
label: script,
data: result[script].map(r => r.TPS),
backgroundColor: NAMED_COLORS[index % scripts.length],
}
}));

const config = {
const config: ChartConfiguration = {
type: 'bar',
data: {
labels: reportsForAnyScript
Expand Down Expand Up @@ -66,15 +70,15 @@ export const exportLatencyGraph = async (configuration: {output: string}, key: k
throw new Error("no data");
}

const datasets = scripts.map(((script, index) => {
const datasets = scripts.map(((script, index): ChartDataset => {
return {
label: script,
data: result[script].map(r => r.Metrics.Time[key].substring(0, r.Metrics.Time[key].length-2)),
data: result[script].map(r => parseFloat(r.Metrics.Time[key].substring(0, r.Metrics.Time[key].length-2))),
backgroundColor: NAMED_COLORS[index % scripts.length],
}
}));

const config = {
const config: ChartConfiguration = {
type: 'bar',
data: {
labels: reportsForAnyScript
Expand Down Expand Up @@ -105,4 +109,88 @@ export const exportLatencyGraph = async (configuration: {output: string}, key: k
const chart = new ChartJsImage();
chart.setConfig(config);
await chart.toFile(configuration.output);
}

export const exportDatabaseStats = async (
output: string,
result: BenchmarkResult,
) => {

const scope = 'github.com/uptrace/opentelemetry-go-extra/otelsql';

const scripts = [];
for (let script in result) {
scripts.push(script);
}

const reportsForAnyScript = result[scripts[0]];
if (!reportsForAnyScript) {
throw new Error("no data");
}

const datasets = scripts.map(((script, index): ChartDataset => {
return {
label: script,
data: result[script].map(r => r.InternalMetrics.ScopeMetrics
.find(scopeMetric => scopeMetric.Scope.Name == scope)!
.Metrics
.find(metric => metric.Name == 'go.sql.connections_open')!
.Data
.DataPoints[0]
.Value
),
backgroundColor: NAMED_COLORS[index % scripts.length],
}
}));

const maxConnection = reportsForAnyScript[0].InternalMetrics.ScopeMetrics
.find(scopeMetric => scopeMetric.Scope.Name == scope)!
.Metrics
.find(metric => metric.Name == 'go.sql.connections_max_open')!
.Data
.DataPoints[0]
.Value

const config: ChartConfiguration = {
type: 'bar',
data: {
labels: reportsForAnyScript.map(r => r.Configuration.Name),
datasets: datasets
},
options: {
plugins: {
title: {
display: true,
text: 'Database connections'
},
annotation: {
annotations: {
line1: {
type: 'line',
yMin: maxConnection,
yMax: maxConnection,
borderColor: 'rgb(255, 99, 132)',
borderWidth: 2,
}
}
}
},
interaction: {
intersect: false,
},
scales: {
x: {
stacked: false,
},
y: {
stacked: false
}
}
}
};

const chart = new ChartJsImage();
chart.setConfig(config);
chart.setChartJsVersion('4')
await chart.toFile(output);
}
39 changes: 39 additions & 0 deletions test/performance/charts/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,52 @@ interface Configuration {
FeatureSet: Map<string, string>
}

interface DataPoint {
Attributes: string[]
Bounds: number[]
BucketCounts: number[]
Count: number
Max: number
Min: number
StartTime: string
Sum: number
Time: string
Value: number
}

interface OtelMetric {
Data: {
DataPoints: DataPoint[]
Temporality: string
},
Description: string
Name: string
Unit: string
}

interface Scope {
Name: string
SchemaURL: string
Version: string
}

interface ScopeMetric {
Metrics: OtelMetric[]
Scope: Scope
}

interface InternalMetrics {
ScopeMetrics: ScopeMetric[]
}

interface Report {
Start: string,
End: string,
Metrics: Metrics,
Scenario: string,
Configuration: Configuration,
TPS: number
InternalMetrics: InternalMetrics
}

interface BenchmarkResult {
Expand Down

0 comments on commit e1dbb19

Please sign in to comment.