You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/02/24 03:37:50 UTC

[GitHub] [spark] LuciferYang edited a comment on pull request #35622: [SPARK-38300][SQL] Use `ByteStreams.toByteArray` to simplify `fileToString` and `resourceToBytes` in catalyst.util

LuciferYang edited a comment on pull request #35622:
URL: https://github.com/apache/spark/pull/35622#issuecomment-1049461442


   ```scala
   
   def testToByteArray(fileSie: Int): Unit = {
   
       val bytes = RandomUtils.nextBytes(fileSie)
       val file0 = File.createTempFile(s"$fileSie-${UUID.randomUUID()}", ".dat")
       val file1 = File.createTempFile(s"$fileSie-${UUID.randomUUID()}", ".dat")
       file0.deleteOnExit()
       file1.deleteOnExit()
       FileUtils.writeByteArrayToFile(file0, bytes)
       FileUtils.writeByteArrayToFile(file1, bytes)
   
       val benchmark = new Benchmark(
         s"ToByteArray with $fileSie ",
         1,
         output = output)
   
       benchmark.addCase("toByteArray") { _: Int =>
         toByteArray(file0)
       }
   
       benchmark.addCase("toByteArray Use Guava") { _: Int =>
         toByteArrayUseGuava(file1)
       }
       benchmark.run()
     }
   
     private def toByteArrayUseGuava(file: File): Array[Byte] = {
       val inStream = new FileInputStream(file)
       try {
         ByteStreams.toByteArray(inStream)
       } finally {
         inStream.close()
       }
     }
   
     private def toByteArray(file: File): Array[Byte] = {
       val inStream = new FileInputStream(file)
       val outStream = new ByteArrayOutputStream
       try {
         var reading = true
         while (reading) {
           inStream.read() match {
             case -1 => reading = false
             case c => outStream.write(c)
           }
         }
         outStream.flush()
       } finally {
         inStream.close()
       }
       outStream.toByteArray
     }
   
     override def runBenchmarkSuite(mainArgs: Array[String]): Unit = {
      // test 1K, 1M, 10M, 100M
       Seq(1024, 1024 * 1024, 1024 * 1024 * 10, 1024 * 1024 * 100).foreach { fileSize =>
         testToByteArray(fileSize)
       }
     }
   ```
   
   I write a micro bench as above to compare the old method and ` ByteStreams.toByteArray`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org