1+ /* *
2+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ * SPDX-License-Identifier: Apache-2.0.
4+ */
5+
6+ #pragma once
7+
8+ #include < aws/core/client/AWSClient.h>
9+ #include < aws/core/http/HttpRequest.h>
10+ #include < aws/core/monitoring/CoreMetrics.h>
11+ #include < aws/core/monitoring/MonitoringFactory.h>
12+ #include < aws/core/monitoring/MonitoringInterface.h>
13+ #include < aws/core/utils/DateTime.h>
14+ #include < aws/core/utils/json/JsonSerializer.h>
15+ #include < aws/core/utils/memory/AWSMemory.h>
16+ #include < aws/core/utils/memory/stl/AWSMap.h>
17+ #include < aws/core/utils/memory/stl/AWSSet.h>
18+ #include < aws/core/utils/memory/stl/AWSString.h>
19+ #include < aws/core/utils/memory/stl/AWSVector.h>
20+
21+ #include < cstdint>
22+ #include < memory>
23+ #include < variant>
24+
25+ namespace PerformanceTest {
26+ namespace Reporting {
27+
28+ /* *
29+ * Container for a single performance metric record that stores measurement data and associated metadata.
30+ */
31+ struct PerformanceMetricRecord {
32+ Aws::String name;
33+ Aws::String description;
34+ Aws::String unit;
35+ Aws::Utils::DateTime date;
36+ Aws::Vector<std::variant<int64_t , double >> measurements;
37+ Aws::Map<Aws::String, Aws::String> dimensions;
38+ };
39+
40+ /* *
41+ * An implementation of the MonitoringInterface that collects performance metrics
42+ * and reports them in a JSON format.
43+ */
44+ class JsonReportingMetrics : public Aws ::Monitoring::MonitoringInterface {
45+ public:
46+ /* *
47+ * Constructor that initializes the metrics collector with configuration parameters.
48+ * @param monitoredOperations Set of operations to monitor (empty means monitor all)
49+ * @param productId Product identifier (e.g., "cpp1")
50+ * @param sdkVersion SDK version string
51+ * @param commitId Git commit identifier
52+ * @param outputFilename Path to output file (e.g., "s3-perf-results.json")
53+ */
54+ JsonReportingMetrics (const Aws::Set<Aws::String>& monitoredOperations = Aws::Set<Aws::String>(), const Aws::String& productId = " unknown" ,
55+ const Aws::String& sdkVersion = " unknown" , const Aws::String& commitId = " unknown" ,
56+ const Aws::String& outputFilename = " performance-test-results.json" );
57+
58+ ~JsonReportingMetrics () override ;
59+
60+ /* *
61+ * Called when an AWS request is started. Returns context for tracking.
62+ * @param serviceName Name of the AWS service
63+ * @param requestName Name of the operation
64+ * @param request HTTP request object
65+ * @return Context pointer (always returns nullptr)
66+ */
67+ void * OnRequestStarted (const Aws::String& serviceName, const Aws::String& requestName,
68+ const std::shared_ptr<const Aws::Http::HttpRequest>& request) const override ;
69+
70+ /* *
71+ * Called when an AWS request succeeds. Records performance metrics.
72+ * @param serviceName Name of the AWS service
73+ * @param requestName Name of the operation
74+ * @param request HTTP request object
75+ * @param outcome HTTP response outcome
76+ * @param metrics Core metrics collection containing latency data
77+ * @param context Request context
78+ */
79+ void OnRequestSucceeded (const Aws::String& serviceName, const Aws::String& requestName,
80+ const std::shared_ptr<const Aws::Http::HttpRequest>& request, const Aws::Client::HttpResponseOutcome& outcome,
81+ const Aws::Monitoring::CoreMetricsCollection& metrics, void * context) const override ;
82+
83+ /* *
84+ * Called when an AWS request fails. Records performance metrics.
85+ * @param serviceName Name of the AWS service
86+ * @param requestName Name of the operation
87+ * @param request HTTP request object
88+ * @param outcome HTTP response outcome
89+ * @param metrics Core metrics collection containing latency data
90+ * @param context Request context
91+ */
92+ void OnRequestFailed (const Aws::String& serviceName, const Aws::String& requestName,
93+ const std::shared_ptr<const Aws::Http::HttpRequest>& request, const Aws::Client::HttpResponseOutcome& outcome,
94+ const Aws::Monitoring::CoreMetricsCollection& metrics, void * context) const override ;
95+
96+ /* *
97+ * Called when an AWS request is retried. No action taken.
98+ * @param serviceName Name of the AWS service
99+ * @param requestName Name of the operation
100+ * @param request HTTP request object
101+ * @param context Request context
102+ */
103+ void OnRequestRetry (const Aws::String& serviceName, const Aws::String& requestName,
104+ const std::shared_ptr<const Aws::Http::HttpRequest>& request, void * context) const override ;
105+
106+ /* *
107+ * Called when an AWS request finishes. No action taken.
108+ * @param serviceName Name of the AWS service
109+ * @param requestName Name of the operation
110+ * @param request HTTP request object
111+ * @param context Request context
112+ */
113+ void OnFinish (const Aws::String& serviceName, const Aws::String& requestName,
114+ const std::shared_ptr<const Aws::Http::HttpRequest>& request, void * context) const override ;
115+
116+ private:
117+ /* *
118+ * Helper method to process request metrics and store in context.
119+ * @param serviceName Name of the AWS service
120+ * @param requestName Name of the operation
121+ * @param request HTTP request object
122+ * @param metricsFromCore Core metrics collection containing latency data
123+ * @param context Request context
124+ */
125+ void StoreLatencyInContext (const Aws::String& serviceName, const Aws::String& requestName,
126+ const std::shared_ptr<const Aws::Http::HttpRequest>& request,
127+ const Aws::Monitoring::CoreMetricsCollection& metricsFromCore, void * context) const ;
128+
129+ /* *
130+ * Adds a performance record with a specified duration.
131+ * @param serviceName Name of the AWS service
132+ * @param requestName Name of the operation
133+ * @param request HTTP request object
134+ * @param durationMs Duration of the request in milliseconds
135+ */
136+ void AddPerformanceRecord (const Aws::String& serviceName, const Aws::String& requestName,
137+ const std::shared_ptr<const Aws::Http::HttpRequest>& request,
138+ const std::variant<int64_t , double >& durationMs) const ;
139+
140+ /* *
141+ * Outputs aggregated performance metrics to JSON file.
142+ * Groups records by name and dimensions, then writes to configured output file.
143+ */
144+ void DumpJson () const ;
145+
146+ /* *
147+ * Writes JSON to the output file.
148+ * @param root The JSON root object to write
149+ */
150+ void WriteJsonToFile (const Aws::Utils::Json::JsonValue& root) const ;
151+
152+ mutable Aws::Vector<PerformanceMetricRecord> m_performanceRecords;
153+ Aws::Set<Aws::String> m_monitoredOperations;
154+ Aws::String m_productId;
155+ Aws::String m_sdkVersion;
156+ Aws::String m_commitId;
157+ Aws::String m_outputFilename;
158+ };
159+
160+ /* *
161+ * A factory for creating instances of JsonReportingMetrics.
162+ * Used by the AWS SDK monitoring system to instantiate performance metrics collectors.
163+ */
164+ class JsonReportingMetricsFactory : public Aws ::Monitoring::MonitoringFactory {
165+ public:
166+ /* *
167+ * Constructor that initializes the factory with configuration parameters.
168+ * @param monitoredOperations Set of operations to monitor (empty means monitor all)
169+ * @param productId Product identifier (e.g., "cpp1")
170+ * @param sdkVersion SDK version string
171+ * @param commitId Git commit identifier
172+ * @param outputFilename Path to output file (e.g., "s3-perf-results.json")
173+ */
174+ JsonReportingMetricsFactory (const Aws::Set<Aws::String>& monitoredOperations = Aws::Set<Aws::String>(),
175+ const Aws::String& productId = " unknown" , const Aws::String& sdkVersion = " unknown" ,
176+ const Aws::String& commitId = " unknown" , const Aws::String& outputFilename = " performance-test-results.json" );
177+
178+ ~JsonReportingMetricsFactory () override = default ;
179+
180+ /* *
181+ * Creates a new JsonReportingMetrics instance for performance monitoring.
182+ * @return Unique pointer to monitoring interface implementation
183+ */
184+ Aws::UniquePtr<Aws::Monitoring::MonitoringInterface> CreateMonitoringInstance () const override ;
185+
186+ private:
187+ Aws::Set<Aws::String> m_monitoredOperations;
188+ Aws::String m_productId;
189+ Aws::String m_sdkVersion;
190+ Aws::String m_commitId;
191+ Aws::String m_outputFilename;
192+ };
193+ } // namespace Reporting
194+ } // namespace PerformanceTest
0 commit comments