perfetto: refactor testing code to make benchmarks easier to write

Bug: 74380167
Change-Id: Idfb37433e610dd4871127fb5fe01fe6ca6ea28a1
diff --git a/Android.bp b/Android.bp
index 4165748..2febd4a 100644
--- a/Android.bp
+++ b/Android.bp
@@ -71,6 +71,7 @@
     "src/tracing/core/shared_memory_abi.cc",
     "src/tracing/core/shared_memory_arbiter_impl.cc",
     "src/tracing/core/sliced_protobuf_input_stream.cc",
+    "src/tracing/core/test_config.cc",
     "src/tracing/core/trace_buffer.cc",
     "src/tracing/core/trace_config.cc",
     "src/tracing/core/trace_packet.cc",
@@ -153,6 +154,7 @@
     "src/tracing/core/shared_memory_abi.cc",
     "src/tracing/core/shared_memory_arbiter_impl.cc",
     "src/tracing/core/sliced_protobuf_input_stream.cc",
+    "src/tracing/core/test_config.cc",
     "src/tracing/core/trace_buffer.cc",
     "src/tracing/core/trace_config.cc",
     "src/tracing/core/trace_packet.cc",
@@ -279,6 +281,7 @@
     "src/tracing/core/shared_memory_abi.cc",
     "src/tracing/core/shared_memory_arbiter_impl.cc",
     "src/tracing/core/sliced_protobuf_input_stream.cc",
+    "src/tracing/core/test_config.cc",
     "src/tracing/core/trace_buffer.cc",
     "src/tracing/core/trace_config.cc",
     "src/tracing/core/trace_packet.cc",
@@ -336,6 +339,7 @@
     "protos/perfetto/config/data_source_config.proto",
     "protos/perfetto/config/data_source_descriptor.proto",
     "protos/perfetto/config/ftrace/ftrace_config.proto",
+    "protos/perfetto/config/test_config.proto",
     "protos/perfetto/config/trace_config.proto",
   ],
   tools: [
@@ -347,6 +351,7 @@
     "external/perfetto/protos/perfetto/config/data_source_config.pb.cc",
     "external/perfetto/protos/perfetto/config/data_source_descriptor.pb.cc",
     "external/perfetto/protos/perfetto/config/ftrace/ftrace_config.pb.cc",
+    "external/perfetto/protos/perfetto/config/test_config.pb.cc",
     "external/perfetto/protos/perfetto/config/trace_config.pb.cc",
   ],
 }
@@ -359,6 +364,7 @@
     "protos/perfetto/config/data_source_config.proto",
     "protos/perfetto/config/data_source_descriptor.proto",
     "protos/perfetto/config/ftrace/ftrace_config.proto",
+    "protos/perfetto/config/test_config.proto",
     "protos/perfetto/config/trace_config.proto",
   ],
   tools: [
@@ -370,6 +376,7 @@
     "external/perfetto/protos/perfetto/config/data_source_config.pb.h",
     "external/perfetto/protos/perfetto/config/data_source_descriptor.pb.h",
     "external/perfetto/protos/perfetto/config/ftrace/ftrace_config.pb.h",
+    "external/perfetto/protos/perfetto/config/test_config.pb.h",
     "external/perfetto/protos/perfetto/config/trace_config.pb.h",
   ],
   export_include_dirs: [
@@ -2960,6 +2967,7 @@
     "src/tracing/core/shared_memory_abi.cc",
     "src/tracing/core/shared_memory_arbiter_impl.cc",
     "src/tracing/core/sliced_protobuf_input_stream.cc",
+    "src/tracing/core/test_config.cc",
     "src/tracing/core/trace_buffer.cc",
     "src/tracing/core/trace_config.cc",
     "src/tracing/core/trace_packet.cc",
@@ -3117,6 +3125,7 @@
     "src/tracing/core/shared_memory_arbiter_impl_unittest.cc",
     "src/tracing/core/sliced_protobuf_input_stream.cc",
     "src/tracing/core/sliced_protobuf_input_stream_unittest.cc",
+    "src/tracing/core/test_config.cc",
     "src/tracing/core/trace_buffer.cc",
     "src/tracing/core/trace_buffer_unittest.cc",
     "src/tracing/core/trace_config.cc",
diff --git a/include/perfetto/tracing/core/data_source_config.h b/include/perfetto/tracing/core/data_source_config.h
index c474761..3786093 100644
--- a/include/perfetto/tracing/core/data_source_config.h
+++ b/include/perfetto/tracing/core/data_source_config.h
@@ -35,6 +35,7 @@
 
 #include "perfetto/tracing/core/chrome_config.h"
 #include "perfetto/tracing/core/ftrace_config.h"
+#include "perfetto/tracing/core/test_config.h"
 
 // Forward declarations for protobuf types.
 namespace perfetto {
@@ -42,6 +43,7 @@
 class DataSourceConfig;
 class FtraceConfig;
 class ChromeConfig;
+class TestConfig;
 }  // namespace protos
 }  // namespace perfetto
 
