Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 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> |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 21 | #include <random> |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 24 | #include "perfetto/base/thread_checker.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 25 | #include "perfetto/tracing/core/data_source_descriptor.h" |
| 26 | #include "perfetto/tracing/core/producer.h" |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 27 | #include "perfetto/tracing/core/trace_config.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 28 | #include "perfetto/tracing/ipc/producer_ipc_client.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 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 | |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 38 | void Connect(const char* socket_name, |
| 39 | base::TaskRunner* task_runner, |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 40 | std::function<void()> on_create_data_source_instance); |
| 41 | |
| 42 | // Produces a batch of events (as configured in the DataSourceConfig) and |
| 43 | // posts a callback when the service acknowledges the commit. |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 44 | void ProduceEventBatch(std::function<void()> callback = [] {}); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 45 | |
| 46 | // Producer implementation. |
| 47 | void OnConnect() override; |
| 48 | void OnDisconnect() override; |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 49 | void SetupDataSource(DataSourceInstanceID, |
| 50 | const DataSourceConfig& source_config) override; |
Primiano Tucci | afb72b5 | 2018-09-25 09:37:24 +0100 | [diff] [blame] | 51 | void StartDataSource(DataSourceInstanceID, |
| 52 | const DataSourceConfig& source_config) override; |
| 53 | void StopDataSource(DataSourceInstanceID) override; |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 54 | void OnTracingSetup() override; |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 55 | void Flush(FlushRequestID, const DataSourceInstanceID*, size_t) override; |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | void Shutdown(); |
| 59 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 60 | base::ThreadChecker thread_checker_; |
| 61 | base::TaskRunner* task_runner_ = nullptr; |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 62 | std::string name_; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 63 | std::minstd_rand0 rnd_engine_; |
Primiano Tucci | f026990 | 2018-04-13 11:13:19 +0100 | [diff] [blame] | 64 | uint32_t message_size_ = 0; |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 65 | uint32_t message_count_ = 0; |
| 66 | uint32_t max_messages_per_second_ = 0; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 67 | std::function<void()> on_create_data_source_instance_; |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 68 | std::unique_ptr<TracingService::ProducerEndpoint> endpoint_; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 69 | std::unique_ptr<TraceWriter> trace_writer_; |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | } // namespace perfetto |
| 73 | |
| 74 | #endif // TEST_FAKE_PRODUCER_H_ |