LuciferYang commented 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 = {
       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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to