Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | *is % allowed in string |
| 32 | */ |
| 33 | #ifndef GRPC_TEST_CPP_METRICS_SERVER_H |
| 34 | #define GRPC_TEST_CPP_METRICS_SERVER_H |
| 35 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 36 | #include <map> |
| 37 | #include <mutex> |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 38 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 39 | #include "src/proto/grpc/testing/metrics.grpc.pb.h" |
| 40 | #include "src/proto/grpc/testing/metrics.pb.h" |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 41 | |
| 42 | /* |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 43 | * This implements a Metrics server defined in |
| 44 | * src/proto/grpc/testing/metrics.proto. Any |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 45 | * test service can use this to export Metrics (TODO (sreek): Only Gauges for |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 46 | * now). |
| 47 | * |
| 48 | * Example: |
| 49 | * MetricsServiceImpl metricsImpl; |
| 50 | * .. |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame] | 51 | * // Create QpsGauge(s). Note: QpsGauges can be created even after calling |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 52 | * // 'StartServer'. |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame] | 53 | * QpsGauge qps_gauge1 = metricsImpl.CreateQpsGauge("foo", is_present); |
| 54 | * // qps_gauge1 can now be used anywhere in the program by first making a |
| 55 | * // one-time call qps_gauge1.Reset() and then calling qps_gauge1.Incr() |
| 56 | * // every time to increment a query counter |
| 57 | * |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 58 | * ... |
| 59 | * // Create the metrics server |
| 60 | * std::unique_ptr<grpc::Server> server = metricsImpl.StartServer(port); |
| 61 | * server->Wait(); // Note: This is blocking. |
| 62 | */ |
| 63 | namespace grpc { |
| 64 | namespace testing { |
| 65 | |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame] | 66 | class QpsGauge { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 67 | public: |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame] | 68 | QpsGauge(); |
| 69 | |
| 70 | // Initialize the internal timer and reset the query count to 0 |
| 71 | void Reset(); |
| 72 | |
| 73 | // Increment the query count by 1 |
| 74 | void Incr(); |
| 75 | |
| 76 | // Return the current qps (i.e query count divided by the time since this |
| 77 | // QpsGauge object created (or Reset() was called)) |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 78 | long Get(); |
| 79 | |
| 80 | private: |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame] | 81 | gpr_timespec start_time_; |
| 82 | long num_queries_; |
| 83 | std::mutex num_queries_mu_; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | class MetricsServiceImpl GRPC_FINAL : public MetricsService::Service { |
| 87 | public: |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 88 | grpc::Status GetAllGauges(ServerContext* context, const EmptyMessage* request, |
| 89 | ServerWriter<GaugeResponse>* writer) GRPC_OVERRIDE; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 90 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 91 | grpc::Status GetGauge(ServerContext* context, const GaugeRequest* request, |
| 92 | GaugeResponse* response) GRPC_OVERRIDE; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 93 | |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame] | 94 | // Create a QpsGauge with name 'name'. is_present is set to true if the Gauge |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 95 | // is already present in the map. |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame] | 96 | // NOTE: CreateQpsGauge can be called anytime (i.e before or after calling |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 97 | // StartServer). |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame] | 98 | std::shared_ptr<QpsGauge> CreateQpsGauge(const grpc::string& name, |
Sree Kuchibhotla | 8c065a7 | 2016-04-26 12:11:42 -0700 | [diff] [blame] | 99 | bool* already_present); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 100 | |
| 101 | std::unique_ptr<grpc::Server> StartServer(int port); |
| 102 | |
| 103 | private: |
Sree Kuchibhotla | 3714e30 | 2016-04-22 09:50:46 -0700 | [diff] [blame] | 104 | std::map<string, std::shared_ptr<QpsGauge>> qps_gauges_; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 105 | std::mutex mu_; |
| 106 | }; |
| 107 | |
| 108 | } // namespace testing |
| 109 | } // namespace grpc |
| 110 | |
| 111 | #endif // GRPC_TEST_CPP_METRICS_SERVER_H |