From c8e145bd93d3aa0ae02fe8bbddc265f1c35b5339 Mon Sep 17 00:00:00 2001
From: ZhouyihaiDing <ddyihai@google.com>
Date: Mon, 25 Sep 2017 23:20:30 +0000
Subject: [PATCH] add protobuf c_ext to php benchmark

---
 src/ruby/qps/proxy-worker.rb                  | 16 +++++++++--
 tools/jenkins/run_full_performance.sh         |  2 +-
 tools/run_tests/performance/run_worker_php.sh | 11 ++++++--
 .../run_tests/performance/scenario_config.py  | 28 +++++++++++++++++++
 4 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/src/ruby/qps/proxy-worker.rb b/src/ruby/qps/proxy-worker.rb
index 488610ae74d..ae7006e7d60 100755
--- a/src/ruby/qps/proxy-worker.rb
+++ b/src/ruby/qps/proxy-worker.rb
@@ -31,8 +31,9 @@ require 'src/proto/grpc/testing/services_services_pb'
 require 'src/proto/grpc/testing/proxy-service_services_pb'
 
 class ProxyBenchmarkClientServiceImpl < Grpc::Testing::ProxyClientService::Service
-  def initialize(port)
+  def initialize(port, c_ext)
     @mytarget = "localhost:" + port.to_s
+    @use_c_ext = c_ext
   end
   def setup(config)
     @config = config
@@ -41,7 +42,13 @@ class ProxyBenchmarkClientServiceImpl < Grpc::Testing::ProxyClientService::Servi
     @histogram = Histogram.new(@histres, @histmax)
     @start_time = Time.now
     # TODO(vjpai): Support multiple client channels by spawning off a PHP client per channel
-    command = "php -d extension=" + File.expand_path(File.dirname(__FILE__)) + "/../../php/ext/grpc/modules/grpc.so " + File.expand_path(File.dirname(__FILE__)) + "/../../php/tests/qps/client.php " + @mytarget
+    if @use_c_ext
+      puts "Use protobuf c extension"
+      command = "php -d extension=" + File.expand_path(File.dirname(__FILE__)) + "/../../php/tests/qps/vendor/google/protobuf/php/ext/google/protobuf/modules/protobuf.so " + "-d extension=" + File.expand_path(File.dirname(__FILE__)) + "/../../php/ext/grpc/modules/grpc.so " + File.expand_path(File.dirname(__FILE__)) + "/../../php/tests/qps/client.php " + @mytarget
+    else
+      puts "Use protobuf php extension"
+      command = "php -d extension=" + File.expand_path(File.dirname(__FILE__)) + "/../../php/ext/grpc/modules/grpc.so " + File.expand_path(File.dirname(__FILE__)) + "/../../php/tests/qps/client.php " + @mytarget
+    end	
     puts "Starting command: " + command
     @php_pid = spawn(command)
   end
@@ -128,6 +135,9 @@ def proxymain
     opts.on('--driver_port PORT', '<port>') do |v|
       options['driver_port'] = v
     end
+    opts.on("-c", "--[no-]c_proto_ext", "Use protobuf C-extention") do |c|
+      options[:c_ext] = c
+    end
   end.parse!
 
   # Configure any errors with client or server child threads to surface
@@ -136,7 +146,7 @@ def proxymain
   s = GRPC::RpcServer.new
   port = s.add_http2_port("0.0.0.0:" + options['driver_port'].to_s,
                           :this_port_is_insecure)
-  bmc = ProxyBenchmarkClientServiceImpl.new(port)
+  bmc = ProxyBenchmarkClientServiceImpl.new(port, options[:c_ext])
   s.handle(bmc)
   s.handle(ProxyWorkerServiceImpl.new(s, bmc))
   s.run
diff --git a/tools/jenkins/run_full_performance.sh b/tools/jenkins/run_full_performance.sh
index a9661c7e266..aae76b0fde8 100755
--- a/tools/jenkins/run_full_performance.sh
+++ b/tools/jenkins/run_full_performance.sh
@@ -21,7 +21,7 @@ cd $(dirname $0)/../..
 
 # run 8core client vs 8core server
 tools/run_tests/run_performance_tests.py \
-    -l c++ csharp node ruby java python go node_express php \
+    -l c++ csharp node ruby java python go node_express php php_ext \
     --netperf \
     --category scalable \
     --bq_result_table performance_test.performance_experiment \
diff --git a/tools/run_tests/performance/run_worker_php.sh b/tools/run_tests/performance/run_worker_php.sh
index 5d0c4fa4fd3..e524d5286d0 100755
--- a/tools/run_tests/performance/run_worker_php.sh
+++ b/tools/run_tests/performance/run_worker_php.sh
@@ -16,13 +16,18 @@
 source ~/.rvm/scripts/rvm
 set -ex
 
-repo=$(dirname $0)/../../..
-
+cd $(dirname $0)/../../..
+repo=$(pwd)
 # First set up all dependences needed for PHP QPS test
 cd $repo
 cd src/php/tests/qps
 composer install
+# Install protobuf C-extension for php
+cd vendor/google/protobuf/php/ext/google/protobuf
+phpize
+./configure
+make
 # The proxy worker for PHP is implemented in Ruby
-cd ../../../..
+cd $repo
 ruby src/ruby/qps/proxy-worker.rb $@
 
diff --git a/tools/run_tests/performance/scenario_config.py b/tools/run_tests/performance/scenario_config.py
index 5efc9f56485..31017f1d85a 100644
--- a/tools/run_tests/performance/scenario_config.py
+++ b/tools/run_tests/performance/scenario_config.py
@@ -827,6 +827,33 @@ class PhpLanguage:
     return 'php'
 
 
+class PhpLanguage_ext:
+
+  def __init__(self):
+    pass
+    self.safename = str(self)
+
+  def worker_cmdline(self):
+    return ['tools/run_tests/performance/run_worker_php.sh -c']
+
+  def worker_port_offset(self):
+    return 800
+
+  def scenarios(self):
+    yield _ping_pong_scenario(
+        'php_to_cpp_protobuf_sync_unary_ping_pong', rpc_type='UNARY',
+        client_type='SYNC_CLIENT', server_type='SYNC_SERVER',
+        server_language='c++', async_server_threads=1)
+
+    yield _ping_pong_scenario(
+        'php_to_cpp_protobuf_sync_streaming_ping_pong', rpc_type='STREAMING',
+        client_type='SYNC_CLIENT', server_type='SYNC_SERVER',
+        server_language='c++', async_server_threads=1)
+
+  def __str__(self):
+    return 'php'
+
+
 class JavaLanguage:
 
   def __init__(self):
@@ -1025,6 +1052,7 @@ LANGUAGES = {
     'node_express': NodeExpressLanguage(),
     'ruby' : RubyLanguage(),
     'php' : PhpLanguage(),
+    'php_ext' : PhpLanguage_ext(),
     'java' : JavaLanguage(),
     'python' : PythonLanguage(),
     'go' : GoLanguage(),