/
FileDataWriter.scala
94 lines (83 loc) · 3.28 KB
/
FileDataWriter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Copyright 2011-2012 eBusiness Information, Groupe Excilys (www.excilys.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.excilys.ebi.gatling.core.result.writer
import java.io.{ OutputStreamWriter, FileOutputStream, BufferedOutputStream }
import java.util.concurrent.CountDownLatch
import scala.tools.nsc.io.{ File, Directory }
import com.excilys.ebi.gatling.core.config.GatlingFiles.{ simulationLogFile, resultFolder }
import com.excilys.ebi.gatling.core.result.message.{ InitializeDataWriter, ActionInfo }
import com.excilys.ebi.gatling.core.util.DateHelper.{ printResultDate, printFileNameDate }
import com.excilys.ebi.gatling.core.util.FileHelper.TABULATION_SEPARATOR
import com.excilys.ebi.gatling.core.util.StringHelper.{ END_OF_LINE, EMPTY }
import akka.actor.scala2ActorRef
/**
* File implementation of the DataWriter
*
* It writes the data of the simulation if a tabulation separated values file
*/
class FileDataWriter extends DataWriter {
/**
* The OutputStreamWriter used to write to files
*/
var osw: OutputStreamWriter = null
/**
* The countdown latch that will be decreased when all messaged are written and all scenarios ended
*/
var latch: CountDownLatch = null
/**
* The date on which the simulation started
*/
var runOn = EMPTY
/**
* Method called when this actor receives a message
*/
def receive = {
// If the message comes from an action
case ActionInfo(scenarioName, userId, action, executionStartDate, executionDuration, resultStatus, resultMessage) => {
// Builds the line to be written
val strBuilder = new StringBuilder
strBuilder.append(runOn).append(TABULATION_SEPARATOR)
.append(scenarioName).append(TABULATION_SEPARATOR)
.append(userId).append(TABULATION_SEPARATOR)
.append(action).append(TABULATION_SEPARATOR)
.append(printResultDate(executionStartDate)).append(TABULATION_SEPARATOR)
.append(executionDuration).append(TABULATION_SEPARATOR)
.append(resultStatus).append(TABULATION_SEPARATOR)
.append(resultMessage).append(TABULATION_SEPARATOR)
.append(END_OF_LINE)
// Write the line in the file
osw.write(strBuilder.toString)
if (latch.getCount == 1 && self.dispatcher.mailboxSize(self) == 0) {
try {
// Closes the OutputStreamWriter
osw.flush
} finally {
// Decrease the latch (should be at 0 here)
latch.countDown
osw.close
}
}
}
// If the message is sent to initialize the writer
case InitializeDataWriter(runOn, latch) => {
this.runOn = printFileNameDate(runOn)
// Initialize files and folders that will be used to write the logs
Directory(resultFolder(this.runOn)).createDirectory()
osw = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(File(simulationLogFile(this.runOn)).jfile, true)))
this.latch = latch
}
}
}