@@ -75,12 +77,16 @@
   const ChromeConfig& chrome_config() const { return chrome_config_; }
   ChromeConfig* mutable_chrome_config() { return &chrome_config_; }
 
+  const TestConfig& for_testing() const { return for_testing_; }
+  TestConfig* mutable_for_testing() { return &for_testing_; }
+
  private:
   std::string name_ = {};
   uint32_t target_buffer_ = {};
   uint32_t trace_duration_ms_ = {};
   FtraceConfig ftrace_config_ = {};
   ChromeConfig chrome_config_ = {};
+  TestConfig for_testing_ = {};
 
   // Allows to preserve unknown protobuf fields for compatibility
   // with future versions of .proto files.
diff --git a/include/perfetto/tracing/core/test_config.h b/include/perfetto/tracing/core/test_config.h
new file mode 100644
index 0000000..a03f5d3
--- /dev/null
+++ b/include/perfetto/tracing/core/test_config.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*******************************************************************************
+ * AUTOGENERATED - DO NOT EDIT
+ *******************************************************************************
+ * This file has been generated from the protobuf message
+ * perfetto/config/test_config.proto
+ * by
+ * ../../tools/proto_to_cpp/proto_to_cpp.cc.
+ * If you need to make changes here, change the .proto file and then run
+ * ./tools/gen_tracing_cpp_headers_from_protos.py
+ */
+
+#ifndef INCLUDE_PERFETTO_TRACING_CORE_TEST_CONFIG_H_
+#define INCLUDE_PERFETTO_TRACING_CORE_TEST_CONFIG_H_
+
+#include <stdint.h>
+#include <string>
+#include <type_traits>
+#include <vector>
+
+// Forward declarations for protobuf types.
+namespace perfetto {
+namespace protos {
+class TestConfig;
+}
+}  // namespace perfetto
+
+namespace perfetto {
+
+class TestConfig {
+ public:
+  TestConfig();
+  ~TestConfig();
+  TestConfig(TestConfig&&) noexcept;
+  TestConfig& operator=(TestConfig&&);
+  TestConfig(const TestConfig&);
+  TestConfig& operator=(const TestConfig&);
+
+  // Conversion methods from/to the corresponding protobuf types.
+  void FromProto(const perfetto::protos::TestConfig&);
+  void ToProto(perfetto::protos::TestConfig*) const;
+
+  uint32_t message_count() const { return message_count_; }
+  void set_message_count(uint32_t value) { message_count_ = value; }
+
+  uint32_t seed() const { return seed_; }
+  void set_seed(uint32_t value) { seed_ = value; }
+
+ private:
+  uint32_t message_count_ = {};
+  uint32_t seed_ = {};
+
+  // Allows to preserve unknown protobuf fields for compatibility
+  // with future versions of .proto files.
+  std::string unknown_fields_;
+};
+
+}  // namespace perfetto
+#endif  // INCLUDE_PERFETTO_TRACING_CORE_TEST_CONFIG_H_
diff --git a/include/perfetto/tracing/core/trace_config.h b/include/perfetto/tracing/core/trace_config.h
index b1ed165..7cc73db 100644
--- a/include/perfetto/tracing/core/trace_config.h
+++ b/include/perfetto/tracing/core/trace_config.h
@@ -44,6 +44,7 @@
 class DataSourceConfig;
 class FtraceConfig;
 class ChromeConfig;
+class TestConfig;
 }  // namespace protos
 }  // namespace perfetto
 
diff --git a/protos/perfetto/config/BUILD.gn b/protos/perfetto/config/BUILD.gn
index 2632f9a..517ba1a 100644
--- a/protos/perfetto/config/BUILD.gn
+++ b/protos/perfetto/config/BUILD.gn
@@ -24,6 +24,7 @@
     "data_source_config.proto",
     "data_source_descriptor.proto",
     "ftrace/ftrace_config.proto",
+    "test_config.proto",
     "trace_config.proto",
   ]
 }
diff --git a/protos/perfetto/config/data_source_config.proto b/protos/perfetto/config/data_source_config.proto
index ee4f44b..3c33a10 100644
--- a/protos/perfetto/config/data_source_config.proto
+++ b/protos/perfetto/config/data_source_config.proto
@@ -21,7 +21,7 @@
 
 import "perfetto/config/chrome/chrome_config.proto";
 import "perfetto/config/ftrace/ftrace_config.proto";
-
+import "perfetto/config/test_config.proto";
 // When editing this file run ./tools/gen_tracing_cpp_headers_from_protos.py
 // to reflect changes in the corresponding C++ headers.
 
@@ -49,4 +49,8 @@
 
   optional FtraceConfig ftrace_config = 100;
   optional ChromeConfig chrome_config = 101;
+
+  // This field is only used for testing.
+  optional TestConfig for_testing =
+      536870911;  // 2^29 - 1, max field id for protos.
 }
diff --git a/protos/perfetto/config/perfetto_config.proto b/protos/perfetto/config/perfetto_config.proto
index 21da1e0..be3cf53 100644
--- a/protos/perfetto/config/perfetto_config.proto
+++ b/protos/perfetto/config/perfetto_config.proto
@@ -24,7 +24,6 @@
 
 // Begin of protos/perfetto/config/data_source_config.proto
 
-
 // When editing this file run ./tools/gen_tracing_cpp_headers_from_protos.py
 // to reflect changes in the corresponding C++ headers.
 
@@ -52,6 +51,10 @@
 
   optional FtraceConfig ftrace_config = 100;
   optional ChromeConfig chrome_config = 101;
+
+  // This field is only used for testing.
+  optional TestConfig for_testing =
+      536870911;  // 2^29 - 1, max field id for protos.
 }
 
 // End of protos/perfetto/config/data_source_config.proto
@@ -72,6 +75,23 @@
 
 // End of protos/perfetto/config/ftrace/ftrace_config.proto
 
+// Begin of protos/perfetto/config/test_config.proto
+
+// When editing this file run ./tools/gen_tracing_cpp_headers_from_protos.py
+// to reflect changes in the corresponding C++ headers.
+
+// The configuration for a fake producer used in tests.
+message TestConfig {
+  // The number of messages the fake producer should send.
+  optional uint32 message_count = 1;
+
+  // The seed value for a simple multiplicative congruential pseudo-random
+  // number sequence.
+  optional uint32 seed = 2;
+}
+
+// End of protos/perfetto/config/test_config.proto
+
 // Begin of protos/perfetto/config/trace_config.proto
 
 // When editing this file run ./tools/gen_tracing_cpp_headers_from_protos.py
diff --git a/protos/perfetto/config/test_config.proto b/protos/perfetto/config/test_config.proto
new file mode 100644
index 0000000..c97f9fa
--- /dev/null
+++ b/protos/perfetto/config/test_config.proto
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+syntax = "proto2";
+option optimize_for = LITE_RUNTIME;
+
+package perfetto.protos;
+
+// When editing this file run ./tools/gen_tracing_cpp_headers_from_protos.py
+// to reflect changes in the corresponding C++ headers.
+
+// The configuration for a fake producer used in tests.
+message TestConfig {
+  // The number of messages the fake producer should send.
+  optional uint32 message_count = 1;
+
+  // The seed value for a simple multiplicative congruential pseudo-random
+  // number sequence.
+  optional uint32 seed = 2;
+}
diff --git a/protos/perfetto/trace/test_event.proto b/protos/perfetto/trace/test_event.proto
index f43e4bd..bd0fcf0 100644
--- a/protos/perfetto/trace/test_event.proto
+++ b/protos/perfetto/trace/test_event.proto
@@ -19,6 +19,13 @@
 
 package perfetto.protos;
 
+// Event used by testing code.
 message TestEvent {
-  optional string str = 1;
+  oneof data {
+    // Arbitary string used in tests.
+    string str = 1;
+
+    // The current value of the random number sequence used in tests.
+    uint32 seq_value = 2;
+  }
 }
