finished Website Fingerprinting Lab

This commit is contained in:
ClF3 2024-11-02 20:50:02 +08:00
parent b08e254f2f
commit 868c83194e
8 changed files with 63 additions and 10 deletions

View File

@ -1,7 +1,7 @@
const runs = 10;
const N= 393216;
function measureOneLine() {
const LINE_SIZE = 16; // 128/sizeof(double) Note that js treats all numbers as double
const LINE_SIZE = 8; // 64/sizeof(double) Note that js treats all numbers as double
let result = [];
// Fill with -1 to ensure allocation
@ -12,16 +12,27 @@ function measureOneLine() {
let val = M[i * LINE_SIZE];
const end = performance.now();
result.push(end - start);
result.push((end - start).toFixed(2));
}
return result;
}
function measureNLines() {
const LINE_SIZE = 8; // 64/sizeof(double) Note that js treats all numbers as double
let result = [];
// TODO: Exercise 1-1
// Fill with -1 to ensure allocation
const M = new Array(runs * N * LINE_SIZE).fill(-1);
for (let i = 0; i < runs; i++) {
const start = performance.now();
for (let j = 0; j < N; j++) {
let val = M[i * N * LINE_SIZE + j * LINE_SIZE];
}
const end = performance.now();
result.push((end - start).toFixed(2));
}
return result;
}

View File

@ -7,7 +7,9 @@ from sklearn.model_selection import train_test_split
def eval():
y_pred_full, y_test_full = [], []
traces=json.load(open('../traces_nomem.out'))["traces"]
labels=json.load(open('../traces_nomem.out'))["labels"]
print("Number of traces: ", len(traces))
# Re-train 10 times in order to reduce effects of randomness
for i in range(10):
### TODO: Exercise 2-5
@ -15,6 +17,10 @@ def eval():
### 2. Split data into X_train, X_test, y_train, y_test with train_test_split
### 3. Train classifier with X_train and y_train
### 4. Use classifier to make predictions on X_test. Save the result to a variable called y_pred
X_train, X_test, y_train, y_test = train_test_split(traces, labels, test_size=0.2)
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
# Do not modify the next two lines
y_test_full.extend(y_test)
@ -22,6 +28,8 @@ def eval():
### TODO: Exercise 2-5 (continued)
### 5. Print classification report using y_test_full and y_pred_full
print(classification_report(y_test_full, y_pred_full))
if __name__ == "__main__":
eval()

View File

@ -1,6 +1,7 @@
const N= 393216; // Number of cache lines 24MB/64B
// Number of sweep counts
// TODO (Exercise 2-1): Choose an appropriate value!
let P = 1000;
let P = 10;
// Number of elements in your trace
let K = 5 * 1000 / P;
@ -12,19 +13,33 @@ let T;
let start;
function record() {
// Create empty array for saving trace values
T = new Array(K);
// Fill array with -1 so we can be sure memory is allocated
T.fill(-1, 0, T.length);
const LINE_SIZE = 8; // 64/sizeof(double) Note that js treats all numbers as double
const M = new Array(N * LINE_SIZE).fill(-1);
// Save start timestamp
start = performance.now();
for (let i = 0; i < K; i++) {
let count=0;
while (performance.now() - start < P*(i+1)) {
// sweep the cache
for (let j = 0; j < N; j++) {
let val = M[j * LINE_SIZE];
}
count++;
}
T[i]=count;
}
// TODO (Exercise 2-1): Record data for 5 seconds and save values to T.
// Once done recording, send result to main thread
postMessage(JSON.stringify(T));
console.log(T);
}
// DO NOT MODIFY BELOW THIS LINE -- PROVIDED BY COURSE STAFF

View File

@ -1,6 +1,7 @@
const N= 393216; // Number of cache lines 24MB/64B
// Number of sweep counts
// TODO (Exercise 3-1): Choose an appropriate value!
let P = 1000;
let P = 10;
// Number of elements in your trace
let K = 5 * 1000 / P;
@ -12,19 +13,33 @@ let T;
let start;
function record() {
// Create empty array for saving trace values
T = new Array(K);
// Fill array with -1 so we can be sure memory is allocated
T.fill(-1, 0, T.length);
const LINE_SIZE = 8; // 64/sizeof(double) Note that js treats all numbers as double
const M = new Array(N * LINE_SIZE).fill(-1);
// Save start timestamp
start = performance.now();
for (let i = 0; i < K; i++) {
let count=0;
while (performance.now() - start < P*(i+1)) {
// sweep the cache
for (let j = 0; j < N; j++) {
// let val = M[j * LINE_SIZE];
}
count++;
}
T[i]=count;
}
// TODO (Exercise 3-1): Record data for 5 seconds and save values to T.
// TODO (Exercise 2-1): Record data for 5 seconds and save values to T.
// Once done recording, send result to main thread
postMessage(JSON.stringify(T));
console.log(T);
}
// DO NOT MODIFY BELOW THIS LINE -- PROVIDED BY COURSE STAFF

1
traces.out Normal file

File diff suppressed because one or more lines are too long

1
traces_4.out Normal file

File diff suppressed because one or more lines are too long

1
traces_example.out Normal file

File diff suppressed because one or more lines are too long

1
traces_nomem.out Normal file

File diff suppressed because one or more lines are too long