1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef TEST_FAKE_PRODUCER_H_ 18 #define TEST_FAKE_PRODUCER_H_ 19 20 #include <memory> 21 #include <random> 22 #include <string> 23 24 #include "perfetto/base/thread_checker.h" 25 #include "perfetto/tracing/core/data_source_descriptor.h" 26 #include "perfetto/tracing/core/producer.h" 27 #include "perfetto/tracing/core/trace_config.h" 28 #include "perfetto/tracing/ipc/producer_ipc_client.h" 29 #include "src/base/test/test_task_runner.h" 30 31 namespace perfetto { 32 33 class FakeProducer : public Producer { 34 public: 35 explicit FakeProducer(const std::string& name); 36 ~FakeProducer() override; 37 38 void Connect(const char* socket_name, 39 base::TaskRunner* task_runner, 40 std::function<void()> on_setup_data_source_instance, 41 std::function<void()> on_create_data_source_instance); 42 43 // Produces a batch of events (as configured in the DataSourceConfig) and 44 // posts a callback when the service acknowledges the commit. 45 void ProduceEventBatch(std::function<void()> callback = [] {}); 46 47 // Producer implementation. 48 void OnConnect() override; 49 void OnDisconnect() override; 50 void SetupDataSource(DataSourceInstanceID, 51 const DataSourceConfig& source_config) override; 52 void StartDataSource(DataSourceInstanceID, 53 const DataSourceConfig& source_config) override; 54 void StopDataSource(DataSourceInstanceID) override; 55 void OnTracingSetup() override; 56 void Flush(FlushRequestID, const DataSourceInstanceID*, size_t) override; ClearIncrementalState(const DataSourceInstanceID *,size_t)57 void ClearIncrementalState(const DataSourceInstanceID* /*data_source_ids*/, 58 size_t /*num_data_sources*/) override {} 59 60 private: 61 void Shutdown(); 62 63 base::ThreadChecker thread_checker_; 64 base::TaskRunner* task_runner_ = nullptr; 65 std::string name_; 66 std::minstd_rand0 rnd_engine_; 67 uint32_t message_size_ = 0; 68 uint32_t message_count_ = 0; 69 uint32_t max_messages_per_second_ = 0; 70 std::function<void()> on_setup_data_source_instance_; 71 std::function<void()> on_create_data_source_instance_; 72 std::unique_ptr<TracingService::ProducerEndpoint> endpoint_; 73 std::unique_ptr<TraceWriter> trace_writer_; 74 }; 75 76 } // namespace perfetto 77 78 #endif // TEST_FAKE_PRODUCER_H_ 79