1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PERFORMANCE_ANALYSIS_H 17 #define PERFORMANCE_ANALYSIS_H 18 19 #include <fstream> 20 #include <vector> 21 22 #include "db_types.h" 23 24 namespace DistributedDB { 25 enum PT_TEST_RECORDS : uint32_t { 26 RECORD_PUT_DATA = 1, 27 RECORD_SYNC_TOTAL, 28 RECORD_WATERMARK_SYNC, 29 RECORD_READ_DATA, 30 RECORD_SAVE_DATA, 31 RECORD_SAVE_LOCAL_WATERMARK, 32 RECORD_SAVE_PEER_WATERMARK, 33 RECORD_DATA_SEND_REQUEST_TO_ACK_RECV, 34 RECORD_DATA_REQUEST_RECV_TO_SEND_ACK, 35 RECORD_MACHINE_START_TO_PUSH_SEND, 36 RECORD_ACK_RECV_TO_USER_CALL_BACK, 37 }; 38 39 enum MV_TEST_RECORDS : uint32_t { 40 RECORD_SEND_LOCAL_DATA_CHANGED_TO_COMMIT_REQUEST_RECV = 3, 41 RECORD_GET_DEVICE_LATEST_COMMIT, 42 RECORD_COMMIT_SEND_REQUEST_TO_ACK_RECV, 43 RECORD_GET_COMMIT_TREE, 44 RECORD_DATA_GET_VALID_COMMIT, 45 RECORD_DATA_ENTRY_SEND_REQUEST_TO_ACK_RECV, 46 RECORD_GET_COMMIT_DATA, 47 RECORD_GET_VALUE_SLICE_NODE, 48 RECORD_VALUE_SLICE_SEND_REQUEST_TO_ACK_RECV, 49 RECORD_READ_VALUE_SLICE, 50 RECORD_SAVE_VALUE_SLICE, 51 RECORD_PUT_COMMIT_DATA, 52 RECORD_MERGE, 53 }; 54 55 struct TimePair { 56 Timestamp startTime = 0; 57 Timestamp endTime = 0; 58 }; 59 60 struct StatisticsInfo { 61 Timestamp max = 0; 62 Timestamp min = 0; 63 float average = 0.0; 64 }; 65 66 struct SingleStatistics { 67 std::vector<TimePair> timeInfo; 68 }; 69 70 class PerformanceAnalysis { 71 public: 72 explicit PerformanceAnalysis(uint32_t step); 73 ~PerformanceAnalysis(); 74 75 static PerformanceAnalysis *GetInstance(int stepNum = 20); 76 77 void TimeRecordStart(); 78 79 void TimeRecordEnd(); 80 81 void StepTimeRecordStart(uint32_t step); 82 83 void StepTimeRecordEnd(uint32_t step); 84 85 std::string GetStatistics(); 86 87 void OpenPerformanceAnalysis(); 88 89 void ClosePerformanceAnalysis(); 90 91 void SetFileNumber(const std::string &FileID); 92 93 private: 94 95 bool IsStepValid(uint32_t step) const; 96 97 bool IsOpen() const; 98 99 bool InsertTimeRecord(const TimePair &timePair, uint32_t step); 100 101 bool GetTimeRecord(uint32_t step, TimePair &timePair) const; 102 103 void OutStatistics(); 104 105 void Clear(); 106 107 void Close(); 108 109 const static int MAX_TIMERECORD_STEP_NUM = 200; 110 const static std::string STATISTICAL_DATA_FILE_NAME_HEADER; 111 const static std::string CSV_FILE_EXTENSION; 112 const static std::string DEFAULT_FILE_NAME; 113 SingleStatistics timeRecordData_; 114 std::vector<StatisticsInfo> stepTimeRecordInfo_; 115 std::vector<uint64_t> counts_; 116 uint32_t stepNum_; 117 bool isOpen_; 118 std::ofstream outFile; 119 int fileNumber_; 120 std::string fileID_; 121 }; 122 } // namespace DistributedDB 123 #endif 124