diff --git a/src/tracing/BUILD.gn b/src/tracing/BUILD.gn
index 62f26d7..5cdb89c 100644
--- a/src/tracing/BUILD.gn
+++ b/src/tracing/BUILD.gn
@@ -46,6 +46,7 @@
     "core/shared_memory_arbiter_impl.h",
     "core/sliced_protobuf_input_stream.cc",
     "core/sliced_protobuf_input_stream.h",
+    "core/test_config.cc",
     "core/trace_buffer.cc",
     "core/trace_buffer.h",
     "core/trace_config.cc",
diff --git a/src/tracing/core/data_source_config.cc b/src/tracing/core/data_source_config.cc
index 069443c..622eb4c 100644
--- a/src/tracing/core/data_source_config.cc
+++ b/src/tracing/core/data_source_config.cc
@@ -30,6 +30,7 @@
 #include "perfetto/config/chrome/chrome_config.pb.h"
 #include "perfetto/config/data_source_config.pb.h"
 #include "perfetto/config/ftrace/ftrace_config.pb.h"
+#include "perfetto/config/test_config.pb.h"
 
 namespace perfetto {
 
@@ -58,6 +59,8 @@
   ftrace_config_.FromProto(proto.ftrace_config());
 
   chrome_config_.FromProto(proto.chrome_config());
+
+  for_testing_.FromProto(proto.for_testing());
   unknown_fields_ = proto.unknown_fields();
 }
 
@@ -82,6 +85,8 @@
   ftrace_config_.ToProto(proto->mutable_ftrace_config());
 
   chrome_config_.ToProto(proto->mutable_chrome_config());
+
+  for_testing_.ToProto(proto->mutable_for_testing());
   *(proto->mutable_unknown_fields()) = unknown_fields_;
 }
 
diff --git a/src/tracing/core/test_config.cc b/src/tracing/core/test_config.cc
new file mode 100644
index 0000000..c20f8f5
--- /dev/null
+++ b/src/tracing/core/test_config.cc
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*******************************************************************************
+ * AUTOGENERATED - DO NOT EDIT
+ *******************************************************************************
+ * This file has been generated from the protobuf message
+ * perfetto/config/test_config.proto
+ * by
+ * ../../tools/proto_to_cpp/proto_to_cpp.cc.
+ * If you need to make changes here, change the .proto file and then run
+ * ./tools/gen_tracing_cpp_headers_from_protos.py
+ */
+
+#include "perfetto/tracing/core/test_config.h"
+
+#include "perfetto/config/test_config.pb.h"
+
+namespace perfetto {
+
+TestConfig::TestConfig() = default;
+TestConfig::~TestConfig() = default;
+TestConfig::TestConfig(const TestConfig&) = default;
+TestConfig& TestConfig::operator=(const TestConfig&) = default;
+TestConfig::TestConfig(TestConfig&&) noexcept = default;
+TestConfig& TestConfig::operator=(TestConfig&&) = default;
+
+void TestConfig::FromProto(const perfetto::protos::TestConfig& proto) {
+  static_assert(sizeof(message_count_) == sizeof(proto.message_count()),
+                "size mismatch");
+  message_count_ = static_cast<decltype(message_count_)>(proto.message_count());
+
+  static_assert(sizeof(seed_) == sizeof(proto.seed()), "size mismatch");
+  seed_ = static_cast<decltype(seed_)>(proto.seed());
+  unknown_fields_ = proto.unknown_fields();
+}
+
+void TestConfig::ToProto(perfetto::protos::TestConfig* proto) const {
+  proto->Clear();
+
+  static_assert(sizeof(message_count_) == sizeof(proto->message_count()),
+                "size mismatch");
+  proto->set_message_count(
+      static_cast<decltype(proto->message_count())>(message_count_));
+
+  static_assert(sizeof(seed_) == sizeof(proto->seed()), "size mismatch");
+  proto->set_seed(static_cast<decltype(proto->seed())>(seed_));
+  *(proto->mutable_unknown_fields()) = unknown_fields_;
+}
+
+}  // namespace perfetto
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 707a5eb..587cc12 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -27,18 +27,15 @@
   deps = [
     ":fake_consumer",
     ":task_runner_thread",
+    ":task_runner_thread_delegates",
     "../gn:default_deps",
     "../gn:gtest_deps",
     "../protos/perfetto/trace:lite",
     "../src/base:base",
     "../src/base:test_support",
-    "../src/traced/probes:probes_src",
-    "../src/tracing:ipc",
   ]
   sources = [
     "end_to_end_integrationtest.cc",
-    "fake_producer.cc",
-    "fake_producer.h",
   ]
   if (is_android && !build_with_chromium) {
     deps += [ "../src/base:android_task_runner" ]
@@ -95,6 +92,22 @@
   ]
 }
 
