Skip to content

Conversation

@beliefer
Copy link
Contributor

@beliefer beliefer commented Mar 31, 2020

What changes were proposed in this pull request?

After my investigation, SQLQueryTestSuite spent a lot of time compiling the generated java code.
Take group-by.sql as an example.
At first, I added some debug log into SQLQueryTestSuite.
Please reference https://github.com/beliefer/spark/blob/92b6af740c73e33007c21592023eb65635bb0f07/sql/core/src/test/scala/org/apache/spark/sql/SQLQueryTestSuite.scala#L402
The execution command is as follows:
build/sbt "~sql/test-only *SQLQueryTestSuite -- -z group-by.sql"
The output show below:

00:56:06.192 WARN org.apache.spark.sql.SQLQueryTestSuite: group-by.sql using configs: spark.sql.codegen.wholeStage=true. run time: 20604
00:56:13.719 WARN org.apache.spark.sql.SQLQueryTestSuite: group-by.sql using configs: spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=CODEGEN_ONLY. run time: 7526
00:56:18.786 WARN org.apache.spark.sql.SQLQueryTestSuite: group-by.sql using configs: spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=NO_CODEGEN. run time: 5066

According to the log, we know.

Config Run time(ms)
spark.sql.codegen.wholeStage=true 20604
spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=CODEGEN_ONLY 7526
spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=NO_CODEGEN 5066

We should display the total compile time for generated java code.

This PR will add the following to SQLQueryTestSuite's output.

=== Metrics of Whole Codegen ===
Total compile time: 80.564516529 seconds

Note: At first, I wanted to use CodegenMetrics.METRIC_COMPILATION_TIME to do this. After many experiments, I found that CodegenMetrics.METRIC_COMPILATION_TIME is only effective for a single test case, and cannot play a role in the whole life cycle of SQLQueryTestSuite.
I checked the type of CodegenMetrics.METRIC_COMPILATION_TIME is Histogram and the latter preserves 1028 elements. Histogram is a metric which calculates the distribution of a value.

Why are the changes needed?

Display the total compile time for generated java code.

Does this PR introduce any user-facing change?

'No'.

How was this patch tested?

Jenkins test.

@SparkQA
Copy link

SparkQA commented Mar 31, 2020

Test build #120643 has finished for PR 28081 at commit 41aa7e9.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

