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 17
17
// under the License.
18
18
//
19
19
20
+ import Dispatch
20
21
import Foundation
21
22
import GRPCCore
22
23
import GRPCNIOTransportHTTP2
@@ -116,12 +117,26 @@ public actor SparkSession {
116
117
return try await DataFrame ( spark: self , sqlText: sqlText)
117
118
}
118
119
120
+ /// Returns a ``DataFrameReader`` that can be used to read non-streaming data in as a
121
+ /// `DataFrame`
119
122
var read : DataFrameReader {
120
123
get {
121
124
return DataFrameReader ( sparkSession: self )
122
125
}
123
126
}
124
127
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
+
125
140
/// This is defined as the return type of `SparkSession.sparkContext` method.
126
141
/// This is an empty `Struct` type because `sparkContext` method is designed to throw
127
142
/// `UNSUPPORTED_CONNECT_FEATURE.SESSION_SPARK_CONTEXT`.
Original file line number Diff line number Diff line change @@ -74,4 +74,15 @@ struct SparkSessionTests {
74
74
#expect( try await spark. range ( 0 , 100 , 2 ) . count ( ) == 50 )
75
75
await spark. stop ( )
76
76
}
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
+ }
77
88
}
You can’t perform that action at this time.
0 commit comments