File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 1717// under the License.
1818//
1919
20+ import Dispatch
2021import Foundation
2122import GRPCCore
2223import GRPCNIOTransportHTTP2
@@ -116,12 +117,26 @@ public actor SparkSession {
116117 return try await DataFrame ( spark: self , sqlText: sqlText)
117118 }
118119
120+ /// Returns a ``DataFrameReader`` that can be used to read non-streaming data in as a
121+ /// `DataFrame`
119122 var read : DataFrameReader {
120123 get {
121124 return DataFrameReader ( sparkSession: self )
122125 }
123126 }
124127
128+ /// Executes some code block and prints to stdout the time taken to execute the block.
129+ /// - Parameter f: A function to execute.
130+ /// - Returns: The result of the executed code.
131+ public func time< T: Sendable > ( _ f: ( ) async throws -> T ) async throws -> T {
132+ let start = DispatchTime . now ( )
133+ let ret = try await f ( )
134+ let end = DispatchTime . now ( )
135+ let elapsed = ( end. uptimeNanoseconds - start. uptimeNanoseconds) / 1_000_000
136+ print ( " Time taken: \( elapsed) ms " )
137+ return ret
138+ }
139+
125140 /// This is defined as the return type of `SparkSession.sparkContext` method.
126141 /// This is an empty `Struct` type because `sparkContext` method is designed to throw
127142 /// `UNSUPPORTED_CONNECT_FEATURE.SESSION_SPARK_CONTEXT`.
Original file line number Diff line number Diff line change @@ -74,4 +74,15 @@ struct SparkSessionTests {
7474 #expect( try await spark. range ( 0 , 100 , 2 ) . count ( ) == 50 )
7575 await spark. stop ( )
7676 }
77+
78+ @Test
79+ func time( ) async throws {
80+ let spark = try await SparkSession . builder. getOrCreate ( )
81+ #expect( try await spark. time ( spark. range ( 1000 ) . count) == 1000 )
82+ #if !os(Linux)
83+ #expect( try await spark. time ( spark. range ( 1 ) . collect) == [ [ " 0 " ] ] )
84+ try await spark. time ( spark. range ( 10 ) . show)
85+ #endif
86+ await spark. stop ( )
87+ }
7788}
You can’t perform that action at this time.
0 commit comments