+source_set("task_runner_thread_delegates") {
+  testonly = true
+  deps = [
+    ":task_runner_thread",
+    "../gn:default_deps",
+    "../src/base:test_support",
+    "../src/traced/probes:probes_src",
+    "../src/tracing:ipc",
+  ]
+  sources = [
+    "fake_producer.cc",
+    "fake_producer.h",
+    "task_runner_thread_delegates.h",
+  ]
+}
+
 if (!build_with_chromium) {
   source_set("benchmark_main") {
     testonly = true
diff --git a/test/end_to_end_integrationtest.cc b/test/end_to_end_integrationtest.cc
index a6fd5dc..b804540 100644
--- a/test/end_to_end_integrationtest.cc
+++ b/test/end_to_end_integrationtest.cc
@@ -19,6 +19,7 @@
 #include <chrono>
 #include <condition_variable>
 #include <functional>
+#include <random>
 #include <thread>
 
 #include "perfetto/base/logging.h"
@@ -29,13 +30,10 @@
 #include "perfetto/tracing/core/trace_config.h"
 #include "perfetto/tracing/core/trace_packet.h"
 #include "perfetto/tracing/ipc/consumer_ipc_client.h"
-#include "perfetto/tracing/ipc/service_ipc_host.h"
-
 #include "src/base/test/test_task_runner.h"
-#include "src/traced/probes/probes_producer.h"
 #include "test/fake_consumer.h"
-#include "test/fake_producer.h"
 #include "test/task_runner_thread.h"
+#include "test/task_runner_thread_delegates.h"
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
 #include "perfetto/base/android_task_runner.h"
@@ -64,56 +62,6 @@
  public:
   PerfettoTest() {}
   ~PerfettoTest() override = default;
-
- protected:
-  // This is used only in daemon starting integrations tests.
-  class ServiceDelegate : public ThreadDelegate {
-   public:
-    ServiceDelegate() = default;
-    ~ServiceDelegate() override = default;
-
-    void Initialize(base::TaskRunner* task_runner) override {
-      svc_ = ServiceIPCHost::CreateInstance(task_runner);
-      unlink(TEST_PRODUCER_SOCK_NAME);
-      unlink(TEST_CONSUMER_SOCK_NAME);
-      svc_->Start(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME);
-    }
-
-   private:
-    std::unique_ptr<ServiceIPCHost> svc_;
-  };
-
-  // This is used only in daemon starting integrations tests.
-  class ProbesProducerDelegate : public ThreadDelegate {
-   public:
-    ProbesProducerDelegate() = default;
-    ~ProbesProducerDelegate() override = default;
-
-    void Initialize(base::TaskRunner* task_runner) override {
-      producer_.reset(new ProbesProducer);
-      producer_->ConnectWithRetries(TEST_PRODUCER_SOCK_NAME, task_runner);
-    }
-
-   private:
-    std::unique_ptr<ProbesProducer> producer_;
-  };
-
-  class FakeProducerDelegate : public ThreadDelegate {
-   public:
-    FakeProducerDelegate(std::function<void()> connect_callback)
-        : connect_callback_(std::move(connect_callback)) {}
-    ~FakeProducerDelegate() override = default;
-
-    void Initialize(base::TaskRunner* task_runner) override {
-      producer_.reset(new FakeProducer("android.perfetto.FakeProducer"));
-      producer_->Connect(TEST_PRODUCER_SOCK_NAME, task_runner,
-                         std::move(connect_callback_));
-    }
-
-   private:
-    std::unique_ptr<FakeProducer> producer_;
-    std::function<void()> connect_callback_;
-  };
 };
 
 // TODO(b/73453011): reenable this on more platforms (including standalone
@@ -167,11 +115,12 @@
 
 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
   TaskRunnerThread service_thread;
-  service_thread.Start(std::unique_ptr<ServiceDelegate>(new ServiceDelegate));
+  service_thread.Start(std::unique_ptr<ServiceDelegate>(
+      new ServiceDelegate(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME)));
 
   TaskRunnerThread producer_thread;
-  producer_thread.Start(
-      std::unique_ptr<ProbesProducerDelegate>(new ProbesProducerDelegate));
+  producer_thread.Start(std::unique_ptr<ProbesProducerDelegate>(
+      new ProbesProducerDelegate(TEST_PRODUCER_SOCK_NAME)));
 #endif
 
   // Finally, make the consumer connect to the service.
@@ -208,24 +157,35 @@
   ds_config->set_name("android.perfetto.FakeProducer");
   ds_config->set_target_buffer(0);
 
+  // The parameters for the producer.
+  static constexpr uint32_t kRandomSeed = 42;
+  static constexpr uint32_t kEventCount = 10;
+
+  // Setup the test to use a random number generator.
+  ds_config->mutable_for_testing()->set_seed(kRandomSeed);
+  ds_config->mutable_for_testing()->set_message_count(kEventCount);
+
+  // Create the random generator with the same seed.
+  std::minstd_rand0 random(kRandomSeed);
+
   // Create the function to handle packets as they come in.
   uint64_t total = 0;
-  auto function = [&total, &finish](std::vector<TracePacket> packets,
-                                    bool has_more) {
+  auto function = [&total, &finish, &random](std::vector<TracePacket> packets,
+                                             bool has_more) {
     if (has_more) {
       for (auto& packet : packets) {
         packet.Decode();
         ASSERT_TRUE(packet->has_for_testing());
         ASSERT_EQ(protos::TracePacket::kTrustedUid,
                   packet->optional_trusted_uid_case());
-        ASSERT_EQ(packet->for_testing().str(), "test");
+        ASSERT_EQ(packet->for_testing().seq_value(), random());
       }
       total += packets.size();
 
       // TODO(lalitm): renable this when stiching inside the service is present.
       // ASSERT_FALSE(packets->empty());
     } else {
-      ASSERT_EQ(total, 10u);
+      ASSERT_EQ(total, kEventCount);
       ASSERT_TRUE(packets.empty());
       finish();
     }
@@ -233,15 +193,17 @@
 
 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
   TaskRunnerThread service_thread;
-  service_thread.Start(std::unique_ptr<ServiceDelegate>(new ServiceDelegate));
+  service_thread.Start(std::unique_ptr<ServiceDelegate>(
+      new ServiceDelegate(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME)));
 #endif
 
   auto data_produced = task_runner.CreateCheckpoint("data.produced");
   TaskRunnerThread producer_thread;
-  producer_thread.Start(std::unique_ptr<FakeProducerDelegate>(
-      new FakeProducerDelegate([&task_runner, &data_produced] {
-        task_runner.PostTask(data_produced);
-      })));
+  producer_thread.Start(
+      std::unique_ptr<FakeProducerDelegate>(new FakeProducerDelegate(
+          TEST_PRODUCER_SOCK_NAME, [&task_runner, &data_produced] {
+            task_runner.PostTask(data_produced);
+          })));
 
   // Finally, make the consumer connect to the service.
   FakeConsumer consumer(trace_config, std::move(function), &task_runner);
diff --git a/test/fake_producer.cc b/test/fake_producer.cc
index ba66b66..b16b7cb 100644
--- a/test/fake_producer.cc
+++ b/test/fake_producer.cc
@@ -16,6 +16,8 @@
 
 #include "test/fake_producer.h"
 
+#include <random>
+
 #include "perfetto/base/logging.h"
 #include "perfetto/trace/test_event.pbzero.h"
 #include "perfetto/trace/trace_packet.pbzero.h"
@@ -51,9 +53,12 @@
     const DataSourceConfig& source_config) {
   auto trace_writer = endpoint_->CreateTraceWriter(
       static_cast<BufferID>(source_config.target_buffer()));
-  for (int i = 0; i < 10; i++) {
+
+  const TestConfig& config = source_config.for_testing();
+  std::minstd_rand0 random(config.seed());
+  for (size_t i = 0; i < config.message_count(); i++) {
     auto handle = trace_writer->NewTracePacket();
-    handle->set_for_testing()->set_str("test");
+    handle->set_for_testing()->set_seq_value(random());
     handle->Finalize();
   }
 
diff --git a/test/fake_producer.h b/test/fake_producer.h
index 68a89e1..fb36d86 100644
--- a/test/fake_producer.h
+++ b/test/fake_producer.h
@@ -49,6 +49,7 @@
 
   std::string name_;
   DataSourceID id_ = 0;
+
   std::unique_ptr<Service::ProducerEndpoint> endpoint_;
   base::TaskRunner* task_runner_ = nullptr;
   std::function<void()> data_produced_callback_;
diff --git a/test/task_runner_thread_delegates.h b/test/task_runner_thread_delegates.h
new file mode 100644
index 0000000..282bec4
--- /dev/null
+++ b/test/task_runner_thread_delegates.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef TEST_TASK_RUNNER_THREAD_DELEGATES_H_
+#define TEST_TASK_RUNNER_THREAD_DELEGATES_H_
+
+#include "perfetto/tracing/ipc/service_ipc_host.h"
+#include "src/traced/probes/probes_producer.h"
+#include "test/fake_producer.h"
+#include "test/task_runner_thread.h"
+
+namespace perfetto {
+// This is used only in daemon starting integrations tests.
+class ServiceDelegate : public ThreadDelegate {
+ public:
+  ServiceDelegate(const std::string& producer_socket,
+                  const std::string& consumer_socket)
+      : producer_socket_(producer_socket), consumer_socket_(consumer_socket) {}
+  ~ServiceDelegate() override = default;
+
+  void Initialize(base::TaskRunner* task_runner) override {
+    svc_ = ServiceIPCHost::CreateInstance(task_runner);
+    unlink(producer_socket_.c_str());
+    unlink(consumer_socket_.c_str());
+    svc_->Start(producer_socket_.c_str(), consumer_socket_.c_str());
+  }
+
+ private:
+  std::string producer_socket_;
+  std::string consumer_socket_;
+  std::unique_ptr<ServiceIPCHost> svc_;
+};
+
+// This is used only in daemon starting integrations tests.
+class ProbesProducerDelegate : public ThreadDelegate {
+ public:
+  ProbesProducerDelegate(const std::string& producer_socket)
+      : producer_socket_(producer_socket) {}
+  ~ProbesProducerDelegate() override = default;
+
+  void Initialize(base::TaskRunner* task_runner) override {
+    producer_.reset(new ProbesProducer);
+    producer_->ConnectWithRetries(producer_socket_.c_str(), task_runner);
+  }
+
+ private:
+  std::string producer_socket_;
+  std::unique_ptr<ProbesProducer> producer_;
+};
+
+class FakeProducerDelegate : public ThreadDelegate {
+ public:
+  FakeProducerDelegate(const std::string& producer_socket,
+                       std::function<void()> connect_callback)
+      : producer_socket_(producer_socket),
+        connect_callback_(std::move(connect_callback)) {}
+  ~FakeProducerDelegate() override = default;
+
+  void Initialize(base::TaskRunner* task_runner) override {
+    producer_.reset(new FakeProducer("android.perfetto.FakeProducer"));
+    producer_->Connect(producer_socket_.c_str(), task_runner,
+                       std::move(connect_callback_));
+  }
+
+ private:
+  std::string producer_socket_;
+  std::unique_ptr<FakeProducer> producer_;
+  std::function<void()> connect_callback_;
+};
+}  // namespace perfetto
+
+#endif  // TEST_TASK_RUNNER_THREAD_DELEGATES_H_
diff --git a/tools/gen_merged_trace_config b/tools/gen_merged_trace_config
index dc5d8e5..9a7abba 100755
--- a/tools/gen_merged_trace_config
+++ b/tools/gen_merged_trace_config
@@ -22,6 +22,7 @@
   'protos/perfetto/config/chrome/chrome_config.proto',
   'protos/perfetto/config/data_source_config.proto',
   'protos/perfetto/config/ftrace/ftrace_config.proto',
+  'protos/perfetto/config/test_config.proto',
   'protos/perfetto/config/trace_config.proto',
 )
 
diff --git a/tools/gen_tracing_cpp_headers_from_protos.py b/tools/gen_tracing_cpp_headers_from_protos.py
index fc250ac..d855c77 100755
--- a/tools/gen_tracing_cpp_headers_from_protos.py
+++ b/tools/gen_tracing_cpp_headers_from_protos.py
@@ -23,6 +23,7 @@
   'perfetto/config/data_source_descriptor.proto',
   'perfetto/config/ftrace/ftrace_config.proto',
   'perfetto/config/trace_config.proto',
+  'perfetto/config/test_config.proto',
   'perfetto/ipc/commit_data_request.proto',
 )