val codegenInfo =
s"""
|=== Metrics of Whole Codegen ===
|Total compile time: ${CodeGenerator.compileTime.sum.toDouble / NANOS_PER_SECOND} seconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, we need to reset this metric in beforeAll? btw , could we reuse CodegenMetrics.METRIC_COMPILATION_TIME for this purpose? Getting the same metric for the same purpose seems redundant.

Copy link
Contributor Author

@beliefer beliefer Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, I wanted to use CodegenMetrics.METRIC_COMPILATION_TIME to do this. After many experiments, I found that CodegenMetrics.METRIC_COMPILATION_TIME is only effective for a single test case, and cannot play a role in the whole life cycle of SQLQueryTestSuite.
I checked the type of CodegenMetrics.METRIC_COMPILATION_TIME is Histogram that is it only preserves 1028 elements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maropu Histogram is a metric which calculates the distribution of a value.

@maropu
Copy link
Member

maropu commented Apr 1, 2020

Could we display this metric in BenchmarkQueryTest, too?

@SparkQA
Copy link

SparkQA commented Apr 1, 2020

Test build #120662 has finished for PR 28081 at commit 3dd0adf.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@beliefer
Copy link
Contributor Author

beliefer commented Apr 1, 2020

cc @srowen @cloud-fan

val duration = endTime - startTime
val timeMs: Double = duration.toDouble / NANOS_PER_MILLIS
CodegenMetrics.METRIC_SOURCE_CODE_SIZE.update(code.body.length)
CodegenMetrics.METRIC_COMPILATION_TIME.update(timeMs.toLong)
Copy link
Contributor Author

@beliefer beliefer Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodegenMetrics.METRIC_COMPILATION_TIME is a Histogram that is a metric which calculates the distribution of a value. We can't reuse it.

CodegenMetrics.METRIC_SOURCE_CODE_SIZE.update(code.body.length)
CodegenMetrics.METRIC_COMPILATION_TIME.update(timeMs.toLong)
logInfo(s"Code generated in $timeMs ms")
_compileTime.add(duration)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think timeMs is good enough to be recorded.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeMs lost a lot of precision. Because SQLQueryTestSuite contains 231 test cases contain a lot of SQL, tens of thousands of SQL are executed. The loss of accuracy will increase.

@dongjoon-hyun dongjoon-hyun changed the title [SPARK-31315][SQL][TEST] SQLQueryTestSuite: Display the total compile time for generated java code [SPARK-31315][SQL] SQLQueryTestSuite: Display the total compile time for generated java code Apr 1, 2020
Copy link
Member

@dongjoon-hyun dongjoon-hyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a source code under src/main is changed, [TESTS] should not be used.


def compileTime: Long = _compileTime.sum

def resetCompileTime: Unit = _compileTime.reset()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

// bytecode instruction
final val MUTABLESTATEARRAY_SIZE_LIMIT = 32768

// Display the total compile time for generating java code in the output of the test case.
Copy link
Member

@dongjoon-hyun dongjoon-hyun Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this is designed for a test case, this is a general accumulator in the context of CodeGenerator. Could you revise into a more proper description?
cc @gatorsmile

Also, this doesn't Display anything here. The warning log happens in afterAll in SQLQueryTestSuite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I will supplement a more proper description.

@beliefer
Copy link
Contributor Author

beliefer commented Apr 2, 2020

When a source code under src/main is changed, [TESTS] should not be used.

OK. Thank you. I get it.

@beliefer
Copy link
Contributor Author

beliefer commented Apr 2, 2020

Could we display this metric in BenchmarkQueryTest, too?

The run time of BenchmarkQueryTest is smaller. I'm not sure if I should consider it.

@SparkQA
Copy link

SparkQA commented Apr 2, 2020

Test build #120704 has finished for PR 28081 at commit 1d7dc2f.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

private val _compileTime = new LongAccumulator

// Visible for testing
def compileTime: Long = _compileTime.sum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should mention that it's time in nanoseconds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I will add it.

@cloud-fan
Copy link
Contributor

The last commit just adds a line of comment. I'm merging it to master, thanks!

@cloud-fan cloud-fan closed this in a9260d0 Apr 2, 2020
@beliefer
Copy link
Contributor Author

beliefer commented Apr 2, 2020

@cloud-fan @maropu @dongjoon-hyun Thanks for all your help!

@SparkQA
Copy link

SparkQA commented Apr 2, 2020

Test build #120717 has finished for PR 28081 at commit eb27c3a.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

// bytecode instruction
final val MUTABLESTATEARRAY_SIZE_LIMIT = 32768

// The java source code generated by whole codegen is compiled on the Driver side and sent to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @beliefer ,

I'm really sorry to post edit suggestions after the PR is merged. I noticed this PR just now...

If we have a chance to make follow-up changes, could you please update java code to Java code, and whole codegen to whole-stage codegen?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Thanks for your remind.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beliefer can you send a followup PR to address it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cloud-fan @rednaxelafx I make a followup PR #28112.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one? #28112

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

cloud-fan pushed a commit that referenced this pull request Apr 6, 2020
… for generated java code

### What changes were proposed in this pull request?
After my investigation, SQLQueryTestSuite spent a lot of time to generate java code.
This PR is related to #28081.
#28081 used to display compile time, but this PR used to display generate time.
This PR will add the following to `SQLQueryTestSuite`'s output.
```
=== Metrics of Whole Codegen ===

Total generate time: 82.640913862 seconds

Total compile time: 98.649663572 seconds
```

### Why are the changes needed?
Display the total generate time for generated java code.

### Does this PR introduce any user-facing change?
'No'.

### How was this patch tested?
Jenkins test.

Closes #28105 from beliefer/output-codegen-generation-time.

Lead-authored-by: beliefer <[email protected]>
Co-authored-by: Jiaan Geng <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
sjincho pushed a commit to sjincho/spark that referenced this pull request Apr 15, 2020
…for generated java code

### What changes were proposed in this pull request?
After my investigation, `SQLQueryTestSuite` spent a lot of time compiling the generated java code.
Take `group-by.sql` as an example.
At first, I added some debug log into `SQLQueryTestSuite`.
Please reference https://github.com/beliefer/spark/blob/92b6af740c73e33007c21592023eb65635bb0f07/sql/core/src/test/scala/org/apache/spark/sql/SQLQueryTestSuite.scala#L402
The execution command is as follows:
`build/sbt "~sql/test-only *SQLQueryTestSuite -- -z group-by.sql"`
The output show below:
```
00:56:06.192 WARN org.apache.spark.sql.SQLQueryTestSuite: group-by.sql using configs: spark.sql.codegen.wholeStage=true. run time: 20604
00:56:13.719 WARN org.apache.spark.sql.SQLQueryTestSuite: group-by.sql using configs: spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=CODEGEN_ONLY. run time: 7526
00:56:18.786 WARN org.apache.spark.sql.SQLQueryTestSuite: group-by.sql using configs: spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=NO_CODEGEN. run time: 5066
```
According to the log, we know.

Config | Run time(ms)
-- | --
spark.sql.codegen.wholeStage=true | 20604
spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=CODEGEN_ONLY | 7526
spark.sql.codegen.wholeStage=false,spark.sql.codegen.factoryMode=NO_CODEGEN | 5066

We should display the total compile time for generated java code.

This PR will add the following to `SQLQueryTestSuite`'s output.
```
=== Metrics of Whole Codegen ===
Total compile time: 80.564516529 seconds
```

Note: At first, I wanted to use `CodegenMetrics.METRIC_COMPILATION_TIME` to do this. After many experiments, I found that `CodegenMetrics.METRIC_COMPILATION_TIME` is only effective for a single test case, and cannot play a role in the whole life cycle of `SQLQueryTestSuite`.
I checked the type of  ` CodegenMetrics.METRIC_COMPILATION_TIME` is `Histogram` and the latter preserves 1028 elements.` Histogram` is a metric which calculates the distribution of a value.

### Why are the changes needed?
Display the total compile time for generated java code.

### Does this PR introduce any user-facing change?
'No'.

### How was this patch tested?
Jenkins test.

Closes apache#28081 from beliefer/output-codegen-compile-time.

Lead-authored-by: beliefer <[email protected]>
Co-authored-by: gengjiaan <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
sjincho pushed a commit to sjincho/spark that referenced this pull request Apr 15, 2020
… for generated java code

### What changes were proposed in this pull request?
After my investigation, SQLQueryTestSuite spent a lot of time to generate java code.
This PR is related to apache#28081.
apache#28081 used to display compile time, but this PR used to display generate time.
This PR will add the following to `SQLQueryTestSuite`'s output.
```
=== Metrics of Whole Codegen ===

Total generate time: 82.640913862 seconds

Total compile time: 98.649663572 seconds
```

### Why are the changes needed?
Display the total generate time for generated java code.

### Does this PR introduce any user-facing change?
'No'.

### How was this patch tested?
Jenkins test.

Closes apache#28105 from beliefer/output-codegen-generation-time.

Lead-authored-by: beliefer <[email protected]>
Co-authored-by: Jiaan Geng <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>

// Reset compile time.
// Visible for testing
def resetCompileTime: Unit = _compileTime.reset()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods should be declared with parentheses, unless they are accessors that have no side-effect. See: https://github.com/databricks/scala-style-guide#parentheses

Fixed in #28252

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gatorsmile Thanks. I got it now.

@beliefer beliefer deleted the output-codegen-compile-time branch April 23, 2024 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants